Saturday, 15 January 2011

yii - cascade delete the child record of the table -


I have a table id with name and parent

Relationship function in the model:

  'location_parent' = & gt; Array (auto :: BELONGS_TO, 'location', 'parent'), 'location_button' = & gt; Arrays (auto :: HAS_MANY, 'location', 'parent', 'order' => 'ID ASC'),   

Remove action in controller:

  Delete public function action ($ ID) {$ this- & gt; LoadModel ($ id) - & gt; Remove (); // If AJAX request (Administrator has been removed via grid view), then we should not redirect the browser if (! ($ _GET ['ajax'])) $ ($ ($ ($ _ POST) ['ReturnUrl']) $ $ _POST ['returnUrl']: array ('administrator')); }   

requirement:

Here, if I delete the record with id = 1 , then the line parentid = 1 must also be deleted

All the hair records are already in your model To delete first delete method before removing the parent

  before public function () (foreach ($ this-> location_children $ c) $ C-> Delete (); Return Parent: First Deloitte ();}   

To ensure Be sure to wrap calls to delete the initial new transactions that all records are deleted.

You can only use CDbCommand are

No comments:

Post a Comment