Problem with AR relations

Hello everyone! I need your advices and support.

There are three tables at DB, here is screenshot of ER-model:

rghost.ru/36871096/image.png

  1. product - any product. For example, book or iPad.

  2. option - any option which is used when product is being ordered. For example, color or size.

  3. product_option_value - relation of product and option, containing option’s value. For example u can suppose that there are different values of color for any product.

So "product" and "option" are related MANY2MANY through "product_option_value".

Of course raises obvious task: get all products with its options with each option’s values. And here I’m slow-witted.

Obvious, that: product->product_option_value->option

But the relation between option and product_option_value is not clear.

The most clever variant I’ve invented:


class Product extends CActiveRecord

{

...

        public function relations()

        {

                return array(

                        'purchase' => array(self::BELONGS_TO, 'Purchase', 'purchase_id'),

                        'values' => array(self::HAS_MANY, 'ProductOptionValue', 'product_id'),

            'options' => array(self::HAS_MANY, 'Option', array('option_id'=>'id'),'through'=>'values'),

                );

        }

...

}




class Option extends CActiveRecord

{

...

        public function relations()

        {


return array(

            'valueses' => array(

    self::HAS_MANY,

    'ProductOptionValue',

    'option_id',

    'joinType'=>'INNER JOIN',

    'on'=>'`valueses`.product_id = `products`.product_id'),

                );

        }

...

}

And then, controller loads “products.options.valueses”. But it doesn’t work properly. It loads absolutely all values for each option of product.

Have any ideas?

I think I have solved a similar problem for somebody else:

Linky

Hope it helps.

Thanks, but unfortunately, if look deeper into conditions of my problem, one can see that solution is not for my case.