Friday, August 24, 2012

Re: Model with self association: how do I reference associated field from Child?

What I do with relationships like these is to use the TreeBehavior. It works pretty well; you can check it out here  http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html

On Thursday, 23 August 2012 10:17:32 UTC-4, kevin.ncbible wrote:
Thank you, so much, for taking the time to help. I do appreciate how quickly individuals here has shared their expertise.

I have a model, called "Topic", with a hasOne association to another model, called "Passage." In the Passage model is a field that I always need when calling up Topic data: this field is called "ref_abbr." I can get a view of "Topic" that includes this "Passage" field just fine, e.g.: $topic['Passage']['ref_abbr']

Now, here's the question: The "Topic" model also has a self association setup with $belongsTo ParentTopic and $hasMany ChildTopic. So, when I get a view of "Topic" it also shows its children listed at the bottom of the page. That works great, but I want that Passage ref_abbr field included in this list as well. So, how do I get a view of ChildTopic that also includes this Passage field, e.g. something like: $childTopic['ref_abbr'] -- but that does not actually work.

Is there more I need to do in setting up the association in the Topic model? Right now I have:

public $hasOne = array(
'Passage' => array(
'className' => 'Passage',
'foreignKey' => 'topic_id' ) );

public $belongsTo = array(
'ParentTopic' => array(
'className' => 'Topic',
'foreignKey' => 'parent_id' )  );

public $hasMany = array(
'ChildTopic' => array(
'className' => 'Topic',
'foreignKey' => 'parent_id',
'dependent' => false )  );

Do I need to manually specify a join for ChildTopic to the Passage model?

Thank you!
Kevin

--
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.
 
 

No comments: