Php 在Yii中更新和删除相关模型(关系表)

Php 在Yii中更新和删除相关模型(关系表),php,mysql,activerecord,yii,Php,Mysql,Activerecord,Yii,已更新 我有两个相关模型,即候选人模型和资格模型。他们之间有一对一的关系。我正在使用CActiveForm,希望对关系数据执行CRUD操作。我已经能够插入数据,但我在更新和删除时遇到问题。函数正在显示$id,但这是页面上显示的唯一内容。 候选财务总监 public function actionUpdate($id) { echo $id;// this is printing the id on page $model = Candidate::model()->find

已更新
我有两个相关模型,即候选人模型和资格模型。他们之间有一对一的关系。我正在使用CActiveForm,希望对关系数据执行CRUD操作。我已经能够插入数据,但我在更新和删除时遇到问题。函数正在显示$id,但这是页面上显示的唯一内容。
候选财务总监

public function actionUpdate($id)
{
    echo $id;// this is printing the id on page
    $model = Candidate::model()->findByPk($id);
    $q =  Qualification::model()->findAllByAttributes(array('candidate_id' => $id));
    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));
        }
    }
}



 public function actionView($id)
    {
        /*$this->render('view',array(
            'model'=>$this->loadModel($id),

        ));*/
        $model = $this->loadModel($id);
        $this->render('view',array(
            'Candidate'=>$model,
            'Qualification'=>Qualification::model()->findAllByAttributes(array('candidate_id' => $id))
    ));
    }

我无法理解如何使用编辑选项使数据在表单中可见。

更新

看起来,您只有一对一的关系,因此如果是这种情况,您只需将
$q
链接到特定资格:

public function actionUpdate()
   {
    //load model
    $q=&$model->qualifications[0];
    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));
            }
            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));
        }
    }
通过一些调整,例如循环和上面的代码可以用于一对多

删除

对于删除,请编辑
Candidate::beforeDelete()
删除链接到它的所有资格,如下所示:

public function beforeDelete(){
    foreach($this->qualifications as $q)
        $q->delete();
    return parent::beforeDelete();
}

您应该在事务中包装对
Candidate::delete()
的调用。

确实,您是对的,我有一对一的关系,但您编写的代码不适合我。它正在打印id,但没有显示任何其他内容。您可以为您的
候选者
模型添加relations()定义吗?