In fact, i don't need to use make method in my add fonction.
I just saw it enough to have dirname and basename for Generator
Behavior makes the versions.
Anyway, i think the real problem is why $Model->exists() return false
in BeforeSave method in Coupler Behavior
Is it normal? Am I missing something
my new app/model/mediafile.php :
class Mediafile extends AppModel{
var $name = "Mediafile";
//media plugin behaviors
var $actsAs = array(
'Media.Coupler' => array(
'baseDirectory' => MEDIA_TRANSFER,
'filterDirectory' => MEDIA_FILTER
),
'Media.Generator' => array(
'baseDirectory' => MEDIA_TRANSFER,
'filterDirectory' => MEDIA_FILTER,
'createDirectory' => true,
'overwrite' => true
),
'Media.Polymorphic'
);
//file validation which only allowed jpeg and png to be uploaded
var $validate = array(
'file' => array(
'mimeType' => array(
'rule' => array('checkMimeType', false, array(
'image/jpeg',
'image/png'))
)
)
);
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'foreign_key',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Homeslide' => array(
'className' => 'Homeslide',
'foreignKey' => 'foreign_key',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
// delete filters files
public function beforeDelete($cascade = true) {
if (!$cascade) {
return true;
}
$result = $this->find('first', array(
'conditions' => array($this->primaryKey => $this->id),
'fields' => array('dirname', 'basename'),
'recursive' => -1
));
if (empty($result)) {
return false;
}
$pattern = MEDIA_FILTER . "*/";
$pattern .= $result[$this->alias]['dirname'] . '/';
$pattern .= pathinfo($result[$this->alias]['basename'],
PATHINFO_FILENAME);
$files = glob("{$pattern}.*");
$name = Mime_Type::guessName($result[$this->alias]
['basename']);
$versions = array_keys(Configure::read('Media.filter.' .
$name));
if (count($files) > count($versions)) {
$message = 'MediaFile::beforeDelete - ';
$message .= "Pattern `{$pattern}` matched more than number
of versions. ";
$message .= "Failing deletion of versions and record for
`Media@{$this->id}`.";
CakeLog::write('warning', $message);
return false;
}
foreach ($files as $file) {
$File = new File($file);
if (!$File->delete()) {
return false;
}
}
return true;
}
}
Thanks for your help
On 5 avr, 10:59, David Persson <[email protected]> wrote:
> Hi,
>
> The coupler behavior's beforeSave method looks like it is doing too much
> having too many sideeffects. I'll take a look at it and see if I can reduce
> those to a mininum. Will get back and post the results than.
>
> - David
--
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
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php