Delete on Cascade

I have 3 tables

CREATE TABLE [b]object[/b] (

idobject int(11) NOT NULL AUTO_INCREMENT,

idobject_parent int(11) DEFAULT NULL,

PRIMARY KEY (idobject),

KEY idobject_parent (idobject_parent),

CONSTRAINT idobject_parent

FOREIGN KEY (idobject_parent) REFERENCES object (idobject) ON DELETE NO ACTION ON UPDATE NO ACTION

) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=latin1

CREATE TABLE [b]document[/b] (

iddocument int(11) NOT NULL AUTO_INCREMENT,

idobject int(11) DEFAULT NULL,

title varchar(100) DEFAULT NULL,

PRIMARY KEY (iddocument),

KEY idobject (idobject),

CONSTRAINT idobject_document

FOREIGN KEY (idobject) REFERENCES object (idobject) ON DELETE CASCADE ON UPDATE NO ACTION

) ENGINE=InnoDB DEFAULT CHARSET=latin1

CREATE TABLE [b]p_document[/b] (

idp_document int(11) NOT NULL AUTO_INCREMENT,

idobject int(11) DEFAULT NULL,

idobject_document int(11) DEFAULT NULL,

PRIMARY KEY (idp_document),

KEY idobject_document (idobject_document),

KEY idobject (idobject),

CONSTRAINT idobject_document_p

FOREIGN KEY (idobject_document) REFERENCES object (idobject) ON DELETE CASCADE ON UPDATE NO ACTION,

CONSTRAINT idobject_pdocument

FOREIGN KEY (idobject) REFERENCES object (idobject) ON DELETE CASCADE ON UPDATE NO ACTION

) ENGINE=InnoDB DEFAULT CHARSET=latin1

and these are my sample records

document

iddocument = 1

idobject = 1

title = "test"

object

idobject = 1

idobject_parent = null

p_document

idp_document = 1

idobject = 2

idobject_document = 1

object

idobject = 2

idobject_parent = 1

these are the relations each document has an object and p_document and p_document has an object

I deleted the object of the document and automatically the object of the document and p_document are also deleted because of the ON DELETE CASCADE but object of the p_document remains.

I try to put these codes on PDocument Model

protected $_object;

protected function beforeDelete()

{

$this->_object = clone $this->idobject0;





return parent::beforeDelete();

}

protected function afterDelete()

{

$this->_object->delete();





return parent::afterDelete();

}

but unfortunately it doesnt work. I think when the table is oncascade it doesnt walk through models that’s why the codes above doesnt work. Am I right or Am I missing something?

Thanks

oncascade is a database setting… so as the database is deleting the child records… the Yii or any PHP code does not “know” that… that’s why your object is not deleted and beforeDelete/afterDelete is not called…