How To Bypass "user" When Using Same Models For Frontend And Console?


When reading a record in AR, I have a lot of “user-related” code that is executed in the model’s init() and afterFind() functions.

But in a console script, I want to bypass this "user-related" code (because console scripts do not have a user).

Problem is: init() and afterFind() are executed BEFORE I can set a flag in the model or set the model’s scenario. So it throws errors when reaching “user-related” code.

Obviously this is not a problem when creating or writing models, because then you have time to set flags/scenarios.

Any ideas?

Ps. I do not what to create separate models for console programs.

Found solution:

Console scripts have their own config params.php file.

Just add the flag in that file’s parameter array.

this should be straight forward define a model property and use that as a scenario/check also paste your code if possible, one cannot understand the whole picture without looking at code

in your model:

public $skip=false;


public function afterFind()


                if ($this->skip){    

                        // skip this code




// set skip to true

$model = ModelName::model();

$model->skip = true;


well I was still editing that could also work

Hi alirz23, thanx for the reply.

Unfortunately this code is not working:

// set skip to true

$model = ModelName::model();

$model->skip = true;


The problem is that the first line "$model = ModelName::model();" also executes its own init() function, by which time $model->skip has not been set yet - resulting in errors.

(I need to test the ‘skip’ parameter in both init() and afterFind().)

So for the moment, I find the config parameter to be the only working solution.



Well I guess that config/param does the trick