Skip to content
This repository was archived by the owner on Nov 13, 2025. It is now read-only.

Gallery Usage

zuha edited this page Jul 5, 2012 · 2 revisions

Attaching a gallery to any model is pretty straight forward code.

In your add view (ex. app/Plugin/Tasks/View/Tasks/add.ctp)

Make sure your form is of the file type

echo $this->Form->create('Task', array('type' => 'file'));

Then add these inputs

echo $this->Form->input('GalleryImage.filename', array('type' => 'file', 'label' => 'Upload your best image for this item.', 'after' => ' <p> You can add additional images after you save.</p>'));
echo $this->Form->input('GalleryImage.dir', array('type' => 'hidden'));
echo $this->Form->input('GalleryImage.mimetype', array('type' => 'hidden'));
echo $this->Form->input('GalleryImage.filesize', array('type' => 'hidden'));

Now attach Gallery in the model (still using the Task Model as an example)

    public $hasOne = array(
        'Gallery' => array(
            'className' => 'Galleries.Gallery',
            'foreignKey' => 'foreign_key',
            'dependent' => false,
            'conditions' => array('Gallery.model' => 'Task'),
            'fields' => '',
            'order' => ''
            ),
        );

Now handle the Gallery save

    function afterSave() {
        $this->data['Gallery']['model'] = $this->alias;
        $this->data['Gallery']['foreign_key'] = $this->id;

        // check if GalleryImage data is in data 
        if (isset($this->data['GalleryImage'])){
            if ($this->data['GalleryImage']['filename']['error'] == 0 
                    && $this->Gallery->GalleryImage->add($this->data, 'filename')) {
                return true;
            } else {
                return false;
            }
        } 
    }

Clone this wiki locally