0

I have this very weird problem in yii2,

public function actionUpdate($draftId) {
    $model = $this->findModel($draftId);
    // $model->product_title = \filter_var(\trim($_POST['product_title']), \FILTER_SANITIZE_STRING,\FILTER_FLAG_NO_ENCODE_QUOTES);
    $model->product_title;
    $model->updated_at = \time();
    $model->unit_price = "6666";
    //echo $model->updated_at;
    //exit();
    if ($model->save()) {
        echo "success";
    } else {
        echo "fail";
    }
}

In my Model i have this

 class Draft extends \yii\db\ActiveRecord
    {
    public $created_at;
    public $updated_at;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'draft';
    }

    /**
     * @inheritdoc
     */

i have no rule set in my Model

Now if i tried to save this in DB only the $model->updated_at don't get saved, i try to echo the outcome and i got the timestamp as expected but it would not save in database, i try check the column type and it's in INT so it shouldn't have any problem but still it has, anyone can help me on this

sam
  • 753
  • 1
  • 9
  • 32
  • try `$model->save(false);` – GAMITG Jun 30 '16 at 11:47
  • Is Your Created at is being saved ? – Chetan Sharma Jun 30 '16 at 11:49
  • Try this to see the exact error.`if ($model->save()) { echo "success"; } else { print_r($model->getErrors());exit; }` – yetanotherse Jun 30 '16 at 11:50
  • @Unknown Yes my created_at is been saved and i did it the same way just that the action is in create action – sam Jun 30 '16 at 11:55
  • If the model is saved with ->save(false) then you must check for you validation rules .. see this for refer http://stackoverflow.com/questions/33759514/model-save-not-working-in-yii2 – scaisEdge Jun 30 '16 at 12:00
  • @Shishir it echo success because it successfully save the other model attributes except updated_at attribute – sam Jun 30 '16 at 12:00
  • @ GAMITG i try $model->save(false); but seems not to work too, the result it i got success on successful update but update_at attribute not been updated – sam Jun 30 '16 at 12:02
  • @ scaisEdge it wasn't saved with ->save(false) and i comment out all the validation rules in model already – sam Jun 30 '16 at 12:03
  • Can you print the value of `\time()` and see what it's returning? If only this attribute isn't saving then may be it's being set as empty. You can also try `$model->updated_at = new yii\db\Expression('NOW()');` – yetanotherse Jun 30 '16 at 12:15
  • @ Shishir i can print the value of $model->updated_at and it return 10 digit time stamp so i think this problem is very weird i will try your suggestion and see if it works – sam Jun 30 '16 at 12:22

1 Answers1

0

Remove

 public $created_at;
 public $updated_at;

From the model class public property, as these will be treated as non saving attributes.

Then save the model.

If you get error Please post it, so that we can check if there is other issue with the model.

so final code will be:

class Draft extends \yii\db\ActiveRecord
    {

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'draft';
    }

    /**
     * @inheritdoc
     */

But I recommend you using RULES for model to filter and validate data

Chetan Sharma
  • 2,509
  • 5
  • 23
  • 41
  • Thanks very much u just save my day, i Remove the attribute as u said and it worked fine. thanks nice one – sam Jun 30 '16 at 15:13