There is a model:
class Model extends ActiveRecord
{
public static function model($className=__CLASS__) {
return parent::model($className);
}
public function toSave(Array $data)
{
$this->setAttributes($data);
$this->save(); // returns true
return $this;
}
}
and running
$model = Model::model()->toSave($data);
and when im dumping $model there is all data which setted from $data but not exists PrimaryKey (id).
but, if i run
$model = new Model;
$model->toSave($data);
works as expected.
Where is a problem?
you are doing multiple save, through iterating, and pass new set of
$data
everytime.$model
here is an object of single record. So by doing everytime, new model , you are creating fresh new object, assign data and save. Later you did is the right approach.