Pass Parameter To Relations Scope


I’m wondering, is it possible to pass a parameter(variable) to a scope used in relation.


// relation in Product model

public function relations()


    return array(        

        'reg' => array(self::HAS_ONE, 'ProductRegional', 'product_id', 'scopes' => 'lang'),       



// scope in ProductRegional

public function lang($langId = 1)



        'alias' => 'pr',

        'condition'=>'pr.lang_id = :langId',

        'params'=>array(':langId' => $langId),


    return $this;


// instantiate product

$product = Product::model()->findByPk(123);

$regionalData = $product->reg; // returns ProductRegional where lang_id=1

$regionalData = $product->reg(1); // doesn't even go in the scope, returns null

Yes, click here (and scroll up a bit) to see how the parameters are applied.

I presume you mean this:










My scope in ProductRegional is set up like in the example but the problem is how to call it .

I’d like to avoid writing all that ‘with’=>array()… on every query and perhaps use lazy loading.

I tried it like this:

'reg' => array(self::HAS_ONE, 'ProductRegional', 'product_id', 'scopes' => array(

    'lang' => array(

        'params' => 1



and it works but I’d like to pass lang_id as parameter in relation and cannot figure out how.

$regionalData = $product->reg(1); //does not work