Wednesday, April 27, 2011

David Persson Media Plugin

More than one of my apps are doing this so I am very confused. I am
using Show hasMany Attachment. I try and create a new record and it
uploads and resizes the image and saves the record to my Attachment
model without a foreign_key but with the correct model (Show).

However, it does NOT save the main record in my Show model. The flash
says the record has been saved but it hasn't. If I EDIT a preexisting
record, the image is uploaded, resized and saved to the Attachment db
with the foreign_key.

Any help would be greatly appreciated. I would really like to be able
to use this plugin as it looks amazing but can't figure this one
hurdle out.

Thank you.

show.php


var $actsAs = array(
'Sluggable' => array('overwrite' => 'true'),
'Tags.Taggable',
'Media.Transfer',
'Media.Generator',
'Media.Coupler'
);

var $hasMany = array(
'Attachment' => array(
'className' => 'Media.Attachment',
'foreignKey' => 'foreign_key',
'dependent' => true,
'conditions' => array('Attachment.model' => 'Show')
),
);

shows_controller.php


function admin_add() {
if (!empty($this->data)) {
$this->Show->create();
if ($this->Show->saveAll($this->data, array('validate' =>
'first'))) {
$this->Session->setFlash(__('The show has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The show could not be saved. Please,
try again.', true));
}
}
$users = $this->Show->User->find('list', array(
'conditions' => array('User.group_id' => array('1', '3')),
'fields' => array('User.username'),
'order' => array('User.username' => 'asc')
)
);
$this->set(compact('users'));
}

function admin_edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid show', true));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
if ($this->Show->saveAll($this->data, array('validate' =>
'first'))) {
$this->Session->setFlash(__('The show has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The show could not be saved. Please,
try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->Show->read(null, $id);
}
$users = $this->Show->User->find('list', array(
'conditions' => array('User.group_id' => array('1', '3')),
'fields' => array('User.username'),
'order' => array('User.username' => 'asc')
)
);
$this->set(compact('users'));
}

admin_add.ctp

<?php echo $this->Form->create('Show', array('enctype' => 'multipart/
form-data'));?>
<fieldset>
<legend><?php __('Admin Add Show'); ?></legend>
<?php
echo $this->Form->input('user_id', array('empty' => true));
echo $this->Form->input('title');
echo $this->Form->input('description');
echo $this->element('attachments', array('plugin' => 'media'));
echo $this->Form->input('tags', array('type' => 'text'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: