1

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

start_time_hr start_time_mn

end_time_hr end_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->attributes 工作的解决方法?

再次感谢

编辑#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 个虚拟属性。

希望这对其他人有帮助..

4

1 回答 1

3

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

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 个虚拟属性。

希望这对其他人有帮助..

于 2012-04-26T00:44:15.820 回答