cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

The record doesn't have a valid data snapshot in 1.3.0

Open MaksSlesarenko opened this issue 12 years ago • 39 comments

All models use dynamic update, but periodically error message appears Same problem was in previous versions.

According to doc http://docs.phalconphp.com/en/1.3.0/reference/models-cache.html#overriding-find-findfirst you may override find and findFirst method. As I understand problem appears when save failed and refresh is performed, which also uses those methods.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

MaksSlesarenko avatar Jan 20 '14 12:01 MaksSlesarenko

I have the same issue.

andersonamuller avatar Mar 09 '14 02:03 andersonamuller

Same here.

vlad4800 avatar Mar 22 '14 02:03 vlad4800

Could anyone of you post a test to reproduce the issue?

ghost avatar Mar 22 '14 02:03 ghost

I get the same error. I use a custom behavior to maintain timestamps and revisions. My Behavior: http://pastebin.com/znfmLdQT Phalcon version: 1.3.1 PHP: 5.5.10 Ubuntu

I initialize my model with:

public function initialize(){
    $this->addBehavior(new KitCloud\Model\DefaultBehavior());
    $this->keepSnapshots(true);
}

I have also added a function to get the previous value of the field in the Behavior class:

public function _getSnapshotField($field) {
    return (isset($this->_snapshot[$field]) ? $this->_snapshot[$field] : false);
}

Exception:

Phalcon\Mvc\Model\Exception Object
(
    [message:protected] => The record doesn't have a valid data snapshot
    [string:Exception:private] => 
    [code:protected] => 0
    [file:protected] => mypath\Model\DefaultBehavior.php
    [line:protected] => 27
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [file] => mypath\Model\DefaultBehavior.php
                    [line] => 27
                    [function] => getChangedFields
                    [class] => Phalcon\Mvc\Model
                    [type] => ->
                    [args] => Array
                        (
                        )

                )
)

fl0pp avatar May 22 '14 10:05 fl0pp

Reproduction of the error in Phalcon 1.3.1 @phalcon https://gist.github.com/SquareOfPi/a7f1910377cf2501599f

CodingBeard avatar Jul 27 '14 21:07 CodingBeard

Is there any update on this?

fl0pp avatar Oct 27 '14 14:10 fl0pp

Bump. I am experiencing this problem also.

blm14 avatar Oct 30 '14 15:10 blm14

I did a test right now. I am working with serialized data from a session, and got this error when I unserialized a session and executed "$user-update()". When I loaded a new Model I did not get the error:

$user = unserialize($this->session->get('user'));
$user = User::findFirst("id=".$user->id);
$user->testfield = 1;
$user->update();

fl0pp avatar Nov 04 '14 12:11 fl0pp

+1 get same error when using behavior and trying to catch beforeSave event

mzf avatar Nov 18 '14 08:11 mzf

+1 same here. Does anybody know of some workaround or dirty quick fix?

DusanBrejka avatar Dec 08 '14 18:12 DusanBrejka

In my case it turned out that I had the

$this->keepSnapshots(true);

in the parent class and when I overrode initialize() in the child class I forgot to call parent::initialize(); first. :)

blm14 avatar Dec 08 '14 18:12 blm14

Unfortunately this is not my case. This bug only occurs if the entity has been created before during the same script execution. Phalcon version 1.3.4

This is my User dummy model

<?php
class User extends \Phalcon\Mvc\Model {
    public function initialize() {
        $this->keepSnapshots(true);
    }
}

Create, update (does not work)

$user = new \User();
// also tried $this->getDI()->get('modelsManager')->keepSnapshots($user, true);
echo $user->getModelsManager()->isKeepingSnapshots($user) . '<br>';

$user->create([
    'firstName' => 'foo',
    'lastName' => 'bar',
    'password' => 'test'
]);
// also tried $user->refresh();
$user->firstName = 'test';
$user->save();

echo $user->getModelsManager()->isKeepingSnapshots($user) . '<br>';
var_dump($user->getSnapshotData());
1
1
NULL 

Select, update (works)

$user = \User::findFirst(1);

$user->firstName = 'test';
$user->save();

var_dump($user->getSnapshotData());
array(4) {
  ["userID"]=>
  string(2) "1"
  ["firstName"]=>
  string(3) "test"
  ["lastName"]=>
  string(4) "bar"
  ["password"]=>
  string(4) "test"
}

Or am I missing something?

DusanBrejka avatar Dec 08 '14 19:12 DusanBrejka

100% reproducible, just call getChangedFields() for object not yet saved or loaded from db (newly created).

e-oz avatar Jan 20 '15 22:01 e-oz

I've tried getChangedFields() with Create and Update steps posted by Dusan but it just returns 'The record doesn't have a valid data snapshot'.

marcTJP avatar Jan 21 '15 15:01 marcTJP

To fix the 'The record doesn't have a valid data snapshot' you need to do one of the two things:

  1. Fetch record from database, example: $model = MyModel::findFirst($id);
  2. Explicitly set snapshot data when you need it: $myModel->setSnapshotData($myModel->toArray());

The bug is that setSnapshotData() is called internally only when you fetch data from database, and is NOT called when you save data or simply assign fields. If it's not a bug, it needs to be at least documented.

msty avatar Jan 23 '15 07:01 msty

@msty thanks for fix, maybe in version 2 will correct

mzf avatar Mar 16 '15 08:03 mzf

I'm having the same issue when fetching multiple records in a loop using findFirst and then later updating them again in another loop based on some conditions. @msty the fix works but can you give me some more insight on why it works ?

daveamol avatar Jun 11 '15 14:06 daveamol

create, update does NOT work.

manish-ga avatar Jun 16 '15 08:06 manish-ga

is there a better solution than mine? ty

public function beforeCreate() {
    $this->setSnapshotData($this->toArray());
}

public function afterSave() {
    if ($this->hasChanged('field')) {
        //do stuff..
    }
}

jturbide avatar Sep 17 '15 18:09 jturbide

Are you using Phalcon >= 2.0.7?

andresgutierrez avatar Sep 17 '15 19:09 andresgutierrez