Monday, April 4, 2011

tiny question on Media plugin and CouplerBehavior

Hello,
I would like to use the Media plugin in my application and my question
is

in media/models/behaviors/coupler.php, what is the goal of the line
104 ? is it usefull for something ?
line 104 : unset($Model->data[$Model->alias]);

In my exemple, I have a table Mediafiles
CREATE TABLE `mediafiles` (
`id` int(11) unsigned NOT NULL auto_increment,
`dirname` varchar(100) collate utf8_unicode_ci NOT NULL,
`basename` varchar(60) collate utf8_unicode_ci NOT NULL,
`model` varchar(20) collate utf8_unicode_ci NOT NULL,
`foreign_key` int(11) unsigned NOT NULL,
`size` int(11) NOT NULL,
`width` int(10) unsigned NOT NULL,
`height` int(10) unsigned NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
AUTO_INCREMENT=11 ;

I use uploadify for uploading my file with progressbar and Media
plugin to add data in DB and make the thumb with Generator behavior.

models/mediafile.php :
class Mediafile extends AppModel{
var $name = "Mediafile";

//media plugin behaviors
var $actsAs = array(
'Media.Coupler',
'Media.Generator'
);

//file validation which only allowed jpeg and png to be uploaded
var $validate = array(
'fileData' => array(
'mimeType' => array(
'rule' => array('checkMimeType', false, array( 'image/jpeg',
'image/png'))
)
)
);

var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'mediafile_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Homeslide' => array(
'className' => 'Homeslide',
'foreignKey' => 'mediafile_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}

controllers/mediafiles_controller.php :
class MediafilesController extends AppController {
var $name = 'Mediafiles';
var $components = array('RequestHandler', 'Cuploadify.cuploadify');

function upload_img() {
$this->cuploadify->upload();
}

function add() {
$this->layout = "admin";
if (!empty($this->data)) {

$this->data['Mediafile']['dirname'] = 'img';
// Thumb creation with Media Plugin
$this->Mediafile->make($this->data['Mediafile']['dirname'] . DS .
$this->data['Mediafile']['basename']);

// save in DB
if ($this->Mediafile->save($this->data)) {
$result = '<div id="output">success</div>';
$result .= '<div id="message">mediafile_id = '.$this->Mediafile-
>id.'</div>';
}else{
$result = "<div id='output'>failed</div>";
$result .= '<div id="message">'. $this->Mediafile-
>validationErrors['file'] .'</div>';
}
$this->set(compact('result'));
}
}
}

I use a component for uploadify
controllers/components/cuploadify.php
So I upload the file in ajax (upload_img()) and then I submit the form
with data but without file input (add()).

In add function The generator part works fine.
But when I save the data, the Coupler BeforeSave function don't find
$Model [in media/models/behaviors/coupler.php line 102] ( I don't
understand why)
and then don't find file input (because there is no file input, only
hidden input)
then the $Model->data[$Model->alias] is unset [in media/models/
behaviors/coupler.php line 104]
and return true but don't save data in DB.
if I comment the line 104, the data are saved.

I would really appreciate a clarification on this point.

Thank you

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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate