The record doesn't have a valid data snapshot in 1.3.0
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.
I have the same issue.
Same here.
Could anyone of you post a test to reproduce the issue?
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
(
)
)
)
Reproduction of the error in Phalcon 1.3.1 @phalcon https://gist.github.com/SquareOfPi/a7f1910377cf2501599f
Is there any update on this?
Bump. I am experiencing this problem also.
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();
+1 get same error when using behavior and trying to catch beforeSave event
+1 same here. Does anybody know of some workaround or dirty quick fix?
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. :)
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?
100% reproducible, just call getChangedFields() for object not yet saved or loaded from db (newly created).
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'.
To fix the 'The record doesn't have a valid data snapshot' you need to do one of the two things:
- Fetch record from database, example: $model = MyModel::findFirst($id);
- 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 thanks for fix, maybe in version 2 will correct
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 ?
create, update does NOT work.
is there a better solution than mine? ty
public function beforeCreate() {
$this->setSnapshotData($this->toArray());
}
public function afterSave() {
if ($this->hasChanged('field')) {
//do stuff..
}
}
Are you using Phalcon >= 2.0.7?