How to save Phalcon model
lang_of_article_differ
want_proper_trans
When working with database through phalcon orm models there are few ways to save changes on models to database:
Lets imagine you have created new object of phalcon's model and made some changes on it:
$object = new \App\Product();
$object->title = 'new product';
save method
the most simple way to save it to database is to call save
method on it:
$object->save();
It returns true
on success write to database and false on some error. You can get list of errors caused the failure by invoking method getMessages()
:
$state = $object->save();
if (!$state) {
print_r($object->getMessages());
}
update and create method
the save
is a wrapper. It check if the object exists in database already and invokes update
or create
methods accordingly.
If you're confident that object exists or not in database you can directly call methods create
to save it to database or update
to alter information on selected object as well.