-1

For one action I need transform $album_id before save it to DB in model function beforeSave() i do:

// преобразовать album -> album_id
$album_id=array();
foreach($this->string2array($this->album, '\|') as $one)
    $album_id[]=Album::model()->findByAttributes(array('album' => $one))->id;

$this->album_id = $this->array2string($album_id);

but for another action I don't need this transform, because $album_id is already in proper state. So I set scenario 'batchcreate' in that action:

public function actionCreate()
{
    Yii::import('ext.multimodelform.MultiModelForm');

    $model = new Album('create'); 

    $song = new Song();
    $song->setScenario('batchcreate');
    ...
}

and try to check this scenario in model:

if(!($this->scenario === 'batchcreate')) {
    // преобразовать album -> album_id
    $album_id=array();
    foreach($this->string2array($this->album, '\|') as $one)
        $album_id[]=Album::model()->findByAttributes(array('album' => $one))->id;
    $this->album_id = $this->array2string($album_id);
}

but the condition is always true. Why my scenario doesn't set or doesn't check in if statement? Or maybe it's better to check not scenario, but make another variable, so how to set its value for 2 different cases?

my whole beforeSave():

protected function beforeSave()
    {
        if(parent::beforeSave())
        {

            // преобразовать whoes -> who
            $who=array();
            foreach($this->string2array($this->whoes) as $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, '\|') as $one)
                    $album_id[]=Album::model()->findByAttributes(array('album' => $one))->id;
                $this->album_id = $this->array2string($album_id);
            }



            return true;
        }
        else
            return false;
    } 
almix
  • 289
  • 1
  • 8
  • 21
  • What is the base class of Album and Song? – Valentin Rodygin Nov 27 '14 at 09:56
  • class Song extends ActiveRecord; class Album extends ActiveRecord – almix Nov 27 '14 at 09:59
  • 1
    Have you tried to var_dump( $this->scenario )? Where do you perform this check? What method of your model? – Valentin Rodygin Nov 27 '14 at 10:11
  • i perform this check in model in protected function beforeSave(); Valentin, i make variable instead scenario and check it - if($this->notbatchcreate == 'yes'), in one controller action i set it as $model->notbatchcreate = 'yes'; but in another not. So it works. But why scenario doesn't work i can't understand – almix Nov 27 '14 at 10:22
  • hm! var_dump( $this->scenario ) shows: string(6) "insert", and with if(!($this->scenario == 'insert')) it works too, but i set scenario = 'batchcreate', why it is 'insert'? – almix Nov 27 '14 at 10:29
  • 1
    'insert' is the default scenario set in CActiveRecord's constructor. It's hard to say what's wrong looking on the tiny snippet of code in your question. – Valentin Rodygin Nov 27 '14 at 10:35
  • @almix could you include the whole `beforeSave` function? – topher Nov 27 '14 at 11:37
  • added code of beforeSave function – almix Nov 27 '14 at 11:44

2 Answers2

1

Instead of

$song = new Song();
$song->setScenario('batchcreate');

you can simply do

$song = new Song('batchcreate');

In beforeSave()

if ( $this->scenario != 'batchcreate' ) {
    echo "good - scenario is not batchcreate";
    die();
}
echo 'nope...';
var_dump($this->scenario);
die();
took
  • 5
  • 4
Maug Lee
  • 905
  • 1
  • 7
  • 14
0

Switch the order: call parent::beforeSave() after your code for checking the scenario. The inherited method beforeSave() may be altering your scenario.

topher
  • 13,854
  • 5
  • 51
  • 67