Deep Insertion

61 views
Skip to first unread message

Sebastian Frohm

unread,
Mar 6, 2013, 4:20:49 PM3/6/13
to pha...@googlegroups.com
Hello,

I was wondering if someone could explain, a little bit more clearly, how deep inserts work in Phalcon. The documentation in the Storing Related Records section isn't great. I have gotten it to work, but, for some reason, it doesn't seem like the deep copy is actually doing what it is supposed to. Let's say you have this code:

<?php
    $user = new Users();

    $user->name = 'Sebastian';
    $user->zipcode = 60068;

    $email = new Email();
    
    $email->address = 'mye...@gmail.com';
    
    $user->email_id = $email; //I am assuming that Phalcon will be insert $email's ID generated by the database, once I call save on the $user object
    
    $user->save(); //results: Users table has null value in email_id column

Are my assumptions here incorrect? Am I just not understanding how the deep saves work? I was under the assumption Phalcon would populate the email_id field on the Users object with the value from the newly inserted row on the Email object.

If I am not being clear, please let me know.

Thanks!

Andres Gutierrez

unread,
Mar 6, 2013, 6:15:09 PM3/6/13
to pha...@googlegroups.com
Hi Sebastian,

You need to assign the alias of the relationship instead of the direct related entity:

$user->email_id = $email; //wrong
$user->email = $email; //ok

2013/3/6 Sebastian Frohm <sebasti...@gmail.com>

--
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.
To view this discussion on the web visit https://groups.google.com/d/msg/phalcon/-/UND0-ymK8HAJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Sebastian Frohm

unread,
Mar 7, 2013, 12:22:12 AM3/7/13
to pha...@googlegroups.com
Well, if email_id is a row on the DB and I'm not setting it directly, will Phalcon know to create the correct foreign key relation between the two tables?

I'm just not sure how it works; I was hoping to get an explanation :). Thanks, Andres.

Andres Gutierrez

unread,
Mar 7, 2013, 1:21:29 PM3/7/13
to pha...@googlegroups.com
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();
$email->email = 'and...@phalconphp.com';

$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


2013/3/6 Sebastian Frohm <sebasti...@gmail.com>

--
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.
To view this discussion on the web visit https://groups.google.com/d/msg/phalcon/-/HKtJGqnMWwkJ.
Reply all
Reply to author
Forward
0 new messages