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.
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
}
parent::afterFind();
}
// set skip to true
$model = ModelName::model();
$model->skip = true;
$model->findAll()
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.