We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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

I have the "The record doesn't have a valid data snapshot" message when I try to access hasChanged method when I update a model.

here is my controller (somme line are deleted)

In the code below, the hasChanged works only because I added $serie->setSnapshotData($originalData); but it should not be mandatory to add this manually.

        $serie = $seriesService->getSeriesBySlug($slug);
        $originalData = $serie->toArray();
        $form = new SeriesForm();
        $form->setEntity($serie);
        try {
            if ($this->request->isPost()) {
               //...
                } else {
                    $form->bind($this->request->getPost(), $serie);
                    $seriesService = $this->di->get('SeriesService');
                    $serie->setSnapshotData($originalData);
                    $seriesService->saveSeries($serie);
                    //redirect to addChapter page for this series.
                    $this->response->redirect('admin/series/');
                }
            } else {
                //GET

            }
        }
    ...

Am I doing something wrong ?

Phalcon verison : 3.2.2



145.0k
Accepted
answer
edited Nov '17

Are you sure you have keepSnapshots in initialize method? After finding it should correctly set snapshot data already.

Also snapshot in phalcon had many bugs and it was inconsistent. Right now phalcon updates snapshot after saving a model. If you want to check what fields where updated you can use hasUpdated or set phalcon.orm.update_snapshot_on_save in php.ini to 0



8.2k

Hmm Itried that in the initialize methof on, my Series model but didn't work. I'll try again, but I don't think it will works.



8.2k

Ok, just tried again It seems to be working with keepSnapshots(true) ... I had to dod something wrong earlier. however, the hasUpdated return false for a field that was changed and hasChanged reutn true in that case. Dunno how hasUpdated have to be used (maybe AFTER an update).

Anyways, thanks, If I have another problem I'll comme back on this thread.

edited Nov '17

Yes. After an update. Simpy snapshot right now is always updated after save. So to check if something was updated use hasUpdated, if you want something to change if it was changed but before update use hasChanged. I think it's kind of logicial.