Yii Relation Parent

Hi,

When grabbing related records from a CActiveRecord is there a way to get a reference to the parent object from the children?

So:

parent -<—<-----<-

-> child____ ^

____-> parent -->-^

It might seem a little odd but when a child has multiple parents I need to know which one called it for some contextual information.

I understand I could do this by not using a relation but having a function and looping over them myself, but then I loose all the power of the relations and the niceties that they bring.

Thanks

Here’s the similar problem, maybe you’ll find something useful there:

http://www.yiiframework.com/forum/index.php/topic/19319-multi-parent-child-in-yii/

Post your DB schema for the tables. Thanks.

Hi, Thanks but I don’t mean referencing the parent in the database, I can get a list of any child’s parents without a problem.

When in an Active Record model I want to be able to get the parent ActiveRecord object that this was a relation of (if any).

This code for example would work for two parents with the same children, without backreferencing the database, just using an actual object reference:




$id = 4;

$parent = Thing::model()->findByPk($id);

foreach( $parent->children as $child )

{

	echo $child->relatedParent->id . ' -> ' .$child->id;

	

}

//echos 

// 4 -> 1

// 4 -> 2

// 4 -> 3




$id = 5;

$parent = Thing::model()->findByPk($id);

foreach( $parent->children as $child )

{

	echo $child->relatedParent->id . ' -> ' .$child->id;

	

}

//echos 

// 5 -> 1

// 5 -> 2

// 5 -> 3



Is that clear as mud?

Thaks

Was a solution to this ever adopted in Yii?

This can be very usefull! It is certainly much more efficient then [size=2]extra unneccessary querying through a forward only lazy loading of parent > child > parent [/size]

It seems we’ll have to start using Yii 2.0 !

See the section about Inverse Relations in http://www.yiiframework.com/news/77/yii-2-0-beta-is-released/