Php 清除属性的集合值

Php 清除属性的集合值,php,yii,Php,Yii,在我的web应用程序中,我需要在单击以更新该记录时清除一个特定属性或将其设置为Null。我不知道该怎么做。假设您有一个处理表单提交的控制器操作-这是通常情况-您的控制器中会有一个函数像这样: public function actionUpdate($id) { // Load model $Model = Model::model()->findByPk($id); // or maybe do it this way $Model = $this-&g

在我的web应用程序中,我需要在单击以更新该记录时清除一个特定属性或将其设置为Null。我不知道该怎么做。

假设您有一个处理表单提交的控制器操作-这是通常情况-您的控制器中会有一个函数像这样:

public function actionUpdate($id)
{
    // Load model
    $Model = Model::model()->findByPk($id);

    // or maybe do it this way
    $Model = $this->loadModel($id);

    // Check for form POST
    if(isset($_POST['Model']))
    {
         // Mass assignment of Model attributes to matching values in post array
         $Model->attributes = $_POST['Model'];

         if($Model->save())
         {
              // Do something, redirect etc
         }
    }

    $this->render('yourView');
}
因此,在这里,我们大量地将
$Model
属性分配给表单中POST数组中的匹配值。但完成后,您可以覆盖单个属性。例如:

    // Check for form POST
    if(isset($_POST['Model']))
    {
         // Mass assignment of Model attributes to matching values in post array
         $Model->attributes = $_POST['Model'];

         $Model->attribute_a = null;
         $Model->attribute_b = '';
         $Model->name = 'Anything you like';
         $Model->date = 'Anything you like';
         // ...etc

         if($Model->save())
         {
              // Do something, redirect etc
         }
    }

在模型中,在保存()之前创建函数

class Customer extends CActiveRecord
{
....
   public function beforeSave() {
      $this->last_order = null;
   }
....
}