We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Update a row without using find

Guys.... When I update a row like this, works fine:

$row = Models\Avaliacoes::findFirstById( $id );
$row->assign( $data );
return $row->update();

But when I do like this, doesn't work:

$row = new Models\Avaliacoes();
$row->setId( $id );
$row->assign( $data );
return $row->update();

I got this message:

Array ( [0] => Phalcon\Mvc\Model\Message Object ( [_type:protected] => InvalidUpdateAttempt [_message:protected] => Record cannot be updated because it does not exist [_field:protected] => [_model:protected] => )

)

I really have to find the row before updating a record or there is something wrong going on?

Thanks!

What if you try

     $row->id = $id;
     $row->save($data);

or

     $row->id = $id;
     $row->field_name = $data['name'];
       ...
     $row->save();

Hi,

the clean way is use find() metod and then update().

But, you can try to trick phalcon ORM. Try set-up some propertys on newly created model. Take a look on property $_dirtyState here https://phalcon.agent-j.ru/en/1.3.0/Phalcon/Mvc/Model#getDirtyState-details. Probably there is more propertys you need setup to force phalcon updating newly created Models. I never test it, so i wish you luck and pls give us a feedback.

thanks

Actually, I'm avoiding use save(), because of what was said in this topic: https://forum.phalcon.io/discussion/6542/is-really-necessary-to-find-rows-before-delete.

It really bothers me to perform a select on every update or delete, but I will just use the save() method and hope to this behavior be changed in the future.

Thanks again for the replies!