I have two related models i.e. Candidate and Qualifications. They have one to many relationship between them. I am using CActiveForm and want to perform CRUD operation on the relational data. As you can see from the code below that PK of candidate is auto generated and is being send to qualification model as FK.
Controller
public function actionCreate()
{
$model=new Candidate;
$q=new Qualification;
if (isset($_POST['Candidate'], $_POST['Qualification'])) {
$model->attributes=$_POST['Candidate'];
$q->attributes=$_POST['Qualification'];
$error = false;
$transaction = Yii::app()->db->beginTransaction();
try {
if (!$model->save()) {
throw new CException(CHtml::errorSummary($model));
}
$q->candidate_id = $model->id;
if (!$q->save()) {
throw new CException(CHtml::errorSummary($q));
echo $error;
}
$transaction->commit();
} catch (Exception $e) {
$transaction->rollBack();
$error = $e->getMessage();
}
if (!$error) {
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
'qualification'=>$q,
));
}
This is working fine for me but now i want to perform update and delete. I am finding hard to implement it. Moreover, i don’t find much of help regarding relational update and delete other than by using extensions.
From the code you posted I assume you are creating/updating/deleting 1 candidate + 1 qualification at a time (even though the relation is 1 -> n) ?
For a modify, your $_POST should contain the id’s of the candidate & qualification.
So you will have to load the candidate & qualification (findByPk), instead of creating a new ones. The rest of the code looks similar to what you have already.
For delete, you probably want 2 seperate delete actions: 1 for a candidate (also deleting qualifications) and 1 for a qualification ?
Just get the id of the model, load it, then call the delete method.
(you might want to reconsider your approach of sending data of 2 models at once when there is a 1->n relation between them)
public function actionUpdate($id)
{
...
if (!$error) {
$this->redirect(array('view','id'=>$model->id));
}
}
--> You are not rendering anything here <--
}
In case you didn’t get it, it’s about case sensitivity, make sure you access the data in your view by exact same name you sent it from your controller.