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

Save overwrites fields to NULL

I am running a very simple ->save on a table. I am passing in an array of the field=>values that I would like to update.

$stream_scene_app = new StreamSceneApps();
$temp = $stream_scene_app->save($vars);

The $vars variable contains only (2) fields (id=>3, status=>"on"). However after the save all the fields with exception of the (2) that are passed in are set to NULL. The status field updates properly for the correct record. How do I get Phalcon to not overwrite fields on a save when they are not passed in? I obviously do not want to pass in every field on an update when I just want to update a single field.

I have looked all over and cannot find an answer to this question. There must be something obvious that I am missing.

I finally found another thread mentioning my problem but no answer for it:

https://forum.phalcon.io/discussion/2010/is-there-some-way-to-partial-update-db-field-



6.6k
Accepted
answer
edited Dec '14

Ah. Thank you to the following post.

https://forum.phalcon.io/discussion/2333/unable-to-update-with-confidence-just-create-nonesense

"Make sure you are using MyModelName::findFirst($id) not new MyModelName(); in your controller." posted by Andy Myers.

So in your controller create a little if else block. If "id" is missing than new MyModelName() else MyModelName::findFirst($id).

Seems sort of inefficent though as you need to run a select on before every save. If someone has a better answer please post.