CodeIgniter Forums
DataException: There is no data to update after using save() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: DataException: There is no data to update after using save() (/showthread.php?tid=87742)

Pages: 1 2


RE: DataException: There is no data to update after using save() - kenjis - 01-30-2024

Added the word DataException in the user guide:
See https://github.com/codeigniter4/CodeIgniter4/pull/8455/files#diff-0252a08383c6e5972011c9835562823bdfef315b4d69a691792771568635262f


RE: DataException: There is no data to update after using save() - davis.lasis - 06-14-2024

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/CodeIgniter4/blob/develop/system/BaseModel.php#L887

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


RE: DataException: There is no data to update after using save() - kenjis - 06-14-2024

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


RE: DataException: There is no data to update after using save() - davis.lasis - 06-17-2024

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