I did a similar thing. Here's how:

        public $hasOne = array(
                'GalleryThumb' => array(
                        'className' => 'GalleryThumb',
                        'foreignKey' => 'gallery_id'
                )
        );
        
        public $hasMany = array(
                'Image' => array(
                        'order' => array('Image.sort_order' => 'ASC')
                )
        );
        // I'm using SortableBehavior for both Image and Gallery

        // called from index() action
        public function fetchAll()
        {
                return $this->find(
                        'all',
                        array(
                                'order' => array('Gallery.sort_order' => 'ASC'),
                                'contain' => array(
                                        'GalleryThumb' => array('Thumbnail'))
                        )
                );
        }

And I have an action that sets the Thumbnail to use for a given Gallery:

        public function admin_thumbnail($id = null, $thumbnail_id = null)
        {
                if (empty($this->data) && (!$id || !$thumbnail_id))
                {
                        $this->flash('Invalid request', array('action' => 
'index'));
                }
                
                $this->Gallery->setThumb($id, $thumbnail_id);
                
                $this->flash(
                        'thumbnail set',
                        array('action' => 'edit', 'id' => $id)
                );
        }

model:

        public function setThumb($id, $thumbnail_id)
        {
                $thumb = $this->GalleryThumb->find(
                        'first',
                        array(
                                'conditions' => array('gallery_id' => $id),
                                'recursive' => -1
                        )
                );
                
                $data = array(
                        'id' => $thumb['GalleryThumb']['id'],
                        'gallery_id' => $id,
                        'thumbnail_id' => $thumbnail_id
                );
                $this->GalleryThumb->save($data);
        }

It'll be more complicated if your GalleryCover thumbnails are a
different size than the regular Thumbnails though.

On Tue, Nov 6, 2012 at 9:36 AM, metford <[email protected]> wrote:
> 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 [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> Visit this group at http://groups.google.com/group/cake-php?hl=en.
>
>

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
Visit this group at http://groups.google.com/group/cake-php?hl=en.


Reply via email to