Welcome Guest, Not a member yet? Register   Sign In
DataException: There is no data to update after using save()
#11

Added the word DataException in the user guide:
See https://github.com/codeigniter4/CodeIgni...568635262f
Reply
#12

(This post was last modified: 06-14-2024, 08:41 AM by davis.lasis.)

Hi guys,
This feature still does not work as smooth as we would like.
Problem - `updated_at` is not actually updated to NOW() datetime.
New model trigger $updateOnlyChanged does great magic, but not exactly correct in my opinion
if we set this to false in model
PHP Code:
protected bool $updateOnlyChanged false

If i use any update function or one of my favourite 
PHP Code:
model(ItemModel::class)->save($ItemEntity); 

if entity has no changed data - it will save in database, but updated_at will stay the same old value, just because all entity data is pused to BaseModel, including "old" updated_at
Cause of this issue is in BaseModel->setUpdatedField(). If provided data contains updated_at, system will not change `updated_at` value.
PHP Code:
protected $useTimestamps true

I think that if  my model has turned on timestamps - `updated_at` must update to NOW() at any condition.
BaseModel->setUpdatedField() 3rd IFs condition `array_key_exists` should be removed
PHP Code:
    /**
    * Set datetime to updated field.
    *
    * @phpstan-param row_array  $row
    * @param        int|string $date timestamp or datetime string
    */
    protected function setUpdatedField(array $row$date): array
    {
        if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField$row)) {
            $row[$this->updatedField] = $date;
        }

        return $row;
    

https://github.com/codeigniter4/CodeIgni...l.php#L887

Or maybe removing "There is no data to update" exception?
What are your thoughts on all this?
Reply
#13

Are you sure that you did NOT add `updated_at` in the $allowedFields?
Reply
#14

Hi, @kenjis

WOW! That's what you (well... `me`) get on friday evening when head is already boiling up!
ha ha ha
I did not catch-up so simple reverse logic. How shame on me, but, I guess, we all been there and that's why sometimes we need extra pair of eyes.

And, yes, from previous CI versions, I had a habbit to always provide `updated_at` in $allowedFields array. 
Removing it from that array, obviously works perfectly and forfills my needs.
That's why I love CI so much - you can twist and use mainframe logic in a different ways, if you need so.

Again, thanks for pointing to right direction.
And peaceful coding to everyone!

Cheers,
Dāvis
Reply




Theme © iAndrew 2016 - Forum software by © MyBB