Welcome Guest, Not a member yet? Register   Sign In
Model Update ignores null
#1

(This post was last modified: 12-29-2021, 08:12 AM by AngelRodriguez.)

Hi, I have a weird problem, I don't know if I'm doing something wrong or it is a bug.
I have a soft delete for customers. CustomerModel and Customer Entity.
Database field: customer_deleted: 1,0, NULL (Default:NULL)
Code:
PHP Code:
 
$customerModel 
= new CustomerModel();
 $customer = new Customer();
$customer->customer_deleted null;
$customer->customer_deleted_at date('Y-m-d H:i:s');
 
$customerModel->update($customer_id$customer); 

This code update the date but does not set customer_deleted to NULL, it ignores it.

If I use array instead of entity object, It works, updates date and set deleted = null

PHP Code:
 
 $customerModel 
= new CustomerModel();
$array = [
     
'customer_deleted' => null,
     
'customer_deleted_at' => date('Y-m-d H:i:s')
 ];
$customerModel->update($customer_id$array); 
 

Why passing data as entity object does not work?
Maybe codeigniter ignore data = null when calling update()?

Thank you
Reply
#2

(This post was last modified: 12-29-2021, 05:46 PM by kenjis.)

Why customer_deleted is null and customer_deleted_at is not null?
It does not make sense. Is it soft-deleted or not?

And if you use soft-deletion, you should not change customer_deleted and customer_deleted_at like your code.
Reply
#3

(This post was last modified: 12-30-2021, 03:19 AM by AngelRodriguez.)

(12-29-2021, 05:43 PM)kenjis Wrote: Why customer_deleted is null and customer_deleted_at is not null?
It does not make sense. Is it soft-deleted or not?

And if you use soft-deletion, you should not change customer_deleted and customer_deleted_at like your code.


It just an example.

Forget customer_deleted. Imagine I only use customer_deleted_at. How can I set it to NULL?

Codeigniter query is "deleted_at IS NULL" when you call $model->findAll(), so you need to set that field = null but I don't know how to do it.

If I do $customer->customer_deleted_at = NULL; and then $model->save($customer) (or update($customer)) It doesn't save/update. What need to do?
Reply
#4

Do you want the soft-deleted record to be restored?

How about this?
1. Get the record with withDeleted()
2. Change the properties
3. Save it
Reply
#5

You can pass an array instead of an entity.
PHP Code:
$data = [
    
'field' => 'key',
];
$model->update($id$data);
// or
$data = [
    
'id'      => $id,
    
'field' => 'key',
];
$model->save($data); 
Reply
#6

(This post was last modified: 12-30-2021, 05:10 AM by AngelRodriguez.)

(12-30-2021, 04:34 AM)iRedds Wrote: You can pass an array instead of an entity.
PHP Code:
$data = [
    'field' => 'key',
];
$model->update($id$data);
// or
$data = [
    'id'      => $id,
    'field' => 'key',
];
$model->save($data); 


Yes, I know that array works, but Entity Object doesn't work, is a bug?

(12-30-2021, 04:18 AM)kenjis Wrote: Do you want the soft-deleted record to be restored?

How about this?
1. Get the record with withDeleted()
2. Change the properties
3. Save it

I do that, but if I change properties passing data as object to save(), doesn't work. It only works with array. Is a bug?
Reply
#7

(This post was last modified: 12-30-2021, 04:00 PM by iRedds.)

If everything works with the array, then it will work with the entity.
PHP Code:
class Model extends \CodeIgniter\Model
{
    protected 
$table 'table';
    protected 
$allowedFields = ['customer_deleted''customer_deleted_at'];
}
class 
Entity extends \CodeIgniter\Entity\Entity {}


$e = new Entity();
$e->customer_deleted null;
$e->customer_deleted_at date('Y-m-d H:i:s');

(new 
Model)->update(1$e);
// UPDATE `table` SET `customer_deleted` = NULL, `customer_deleted_at` = '2021-12-29 16:59:19' WHERE `table`.`id` IN (1) 
Reply
#8

@AngelRodriguez
iRedds's code works. I don't know why you can't update.
Reply
#9

(12-31-2021, 03:55 AM)kenjis Wrote: @AngelRodriguez
iRedds's code works. I don't know why you can't update.

Problem solved:

My "Customer Entity" was:

PHP Code:
class Customer extends Entity
{
    protected $attributes = [
        'customer_id' => null,
        'customer_deleted' => null,
        'customer_deleted_at' => null,
    ];

    protected $dates = ['customer_deleted_at'];



Removing " => null", works:

PHP Code:
class Customer extends Entity
{
    protected $attributes = [
        'customer_id',
        'customer_deleted',
        'customer_deleted_at',
    ];

    protected $dates = ['customer_deleted_at'];
}

Thank you
Reply




Theme © iAndrew 2016 - Forum software by © MyBB