Delete multi level in cakephp ( chain delete )
Asked Answered
N

3

7

I want to delete all dependent rable record

My association

Branch Model

var $hasMany =array(
    'Dealbranch' => array(
        'className' => 'Dealbranch',
        'foreignKey' => 'DLB_BR_ID',
        'dependent' =>true
    )
 );

Dealbranch Model

var $belongsTo = array(
    'Deal' => array(
        'className' => 'Deal',
        'foreignKey' => 'DL_ID',
        'dependent' => true
    ),
    'Branch' => array(
        'className' => 'Branch',
        'foreignKey' => 'DLB_BR_ID',
    )
);

Deal Model

var $hasMany = array(
    'Dealbranch' => array(
        'className' => 'Dealbranch',
        'foreignKey' => 'DLB_DL_ID',
    )
);

In controller I have used

$this->Branch->delete($id,true);

Now here whem I am deleting branch , so dependent dealbranch deleted successfully but none of any deal record deleted

I want like : whem I am deleting branch , so all dependent dealbranch should be deleted and all dependent( depend on dealbranch) deal record should be deleted

here Deal is child of Dealbranch and Dealbranch is child of branch

Now, For one branch there are multiple record in Dealbranch, and for multiple Dealbranch there is one record in Deal

enter image description here

Please help me. I am using cakephp 2

Nagpur answered 5/9, 2013 at 11:11 Comment(0)
C
0

All the model records that are associated with Branch can be deleted by using

$this->Branch->delete($id,true);

I think if you want to remove the deal records on deleting of branch records then your model Branch should be associated with Deal model.

Just try to add like this

Branch Model

var $hasMany =array(
    'Dealbranch' => array(
        'className' => 'Dealbranch',
        'foreignKey' => 'DLB_BR_ID',
        'dependent' =>true
    ),
    'Deal' => array(
        'className' => 'Deal',
        'foreignKey' => 'your_foriegn_key',
        'dependent' =>true
    )

 );

or you may try like this

Update:

According to your requirement here Deal is child of Dealbranch and Dealbranch is child of branch your model associations should be like this.

deal branch model should be like this.

DealBranch Model

var $belongsTo = array(
    'Branch' => array(
        'className' => 'Branch',
        'foreignKey' => 'DLB_BR_ID',
    )
);
var $hasMany = array(
    'Deal' => array(
        'className' => 'Deal',
        'foreignKey' => 'DL_ID',
        'dependent' => true
    ),
);

deal model should be like this.

Deal Model

var $belongsTo = array(
    'Dealbranch' => array(
        'className' => 'Dealbranch',
        'foreignKey' => 'DLB_DL_ID',
    )
);
Chivaree answered 5/9, 2013 at 13:48 Comment(7)
but there is no any direct relation between branch and dealNagpur
There must be a relation In order to delete Deal on deleting of Branch, otherwise you need to write code manually to delete the deal recordsChivaree
And also one more thing here, Your Deal model is child of DealBranch and you've written Deal in belongsTo, it should be hasMany like this var $belongsTo = array( 'Branch' => array( 'className' => 'Branch', 'foreignKey' => 'DLB_BR_ID', ) ); var $hasMany = array( 'Deal' => array( 'className' => 'Deal', 'foreignKey' => 'DL_ID', 'dependent' => true ) ); Chivaree
Just try like this and view the output, it only deletes the records that are having associations like hasMany and hasAndBelongsToMany just check hereChivaree
see my update in answer, if you try like this i think it works fine.Chivaree
Thanks a lot for your effort @Anil,Now see my updated question , here For one branch there are multiple record in Dealbranch, and for multiple Dealbranch there is one record in DealNagpur
let us continue this discussion in chatChivaree
S
0

While I know the people at cakephp do not use foreign keys in their databases and rely on the framework to take care of these details, in your case if the framework is giving you problems and you have control over the database update your foreign key constraints to CASCADE on DELETE DB operations.

Sanatory answered 24/6, 2014 at 7:53 Comment(0)
S
0

The basic problem is in your data model. According to your model, any branch can be connected to zero or more deals and vice versa. Thus if you delete a branch, it does not make any sense to delete any deal, because the deal could be related to another branch. If the deal was deleted, the database integrity would be corrupted.

The setting you use is often used to model M:N relationship.1

The whole picture if your data model:

original model

And what you really meant:

new model

Branch model

var $hasMany =array(
'Deal' => array(
    'className' => 'Deal',
    'foreignKey' => <set_according_to_your_column_names>,
    'dependent' =>true
   )
);

Deal model

var $hasMany = array(
'Branch' => array(
    'className' => 'Dealbranch',
    'foreignKey' => <set_according_to_your_column_names>,
  )
);

This way, you can in the controller call only:

$this->Branch->delete($id,true);

or even

$this->Branch->delete($id);

since the second parameter is true by default.


1 Example of M:N relationship is Person - Address. Anyone can use more than one address (e.g. home and work) and one address can be shared by more people (family, colleagues).

Sherylsheryle answered 20/9, 2016 at 20:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.