1
votes

Hi all im having a small issue and hoping you can help. Im am creating 4 virtual attributes in my model:

start_time_hr start_time_mn

end_time_hr end_time_mn

using the following:

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

This is mainly for a user interface so users can set start and end times via a set of drop down boxes. this works the issue is writing the selections back to the DB.

I currently have this in my model:

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();
    }

my save form action is:

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

      }

I also tried rebuilding the start_time variable in the set function but this also did not work. What am I missing? do I have to combine the variables manually from the from and pass that to the model?

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

That's not a huge issue but i would rather this combination take place in the model as part of the normal save function.

Side note: There may be time where the times are passed in the format that they are stored in the DB '01:30' and not as separate values so it does need to respond to that.

Thanks Again.

EDIT:

I was able to get this to work by changing the setstart_time_hr function to:

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

But it will not work using

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

I have to assign the values manually by doing the following:

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

Does anyone know of a workaround to get $model->attributes to work?

Thanks again

EDIT #2

I can't answer my own question so here is the answer:

The fix for this was in the model:

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;
}

The key to getting $model->attributes = $_POST['CalShifts'] Working was ensuring the 2 virtual attributes where marked as safe.

Hope this helps someone else..

1

1 Answers

3
votes

The fix for this was, in the model adding:

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;
}

The key to getting $model->attributes = $_POST['CalShifts'] Working was ensuring the 2 virtual attributes where marked as safe.

Hope this helps someone else..