How to/Can you retrieve the related AR instances when eager loading?

I am currently playing around with UserIdentity and have come across a snag…

How do you retrieve the value of a relation table when using the eager loading approach?

Lets say I have something like this which executes with no errors;

$user=user::model()->with('profile.contact_info')->findByAttributes(array('email'=>$this->email));

The table contact_info holds the value of the users 'screen_name' which I want to pass to the UserIdentity 'username'.

So I was thinking I would just be able to do something like this;

$this->username=$user->contact_info->screen_name;

Which fails, could someone please explain what I must do here.

Thanks

I'm working on this as well.

Let me see, if I can help you and help myself at the same time :)

print_($user) // You can look into the array to see if the data has been retrieved from DB > ActiveRecord.

I think this should do the trick… let me know.

$username = $user[0]->profile[0]->contact_info[0]->screen_name

Hi valeron,

thanks for your input. Yeah hopefully we can nut this out.

where abouts do i put the print_($user)? so i can check data. By using the log I can see that the sql performed by AR and that tells me that the data is definately being selected. I just don't know how to call on the value correctly

http://www.yiiframew…0.html#msg12464

You can check this reply from Tommy too, I got mine working now.

For each [0]s, you'll need to treat it as an array, and do a foreach to retrieve the object out, once it's out you can access that object's value.

you can print_r($user) anywhere you like after $user is populated, just to take a peek into the array. From there, you can work out how the data is structured inside the array, and you'll end up with what I did just now.

In your case, I would say

  1. $user contains an array of users or 1 user (I would call it $users)

  2. A foreach ($users as $user) would retrieve the User object from the $users array

  3. Inside the User object($user), profile object can be accessed $user->profile[X]

  4. You'll need to do a for each ($user->profiles) as $profile as well. This will retrieve the Profile Object // profile table.

  5. Now to get to contact_info table, repeat again, foreach($profile->contact_infos as $contact_info)

6)Once you've arrived at the contact_info object, you can $contact_info->screen_name

Maybe someone else can explain better than me…

http://www.yiiframew…ord#with-detail should help

I am very new and still learning php and yii so its proving difficult for something i would have thought should be a simple task.

It seems to hard to retrieve data from a related AR query!!! All I want to do is assign the value of the 'screen_name' in the table 'contact_info' to $this->username in UserIdentity.

When i action the query and test I can see the 'screen_name' value is in column t2_c5. I just want to retrieve it!!!

??? ??? ???

You should use $user->profile->contact_info->screen_name

Note that you are eager loading 'profile.contact_info' which means contact_info is related to user through profile.

Thanks qiang…

As soon as i seen your response i knew it!!! ha ha

I must admit I am really impressed with this community