Assuming you have these two models:
<?php
class User extends Phalcon\Mvc\Model
{
public $id;
public $name;
public $email_id;
public function initialize()
{
//The implicit alias of the relation is 'Email'
$this->belongsTo('email_id', 'Email', 'id');
}
}
<?php
class Email extends Phalcon\Mvc\Model
{
public $id;
public $email;
}
The model 'User' belongs to 'Email'. Every User has an Email related.
$email = new Email();
$user = new User();
$user->name = 'Andres';
//Instead of assigning the email_id we assign the virtual 'email' property which is the name of the relation to the entity 'Email'
$user->email = $email;
$user->save();
Internally, it knows that there is assigned an object into a relationship attribute (email) so according to the relationship: first, it saves the $email object (this saving fills the field 'id' on Email), then it takes the value of the field 'id' on Email and assign it to 'email_id' on User, finally saving $user.
After that, you can print $email->id and $user->email_id
You received this message because you are subscribed to the Google Groups "Phalcon PHP Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
phalcon+u...@googlegroups.com.
To post to this group, send email to
pha...@googlegroups.com.