Friday, August 24, 2012

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

On Fri, Aug 24, 2012 at 6:14 PM, kevin.ncbible <kevin@ncbible.com> wrote:
Thank you cricket.

From what you've shared, I've been learning a lot about the Containable behavior, including limiting the fields displayed. That is very helpful. I was using:

'contain' => array(
  'Passage' => array(
    'fields' => array(
      'Passage.id',
      'Passage.ref_abbr')
    ),
  'ParentTopic' => array(
    'Passage'
    ),
   'ChildTopic' => array(
    'Passage'
   )
)

I still cannot get it to work with ChildTopic -- you're right that it does not recognize it as an associated model (though it does not give that error re: ParentTopic). What exactly is 'ParentTopic' => array('Passage') "saying in SQL" -- something like "make topics have a LEFT JOIN to ParentTopic and a LEFT JOIN to Passage"? Even though I'm getting a SQL errror, I can see that it is trying to do something like that, but ChildTopic is not even included in the SQL error.

I'm not sure I follow. I think you'll have to post the error.
 
One question, before I go too far down the wrong track: what are doing when you wrote, above,  $this->alias.'.id' => $id ? When I include that, die(debug($data)); returns null. So, I tried, instead, $this->id => $id -- which returns a SQL error. I obviously do not understand something about use of alias.

It's just a string. For the Topic model, $this->alias is 'Topic', same as the class. But the same class has aliases ChildTopic & parentTopic.
When writing code inside the model it can be useful to not hard-code the class name. Here it's just specifying the string that will be sent to the database.

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