Delete two tables data using fk

Hello,

I want to delete data which is related to another table. If click delete link then will be deleted both data ( suppose I have two tables : A and B, B has foreign key to A. If I click one data in A table then will be deleted B data too. Is that make sense) . Can any one help me?

Please show some code.

Here is my delete controller

public function actionDelete()

{


	if(Yii::app()->request->isPostRequest)


	{


		// we only allow deletion via POST request


		$this->loadA()->delete();


		$this->loadB()->delete();


		$this->redirect(array('list'));


	}


	else


		throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');


}

Here is my delete link in view file: If click "Delete" link then will be delete data from table A and table B if model->id == model2->a_id

<?php echo CHtml::linkButton(‘Delete’,array(

  	  'submit'=&gt;'',


  	  'params'=&gt;array('command'=&gt;'delete','id'=&gt;&#036;model-&gt;id),


  	  'confirm'=&gt;&quot;Are you sure to delete Name # {&#036;model-&gt;name}?&quot;)); 

?>

This is delete only table A data, but i need to delete table A and table B data if id and a_id will same.

I have no idea where I add table B information

Is that make sense?

Do I need to change model file?

Sorry, Here is my model files

public function relations()

{

return array(

‘b’ => array(self::BELONGS_TO, ‘B’, ‘a_id’),

}

use http://www.yiiframework.com/doc/api/1.0.11/CActiveRecord#deleteAll

maybe something like





public function actionDelete()

{

if(Yii::app()->request->isPostRequest)

{

// we only allow deletion via POST request


B::model()->deleteAll('a_id=:id',array(':id'=>$_GET[id]);

$this->loadA()->delete();


$this->redirect(array('list'));

}

else

throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');

}




THis is working only one table(table A). I need to delete both table data if a_id and b_id will match.

tabla A and table B

They are only examples?

the code




public function relations()

{

return array(

'b' => array(self::BELONGS_TO, 'B', 'a_id'),


}



is bad, The code should be




public function relations()

{

return array(

'b' => array(self::BELONGS_TO, 'A', 'a_id'), //change B for A


}



and

with "b_id" you mean "id" of table A?

and sorry for my code

$_GET[id] should be $_GET[‘id’]

Hey, It’s working fine. Thanks you very much.