Hello, confused in relational communications YII, I ask for help.

There are 2 tables, Products and Related

Products (id, title, etc ..), and related (products_id, link_id)

I can not link them so that in the product to withdraw all related products.

I read to the dock, I tried it myself, nothing happened.

Found it in the dock, maybe someone will need it.

'p_related'=>array(self::HAS_MANY,'Related','products_id','joinType'=>'INNER JOIN'), 'related'=>array(self::HAS_MANY,'Products',array('link_id'=>'id'),'through'=>'p_related','joinType'=>'INNER JOIN'), 

    2 answers 2

     class Products{ // public function relations() { return array( //related - это ваше имя. HAS_MANY - предопределенная константа related_id - поле РОДИТЕЛЬСКОГО товара 'related' => array(self::HAS_MANY, 'Products', 'related_id'), ); } // 
    • It seems to me or with your connection you can withdraw only 1 related product? because In the product there is only 1 related_id field, and in my cases there is a separate table that links the goods and related products. - dogmar

    Products-> with ('relationName') -> find called? Show generally how the models are related, how you call them and look for them.

    • I do not know what connection to do in this case, but I know how I would enter CI :) - dogmar
    • one
      I would make a method in the Products: getRelatedProducts ($ id_product) {Return Related :: model () -> findAll ('id =: id', array (': id' => $ id_product)) model; } Received Products, called: Products :: model () -> getRelatedProducts ($ product-> id); - Ray