cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

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

Open ekmst opened this issue 7 years ago • 5 comments

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

ekmst avatar Oct 13 '19 22:10 ekmst

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?

niden avatar Oct 13 '19 22:10 niden

Thanks @niden!

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

ekmst avatar Oct 13 '19 23:10 ekmst

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"

ruudboon avatar Oct 14 '19 05:10 ruudboon

I agree with @ruudboon

sergeyklay avatar Oct 14 '19 07:10 sergeyklay

Implemented

niden avatar Oct 14 '19 16:10 niden