On Thu, Aug 23, 2012 at 10:17 AM, kevin.ncbible <ke...@ncbible.com> 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.

Actually, it would be something more like
$childTopic['Passage']['ref_abbr']. Keep in mind that ref_abbr is a
property of the Passage model.

I've stopped naming my data variables after the models and instead
almost always call it $data. Aside from avoiding annoying things like,
$topic['Topic']['whatever'] it seems to help keep clear where fields
are and to which model they belong to.


> 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?

This is what I was wondering. I'm not sure, although I know I've done
something very similar to this before. I can't remember where,
however.

Try this:

public function getRelations($id)
{
        return $this->find(
                'first',
                array(
                        'conditions' => array(
                                $this->alias.'.id' => $id
                        )
                        'contain' => array(
                                'Passage',
                                'ParentTopic' => array(
                                        'Passage',
                                ),
                                'childTopic' => array(
                                        'Passage',
                                )
                        )
                )
        );                      
}

TopicsController:

$data = $this->Topic->getRelations($id);
// or just hard-code some Topic.id that exists
die(debug($data));


Cake may complain that parentTopic & childTopic are not associated
with Passage. You might try creating the associations in the Passage
model, although I'm not certain that would work because contain will
be looking at it from the parent/child models' point of view.

Or maybe it'll just work.

-- 
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+unsubscr...@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en-US.


Reply via email to