I’m trying to understand the use of the parameter passed when instantiating a model.
For example, in a "typical" controller in an actionAdmin() we have
$model = new MyModel('search');
However, I don’t see any changes if I omit passing a parameter
$model = new MyModel();
The reason I’m asking is because I want to pass a parameter and I’m adding an init function in the model class, but I wonder if there is any reason why I shouldn’t do this.
$model = new myModel('xxx');
class MyModel extends CActiveRecord
{
public function init() {
if($this->getScenario() == 'xxx') ...
}
above code will use the default scenario ‘insert’;
the scenario is often used by validators . read your MyModel::rules method
when you replace the “search” scenario to ‘insert’ (omit param) it will apply different validate rule .
$model = new MyModel('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['MyModel']))
$model->attributes = $_GET['MyModel'];
notice : $model->attributes = $_GET[‘MyModel’];
this will use the massive assignment readTheGuide and use the “search” scenario . you should know when you use different “insert” scenario some attribute will not be assigned a value .