Tuesday, November 6, 2012

multilevel association's and hasOne confusion..

Models:

Gallery hasMany Image
Image belongsTo Gallery

standard stuff.

I am attempting to automatically create a "GalleryCover" for easy access when outputting each gallery. So I did this:

// gallery model
public $hasOne = array(
'GalleryCover' =>array(
'className' => 'Image',
'foreignKey' => 'gallery_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'limit' => '1',
),
);

which makes sense to me.

I had then hoped to do:

foreach($galleries as $gallery) {
    echo $gallery['GalleryCover']['image']; // the "cover" image for this gallery.
}

however Cake is outputting each gallery twice; presumably of the other assocations, so obviously something isn't right.
I imagine this is just failing to understand hasOne and multiple associations properly.

Some guidance would be appreciated, and if there's a better way to achieve this please point me in the right direction. 

Thank you


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
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.
 
 

No comments: