activerecord - Yii find or initialize method -
I am looking for a uniform rail method in the Yii structure at the level of a class, based on a pass which Finds the array that ActiveRecord will need to match. If found, it will return to the matched ActiveRecord if it is not found, then a new instant ActiveRecord is returned
Here is an example of how I can expect that I can see it Is:
$ person = person :: model () - & gt; Find_or_initialize_by (array ('name' = & gt; 'bob', 'era' => 20));
But no such function has been made in yii, available bilton functions here
,
You can use this custom function in your custom model
public function find_or_initialize_by ($ array) {$ model = person :: Model) - & gt; Search ('name =: name and age =: age', array (': name' = & gt; $ array ["name"], ': age' = & gt; $ array ["age"])); If ($ model == zero) {$ model = new person; } $ Model- & gt; Name = $ array ["name"]; $ Model- & gt; Age = $ array ["age"]; $ Models & gt; Save (); }
Comments
Post a Comment