We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 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?


What if you try

     $row->id = $id;


     $row->id = $id;
     $row->field_name = $data['name'];


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 http://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.


Actually, I'm avoiding use save(), because of what was said in this topic: http://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!