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

I add this to test snapshot in my model

public function afterCreate() {

    $changedFields = $this->getChangedFields();
    if (count($changedFields)) {
        foreach ($changedFields as $field) {
            $auditar = new Auditoria();

            $auditar->AUD_MODELNAME = $field;
            $auditar->AUD_IPADRESS = $request->getClientAddress();
            $auditar->AUD_OLDVALUE = $originalData[$field];
            $auditar->save();
            if ($auditar->save() == false) {
                foreach ($auditar->getMessages() as $message) {
                    echo $message, "\n";
                }
            }
        }
    }
}

I add this line to my function initialize

public function initialize() {
    $this->setSchema("SPOLS");
    $this->hasMany('CONT_CODIGO', 'SPMREFERENCIA', 'CONT_CODIGO', array('alias' => 'SPMREFERENCIA'));
    $this->hasMany('CONT_CODIGO', 'SPTDETALLE', 'CONT_CODIGO', array('alias' => 'SPTDETALLE'));
    $this->hasMany('CONT_CODIGO', 'SPTENCABEZADO', 'CONT_CODIGO', array('alias' => 'SPTENCABEZADO'));

    $this->keepSnapshots(true);

}

but when saving me the following message

The record doesn't have a valid data snapshot

Could it be because some fields left empty?



125.8k
Accepted
answer

The problem is because you're calling getChangedFields() in afterCreate(). There are no changed fields, because there were no values in the fields before the record was created. Therefore there was no state of the record for Phalcon to snapshot. If you put that code in afterSave(), and wrapped it in a call to hasChangedFields(), then it would work.

Is your first code block intended to be production code at all? There a few things you could improve - I'd be happy to point them out if you want.



81.2k

Yes, I realized later. Yes, I realized later. This is not the test code. I correctly understand its operation before finishing it. My objective is to reach this

https://blog.phalcon.io/post/tutorial-creating-a-blameable-behavior-with

Where I really wanted to help it is to use routes and acl because I never work that way'm doing my first tests and there if I'm confused

I got ahead of myself. After looking at your code closer, there's not really a whole lot to change.