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

Considering save(), create() and update() for database table updates

Hello guys!

I'm trying to make an update of many tables with its foreign keys, following the Object-Document Mapper. I don't really know which method will be "better" to use, or when I must to use the correct method.

save(), create(), update()

I would say update() because I want to update my database. But Invo and Vökuró projects use save() to update its database, as far as I know...

This is what I actually do:

$email        = $this->request->getPost('email', 'email');
$name        = $this->request->getPost('name');
// Some many more gets

$user = Users::findFirstById($id);
$user->email = $email;

$user_info               = new UsersInfo();
$user_info->name  = $name;
// Some many more relational mapping.

$res=$user->save();
var_dump($res); // it shows Bool(false)

So the tables never become changed. What I'm doing wrong. Which is the optimal method to proceed in this case?

Thanks for reading and please forgive my bad english ;-(

Save does create or update depening on current model status. You need to obviously set this $user_info to user. Right not you are creating not used anywhere $user_info variable.

If it shows false it means that saving failed. You need to check what errors you have with $user->getMessages()



13.8k
edited Dec '17

Yeah, $user->UsersInfo = $user; The code is long, I forgot to write it on this post, but I actually do the mapping.

Save does create or update depening on current model status. You need to obviously set this $user_info to user. Right not you are creating not used anywhere $user_info variable.

If it shows false it means that saving failed. You need to check what errors you have with $user->getMessages()



13.8k

This code:

$res=$user->save();
exit(var_dump($user->getMessages()));

Shows me a very huge object, which messages are relevant to find out?

Save does create or update depening on current model status. You need to obviously set this $user_info to user. Right not you are creating not used anywhere $user_info variable.

If it shows false it means that saving failed. You need to check what errors you have with $user->getMessages()



13.8k
edited Dec '17

Ah! I found it.

register_date is required

I don't want to update this field, how can I avoid it?

Stop using var_dump - use xdebug, it will save you a lot time.

Im not sure why you have it, you don't have it in this record? Then you should set that it can be null in database.



13.8k

Thanks for suggesting xdebug, I will take a look on it :-)

Obviously I really have a record to update, and its record have register_date which is NOT NULL, and this is actually setted. I don't want update this field, but all other, how can I exclude it?

What about if I want to excluse some other fields? In other words, if I want to update a specific number of field.



145.0k
Accepted
answer
edited Dec '17

But if you get Users::findFirstById($id); then value of register_date is already there right? Just don't set null value for register_date.

You need to figure out why you have added this message in first place.

Then i think you have something wrong with your metadata - just clean it.

There is skipAttributesOnUpdate but it only works in PHQL. It doesn't removes validation of field.



13.8k

Ah, I simply don't added that field, though Phalcon will skip it!

But if you get Users::findFirstById($id); then value of register_date is already there right? Just don't set null value for register_date.

You need to figure out why you have added this message in first place.

Then i think you have something wrong with your metadata - just clean it.

There is skipAttributesOnUpdate but it only works in PHQL. It doesn't removes validation of field.