Welcome Guest, Not a member yet? Register   Sign In
CI4.0rc3 + calling model->save() on unchanged entity causes critical error

it seems that calling a model's save() method on an unchanged instance of an Entity, causes
CRITICAL - 2019-11-24 17:57:19 --> You must use the "set" method to update an entry.
#0 /php/system/Database/BaseBuilder.php(2342): CodeIgniter\Database\BaseBuilder->validateUpdate()
#1 /php/system/Model.php(846): CodeIgniter\Database\BaseBuilder->update()
#2 /php/system/Model.php(533): CodeIgniter\Model->update(Array, Array)

The error message here does not help at all.

I was wondering if it is a bug? My suggestions would be:
  • trying to save an unchanged entity should not cause error at all, or
  • if it causes an error / exception, it should return a helpful message.

This error appears then you save empty object/array, CI thinks that you forgot set data.

$myModel->save([]) - shoud give same error

This is a solution from the Basic App CMS. Add this code to model class.

PHP Code:
     * When I save the model without changes i get an error: "You must use the "set" method to update an entry."
     * To fix this I change the $onlyChanged default value from "true" to "false".
     * ToDo: Remove this later...  
    public static function classToArray($data$primaryKey nullstring $dateFormat 'datetime'bool $onlyChanged false) : array 
        return parent::classToArray($data$primaryKey$dateFormat$onlyChanged);

Basic App, an open source simple CMS based on CodeIgniter 4

Theme © iAndrew 2016 - Forum software by © MyBB