Search code examples
phpcodeigniter-4

I can not update database in CodeIgniter 4 , model's update() function doesn't work


how are you? I have a problem with Mysql DB update using CI 4.x model update function.

$outletUpdateModel->where('id',$data['id'])->set($temp)->update()

There is no error with code. but I can not see any update on db.

Thank you for your time.

I used save() instead of update()


Solution

  • This might be obvious, but make sure you have all the fields you want to update in the $allowedFields property in the your model.

    <?php
    
    namespace App\Models;
    
    use CodeIgniter\Model;
    
    class OutletUpdateModel extends Model
    {
        protected $table      = 'tableName';
        protected $primaryKey = 'primaryKey';
    
        protected $allowedFields = ['field1', 'field2'];
    

    Many times when I ran into situations when records wouldn't get stored or updated, it turned out that I had forgotten to add fields to the $allowedFields array in the model after making changes to the code or database.