How to set a var in model from controller?

I want to use a var from my controller to my behavior and scope in my model?

What is the best way to do this? as many times the model is called statically?

you can probably set it as an attribute/property statically like adding this to your model

public static $var;

and in your action you would use

ModelName::var = 'assign something';

Could you please describe in details what are you trying to achieve?

To ‘call model’ you don’t have to declare static methods. You can

call common model methods like follows:


If you need your variable as a validation parameter, I recommend you

to aviod declaring additional properties for many reasons. In my app

I had to solve 2 tasks:

  • Disallow certain attributes from being massively assigned.

  • Disallow same attributes to be validated.

  • Implement parametrized validation.

In controller we populate a list of attributes to bypass validation:

$attributes = $form->getSafeAttributeNames('');

// ... and exclude hidden attributes.

if (!$this->useName) unset($attributes['Name']);

if (!$this->useSubject) unset($attributes['Subject']);

if (!$this->useDepartment) unset($attributes['Department']);

if (!$this->useCaptcha) unset($attributes['Captcha']);

if ($form->validate('', $attributes)) ...

For parametrized validation I recommend you to combine

YII-based validation and method-based validation. For example:

if ($form->validate() && $form->signup($this->approved)) ...

Signup method would perform additional validation and record manipulation.

Well I am trying to pass a var to behavior and scope, there is pretty much no way to get these values other then the controller

I tried to do a static var as suggested but:


    public function behaviors(){

        return array(

            'TreeBehavior' => array(

                'class' => 'application.extensions.nestedset.TreeBehavior',

                '_idCol' => 'id',

                '_lftCol' => 'lft',

                '_rgtCol' => 'rgt',

                '_lvlCol' => 'level',

                '_rootId' => self::$root_id




    public function scope()


        return array(

            'condition' => 'root_id = ' . self::$root_id,



But then something like:

$root = Pages::model()->findByPK($root_id);

$root->model()->root_id = $root_id;

Does not work

$root = Pages::model()->findByPK($root_id);

$root->root_id = $root_id;

Did not work also… I guess because the class method model is being called statically…

I solved it by…

public static function model($root_id, $className=__CLASS__)


        self::$root_id = $root_id;

        return parent::model($className);


But still wondering if this is the way to go to get data in the model :P