vnnotech
vnnotech

Reputation: 35

cakephp How to Deleting data of two tables

M new in cakephp. M using cakephp 2X. I have model Student and Document.

Student hasMany Document

Document belognsTo Student

I need to delete documents when i am delete student. That student's all documents also been delete.

Upvotes: 0

Views: 689

Answers (1)

Debasis
Debasis

Reputation: 201

Try This :

In Model :

// In your Student Model
var $hasMany=array('Student'=>array('className'=>'Student',
                                 'foreignKey'=>'student_id',
                                 'dependent'=>true, // true without single quote
                                 'exclusive'=>true
                                )
                );

//In your Document Model
var $belongsTo = array('User'=>array('className'=>'Student',
                                     'foreignKey'=>'student_id'
                             )
                );

In Controller :

$this->Model->delete($item_to_delete_id,true);

Ref link

Upvotes: 2

Related Questions