Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii isNewRecord is false in beforeSave?

Tags:

php

yii

I made a beforeSave method in my model (extends GXActiveRecord), the if isNewRecord never gets triggered. My beforeSave() gets called, though.

When I print the $this->isNewRecord variable, it's false. When is this variable set to false anyway? I'm very sure it's new

public function beforeSave(){


        if(parent::beforeSave())
            {
              if($this->isNewRecord){
                    $this->setAttribute('doc_status','new');
                    print "something";
              }else{
                  $this->setAttribute('doc_status','updated');
              }
            return  true;
            }  else { return false;

    }
like image 641
Yun Avatar asked Nov 21 '11 07:11

Yun


1 Answers

CActiveRecord->isNewRecord is false if you've never saved it before.

E.g.

$model = new Product;
$model->name = uniqid("bar");
echo "isNewRecord?".$model->isNewRecord; // 1 (true)
$model->save();
echo "isNewRecord?".$model->isNewRecord; // (false)

$model = Product::model();
$model->name = uniqid("foo");
echo "isNewRecord?".$model->isNewRecord; // (false)
$model->save();
echo "isNewRecord?".$model->isNewRecord; // (false)
like image 123
schmunk Avatar answered Sep 29 '22 18:09

schmunk