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

Er.KT
  • 2,852
  • 1
  • 36
  • 70

3 Answers3

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',
    )
);
Anil kumar
  • 4,107
  • 1
  • 21
  • 36
  • but there is no any direct relation between branch and deal – Er.KT Sep 06 '13 at 04:29
  • 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 records – Anil kumar Sep 06 '13 at 04:33
  • 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 ) ); ` – Anil kumar Sep 06 '13 at 04:37
  • Just try like this and view the output, it only deletes the records that are having associations like `hasMany` and `hasAndBelongsToMany` just check [here](http://book.cakephp.org/2.0/en/models/deleting-data.html#delete) – Anil kumar Sep 06 '13 at 04:44
  • see my update in answer, if you try like this i think it works fine. – Anil kumar Sep 06 '13 at 04:54
  • 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 Deal – Er.KT Sep 06 '13 at 05:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/36901/discussion-between-anil-kumar-and-er-kt) – Anil kumar Sep 06 '13 at 05:48
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.

GatorGuy023
  • 297
  • 2
  • 3
  • 11
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).

trainmaster
  • 145
  • 10