$model->foo和FindbyPK - yii的区别


difference between $model->foo and FindbyPK - yii

我试图了解在模型中调用变量和试图在数据表中找到特定数据集之间的差异。在actionCreate中输入

public function actionCreate($id)
{
    $model=new Recipient;
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);
    if(isset($_POST['Recipient']))
    {
        $model->attributes=$_POST['Recipient'];
        if($model->save())
        {
            Recipient::model()->updateListId($id, $model);
            $this->redirect(array('view','id'=>$model->id));
        }
    }
    $this->render('create',array(
        'model'=>$model,
        'id'=>$id
    ));
}

在我的模型中我尝试过这样做

public function updateListId($id, $model)
{
    $model->list_id = $id;
    echo $id;
}

为什么我的模型没有这样更新?我应该使用findByPK吗?


更新

当我使用

public function updateListId($list_id, $model)
{
    $id = $model->id;
    $model->updateByPk($id,array('list_id'=>$list_id));
}

则更新。有人能解释一下这里发生了什么吗?

既然您有$model = new Recipient,您可以使用

 $model->id = $id;
 $model->save(); // Here you can pass array to save method to save only specified columns

代替

Recipient::model()->updateListId($id, $model)

或者如果你想用模型函数来做:

// controller:
$model->updateListId = $id;
$model->save();
// model Recipient
public function setUpdateListId($id) {
    $this->id = $id;
}