Cphalcon: [BUG]: Fatal error on call $model->getChangedFields()

Created on 14 Oct 2019  路  5Comments  路  Source: phalcon/cphalcon

Describe the bug
Fatal error on call $model->getChangedFields()

To Reproduce

    $user = Users::findFirst();

    var_dump($user->getSnapshotData());  // output: null

    print_r($user->getChangedFields()); // fatal error

Steps to reproduce the behavior:

Fatal error: Uncaught Phalcon\Mvc\Model\Exception: The record doesn't have a valid data snapshot in phalcon/Mvc/Model.zep on line 1520

Expected behavior

empty array output

Details

  • Phalcon version: 4.0.0-rc.1
  • PHP Version: 7.3.9
  • Operating System: linux
  • Installation type: installing via package manager
  • Other related info (Database, table schema): mysql
enhancement

Most helpful comment

For getChangedFields to work snapshot is required. If we sent back an empty array the user doesn't know the method isn't working.
If keepSnapshots is true we can return the correct value.

<?php

use Phalcon\Mvc\Model;

class Robots extends Model
{
    public function initialize()
    {
        $this->keepSnapshots(true);
    }
}

Maybe we should update the error. "keepSnapshots must be enabled to track changes"

All 5 comments

This is the intended behavior. If there is no snapshot, an exception is thrown.

We can change this to return an empty array. This will break backwards compatibility.

@phalcon/core-team Thoughts?

Thanks @niden!

In my opinion this is not the case for throwing an exception.
It would be logical to return an empty array

For getChangedFields to work snapshot is required. If we sent back an empty array the user doesn't know the method isn't working.
If keepSnapshots is true we can return the correct value.

<?php

use Phalcon\Mvc\Model;

class Robots extends Model
{
    public function initialize()
    {
        $this->keepSnapshots(true);
    }
}

Maybe we should update the error. "keepSnapshots must be enabled to track changes"

I agree with @ruudboon

Implemented

Was this page helpful?
0 / 5 - 0 ratings