Yii 设置虚拟属性


Yii set Virtual Attributes

嗨,我有一个小问题,希望你能提供帮助。我正在我的模型中创建 4 个虚拟属性:

start_time_hrstart_time_mn

end_time_hrend_time_mn

使用以下方法:

/**
     * Retrieves hour portion of start_time
     * @return string
     */
    public function getstart_time_hr(){
        $str = explode(':', $this->start_time);
        return $str[0];
    }

这主要用于用户界面,因此用户可以通过一组下拉框设置开始和结束时间。 这有效 问题是将选择写回数据库。

我目前在我的模型中有这个:

public function setstart_time_hr($value){
        $this->start_time_hr = $value;
    }
public function setstart_time_mn($value){
        $this->start_time_mn = $value;
    }
public function beforeSave(){
        if(!empty($this->start_time_hr)){
            $this->start_time = $this->start_time_hr.':'.$this->start_time_mn;
        }
        return parent::beforeSave();
    }

我的保存表单操作是:

public function actionAdminChangeShift($calId){
          $model = CalShift::model()->findByPk($calId);
          $model->attributes = $_POST['CalShift'];
          $model->save();
          //$this->redirect(array('CalDialog','calID'=>$calId,'eventType'=>'click'));
      }

我还尝试在 set 函数中重建 start_time 变量,但这也没有奏效。我错过了什么?我是否必须从 from 手动组合变量并将其传递给模型?

$model->start_time = $_POST['CalShift']['start_time_hr'].':'.$_POST['CalShift']['start_time_mn'];

这不是一个大问题,但我宁愿这种组合作为正常保存功能的一部分在模型中发生。

旁注:有时时间以存储在 DB '01:30' 中的格式传递,而不是作为单独的值传递,因此它确实需要对此做出响应。

再次感谢。

编辑:

我能够通过将setstart_time_hr函数更改为:

public function setstart_time_hr($value){
        $str = explode(':', $this->start_time);
        $this->start_time = $value.':'.$str[1];
    }

但它将无法使用

$model->attributes = $_POST['CalShifts']

我必须通过执行以下操作手动分配值:

$model->start_time_hr = $_POST['CalShift']['start_time_hr'];
$model->start_time_mn = $_POST['CalShift']['start_time_mn'];

有谁知道让$model>属性工作的解决方法?

再次感谢

编辑#2

我无法回答我自己的问题,所以这是答案:

此问题的修复程序在模型中:

public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            .......
            array('notice_sent_date, start_time_hr, start_time_mn', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            ....
        );
    }
public function setstart_time_hr($value){
    $str = explode(':', $this->start_time);
    $this->start_time = $value.':'.$str[1];
}
public function setstart_time_mn($value){
    $str = explode(':', $this->start_time);
    $this->start_time = $str[0].':'.$value;
}

$model->attributes = $_POST['CalShifts']工作的关键是确保 2 个虚拟属性标记为安全。

希望这对其他人有所帮助。

对此的修复是,在模型中添加:

public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            .......
            array('notice_sent_date, start_time_hr, start_time_mn', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            ....
        );
    }
public function setstart_time_hr($value){
    $str = explode(':', $this->start_time);
    $this->start_time = $value.':'.$str[1];
}
public function setstart_time_mn($value){
    $str = explode(':', $this->start_time);
    $this->start_time = $str[0].':'.$value;
}

$model->attributes = $_POST['CalShifts']工作的关键是确保 2 个虚拟属性标记为安全。

希望这对其他人有所帮助。