php - Yii model isNewRecord false due to constructor -


in yii activerecord, possible set attribute in model constructor , still have model's isnewrecord property remain true?

i have model constructor create private attribute hold phppass password hash instance. when this, sets isnewrecord property false, though record created new.

i tried workaround call setisnewrecord(true) in constructor if id attribute greater zero, appears attributes not available in constructor.

i had remove constructor, , make statement in each method requiring phpass.

constructor user model:

public function __construct(){     $this->phpass=new passwordhash(yii::app()->params['phpass']['iteration_count_log2'], yii::app()->params['phpass']['portable_hashes']); } 

user model init , isnewrecord condition in controller

public function actionedit($id = null) {      $mymodel = new user();            echo $mymodel->isnewrecord;   //false due constructor } 

when ovveriding contstructor make sure call parent constructor. call parent constructor when ovveriding, unless know doing.

public function __construct($scenario = 'insert'){     parent::__construct($scenario);     $this->phpass=new passwordhash(yii::app()->params['phpass']['iteration_count_log2'], yii::app()->params['phpass']['portable_hashes']); } 

also phpass can stored static property, don't need new instance of phpass each active record instance.

edit: @kevin link question ovverding constructors.

one more important note add: remember pass parameters constructor. in yii model classes there $scenario param default value insert. when ovveride constructor without passing argument, there insert scenario, , there not error message missing param, because has default value. updated code $scenario.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -