pouria
pouria

Reputation: 1008

Yii: Can't save data into newly added column

In my app, Comment model is rendered partially in Product view. Everything is alright except that after I added a new column to my comment table, I cannot save data into the new column (named 'ddate'). Even I tried this:

$_POST['Comment']['ddate'] = 'something';
$model2->attributes=$_POST['Comment'];
$model2->save();

but not working!

Upvotes: 0

Views: 169

Answers (2)

Dhanush Bala
Dhanush Bala

Reputation: 1132

$temp = $model->attributes;
$model->setFields($_POST['Comment'];);
$model->ddate  = $_POST['Comment']['ddate'];
if($model->save())
{
    echo "saved";
}

Upvotes: 1

Samuel Liew
Samuel Liew

Reputation: 79103

You have to set the attribute as 'safe' in the model, or the form value won't be copied to the model when $model2->attributes = $_POST['Comment']; is called.

Example:

public function rules() {
    return array(
        array('ddate', 'safe'),
    );
}

If you always want ddate to be the current date/time, it might be better to set it in beforeSave() instead:

protected function beforeSave() {

    if($this->hasAttribute('ddate') && !isset($this->ddate)) 
        $this->ddate = date("Y-m-d H:i:s");

Upvotes: 0

Related Questions