I would like to use Coupler behavior for the beforeDelete method and
the afterFind method, but finaly I don't use Coupler Behaviors
anymore, because i don't give a file in my form. Maybe that's why the
Model::exists() returns false, i don't know...
I modified the Mediafiles Model's beforeDelete method to delete all
files (Transfert and filters)
app/models/mediafile.php :
<pre>
class Mediafile extends AppModel{
var $name = "Mediafile";
//media plugin behaviors
var $actsAs = array(
'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 transfert and 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;
}
$fileT = MEDIA_TRANSFER ;
$fileT .= $result[$this->alias]['dirname'];
$fileT .= DS . $result[$this->alias]['basename'];
$FileT = new File($fileT);
if (!$FileT->delete()) {
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;
}
}
</pre>
Thanks
BenJsno
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