Hey guys,

I was working with the generic upload behavior, but any kind of
polymorfic association is subject to the same threat.

Basically I have the following in my, for example, news model:
-------------------------------------------------------------------------------------------------------------------------------------
var $hasMany = array(
                'Attachment' => array(
                                          'foreignKey' => 'foreign_id',
                                          'conditions' => 
array('Attachment.class' =>
'News', 'Attachment.mimetype'=>'LIKE image/%'),
                                          'order' => 'Attachment.order ASC',
                                          'dependent' => true
                                      ),

        );
------------------------------------------------------------------------------------------------------------------------------------


The cake7libs/model/model.php del method is as follows:
------------------------------------------------------------------------------------------------------------------------------------
function _deleteDependent($id, $cascade) {
                if (!empty($this->__backAssociation)) {
                        $savedAssociatons = $this->__backAssociation;
                        $this->__backAssociation = array();
                }
                foreach (array_merge($this->hasMany, $this->hasOne) as $assoc =>
$data) {
                        if ($data['dependent'] === true && $cascade === true) {

                                $model =& $this->{$assoc};
                                $field = 
$model->escapeField($data['foreignKey']);
                                $model->recursive = -1;

                                if (isset($data['exclusive']) && 
$data['exclusive']) {
                                        $model->deleteAll(array($field => $id));
                                } else {
                                        $records = $model->find('all', 
array('conditions' => array($field
=> $id), 'fields' => $model->primaryKey));

                                        if (!empty($records)) {
                                                foreach ($records as $record) {
                                                        
$model->delete($record[$model->alias][$model->primaryKey]);
                                                }
                                        }
                                }
                        }
                }
                if (isset($savedAssociatons)) {
                        $this->__backAssociation = $savedAssociatons;
                }
        }
-----------------------------------------------------------------------------------------------------------------------------------------------------

As you might notice when you delete a record it only looks for the
associated foreign key, and not for the further conditions specified
in the hasMany association.

Therefore I suggest an array_merge ==> am() of the conditions in the
findAll as follows:
You can do it in your app_model.php

----------------------------------------------------------------------------------------------------------------------------------
function _deleteDependent($id, $cascade) {
                if (!empty($this->__backAssociation)) {
                        $savedAssociatons = $this->__backAssociation;
                        $this->__backAssociation = array();
                }
                foreach (array_merge($this->hasMany, $this->hasOne) as $assoc =>
$data) {
                        if ($data['dependent'] === true && $cascade === true) {

                                $model =& $this->{$assoc};
                                $field = 
$model->escapeField($data['foreignKey']);
                                $model->recursive = -1;

                                //se ho condizioni su dipendenza
                                if(isset($data['conditions']) && 
!empty($data['conditions']) &&
$data['conditions']!=''){
                                    $conditions = am($data['conditions'], 
array($field => $id));
                                }
                                else{
                                    $conditions = array($field => $id);
                                }

                                if (isset($data['exclusive']) && 
$data['exclusive']) {
                                        $model->deleteAll($conditions);
                                } else {
                                        $records = $model->find('all', 
array('conditions' => $conditions,
'fields' => $model->primaryKey));

                                        if (!empty($records)) {
                                                foreach ($records as $record) {
                                                        
$model->delete($record[$model->alias][$model->primaryKey]);
                                                }
                                        }
                                }
                        }
                }
                if (isset($savedAssociatons)) {
                        $this->__backAssociation = $savedAssociatons;
                }
        }
-----------------------------------------------------------------------------------------------------------------------------------------------------

what do you risk without the array_merge ?!?!

Well let's suppose that you have a news with foreign id = 5, but you
also have a product with foreign id = 5

when you will delete the news #5, not only the News#5 related
attachments/images will be deleted, but also all related images to
Product #5

Hope this helps!

Dan

PS: please correct me if I did something wrong!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to