behavior - Yii check scenario in beforeSave() -
for 1 action need transform $album_id before save db in model function beforesave() do:
// преобразовать album -> album_id $album_id=array(); foreach($this->string2array($this->album, '\|') $one) $album_id[]=album::model()->findbyattributes(array('album' => $one))->id; $this->album_id = $this->array2string($album_id);
but action don't need transform, because $album_id in proper state. set scenario 'batchcreate' in action:
public function actioncreate() { yii::import('ext.multimodelform.multimodelform'); $model = new album('create'); $song = new song(); $song->setscenario('batchcreate'); ... }
and try check scenario in model:
if(!($this->scenario === 'batchcreate')) { // преобразовать album -> album_id $album_id=array(); foreach($this->string2array($this->album, '\|') $one) $album_id[]=album::model()->findbyattributes(array('album' => $one))->id; $this->album_id = $this->array2string($album_id); }
but condition true. why scenario doesn't set or doesn't check in if statement? or maybe it's better check not scenario, make variable, how set value 2 different cases?
my whole beforesave():
protected function beforesave() { if(parent::beforesave()) { // преобразовать whoes -> $who=array(); foreach($this->string2array($this->whoes) $one) { $userrow = user::model()->findbyattributes(array('username' => $one)); if($userrow) $who[]=chtml::encode($userrow->id); else $who[]=$one; } $this->who = $this->array2string($who); //var_dump( $this->scenario ); if(!($this->scenario == 'batchcreate')) { //if($this->notbatchcreate == 'yes') { // преобразовать album -> album_id $album_id=array(); foreach($this->string2array($this->album, '\|') $one) $album_id[]=album::model()->findbyattributes(array('album' => $one))->id; $this->album_id = $this->array2string($album_id); } return true; } else return false; }
instead of
$song = new song(); $song->setscenario('batchcreate');
you can do
$song = new song('batchcreate');
in beforesave()
if ( $this->scenario != 'batchcreate' ) { echo "good - scenario not batchcreate"; die(); } echo 'nope...'; var_dump($this->scenario); die();
Comments
Post a Comment