php Yii isNewRecord 在 beforeSave 中是假的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8208524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:09:33  来源:igfitidea点击:

Yii isNewRecord is false in beforeSave?

phpyii

提问by Yun

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

我在我的模型中创建了一个 beforeSave 方法(扩展 GXActiveRecord), if isNewRecord 永远不会被触发。不过,我的 beforeSave() 被调用了。

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

当我打印 $this->isNewRecord 变量时,它是错误的。这个变量什么时候设置为false?我很确定这是新的

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;

    }

采纳答案by schmunk

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

如果您以前从未保存过 CActiveRecord->isNewRecord 则为 false。

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)

回答by Amul

Well, this may be too late to answer, but I just wanted to get this out, cause this has caused enough confusion already (for me at least)...

好吧,现在回答可能为时已晚,但我只是想解决这个问题,因为这已经引起了足够的混乱(至少对我而言)......

If you call parent::beforeSave()in your model's beforeSave(), and test for isNewRecordafter this, it will always evaluate to false, because model is saved.

如果您调用parent::beforeSave()模型的 beforeSave() 并isNewRecord在此之后进行测试,它将始终评估为false,因为模型已保存。

You have your model built on top of a framework class, so when you override a method, do your task first then call on the parent method. So:

您的模型建立在框架类之上,因此当您覆盖一个方法时,首先执行您的任务,然后调用父方法。所以:

protected function beforeSave() {


if ($this->isNewRecord)
    //do something
else
    //do something else
/* some more code*/
parent::beforeSave();
return true;
}

You can also call parent beforeSave() like this:

您也可以像这样调用 parent beforeSave():

return parent::beforeSave();

or

或者

return true && parent::beforeSave();

Hope this helps other who find this question through google.

希望这可以帮助其他通过谷歌找到这个问题的人。

回答by Gessle

At least in my case

至少在我的情况下

print($this->isNewRecord);
print(parent::beforeSave());
print($this->isNewRecord);

prints truein every line.

打印true在每一行。

And sorry I don't know how to reply to the answer above.

对不起,我不知道如何回复上面的答案。