PhalconPHP Framework - How to update a model object and its associated object

I work with PhalconPHP and so far I have had few problems.

The frame is very good.

However, I ran into a problem that I could not figure out. This is probably a very simple thing, but I worked in circles and could not find links to this problem.

The problem is that I cannot update the model object if I also update the associated object.

For example, suppose I have a couple of model classes.

Contact:

class Contact extends \Phalcon\Mvc\Model { public function initialize() { $this->belongsTo('email_id', 'Email', 'id', ['alias' => 'Email']); } } 

And email:

 class Email extends \Phalcon\Mvc\Model { public function initialize() { $this->hasMany('id', 'Contact', 'email_id', ['alias' => 'Contacts']); } } 

When I try to create new Contact and Email , the following works:

 $contact = new Contact(); // ... assign other fields here (ie name) $email = new Email(); // ... assign other email fields here (ie address) $contact->setEmail($email); // or $contact->email = $email; $contact->save(); // record created 

However, when I try to update Contact and change the Email relationship, the information simply does not update:

 // Only update other fields $contact->name = 'Some other name'; $contact->save(); // This works // Update the related object $contact->name = 'A new name'; $contact->setEmail($anotherValidEmail); $contact->save(); // This doesn't work! Not even the name field is updated 

I tried using update() instead of save() .

I also tried using $contact->email = $newEmailObject , but I get the same results.

Has anyone encountered this issue? What am I doing wrong?

I am using Mac OS X , PhalconPHP 1.2.3

I also tested it on Windows 8, with PhalconPHP 1.1.0

Any help is appreciated.

updated

I posted the results of $ contact-> getMessages () after saving (), but I am not getting any results. As if save () was successful, however the SQL statement is not executed.

LAST UPDATE: Problem is replicated and workaround found!

We were able to reproduce the problem. We doing this:

 $contact = Contact::findFirst(123); if ($contact->email->emailaddress != $newaddress) { $email = new Email(); $email->emailaddress = $newaddress; $contact->email = $email; $contact->save(); } 

This does not work! As soon as we compare the emailaddress field with the email-related object, the line for saving the new address does not work.

However, if we change the code a bit and do this:

  $contact = Contact::findFirst(123); if ($contact->email->emailaddress != $newaddress) { $email = new Email(); $email->emailaddress = $newaddress; // Load the record (again!) $contact = Contact::findFirst(123); $contact->email = $email; $contact->save(); } 

It really works.

I assume that loading a linked object affects the ability to update this particular field and again find a contact object, clears the linked object.

So, we have a workaround that I hope will help other people who are faced with the same problem.

+6
source share
4 answers

When you use $contact->setEmail($anotherValidEmail); , you must have the set and get method in Contact Contact Contact Contact. For instance:

 Class Contact { public $email; public function setEmail($email) { } public function getEmail() { return $this->email } } 
+1
source

there is an answer in the official dosamena, you should try it (add the ACTION_CASCADE flag):

 <?php namespace Store\Models; use Phalcon\Mvc\Model, Phalcon\Mvc\Model\Relation; class Robots extends Model { public $id; public $name; public function initialize() { $this->hasMany('id', 'Store\\Models\Parts', 'robots_id', array( 'foreignKey' => array( 'action' => Relation::ACTION_CASCADE ) )); } } 

http://docs.phalconphp.com/en/latest/reference/models.html#cascade-restrict-actions

The above code is configured to delete all reference records (parts) if the master record (robot) is deleted.

You can also use the beforeDelete or afterDelete functions and add any required code:

http://phalcon-php-framework-documentation.readthedocs.org/en/latest/reference/models.html#deleting-records

all other available events: http://phalcon-php-framework-documentation.readthedocs.org/en/latest/reference/models.html#events-and-events-manager

0
source
 Actually, your model should look like as below class Contact extends \Phalcon\Mvc\Model { public $id; public $name; public $email; public function initialize() { $this->belongsTo('email_id', 'Email', 'id', ['alias' => 'Email']); } public function columnMap() { return array( 'id' => 'id', 'name' => 'name', 'email' => 'email' ); } } 

Then

$ contact = new Contact ();

$ success = $ contact-> save ($ this-> request-> getPost (), array ('name', 'email'));

0
source

I have the same problem and the only solution at the moment is to load the record again, as in the question.

-2
source

Source: https://habr.com/ru/post/952163/


All Articles