Can I define foreignKey when using Join?




public function relations()

    {

        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

            'shop'      => array(self::BELONGS_TO, 'Shop', 'sid','joinType'=>'JOIN'),

            'hs_mall_item'  => array(self::BELONGS_TO, 'HsMallItem', 'tb_item_id','joinType'=>'JOIN','foreignKey'=>'tb_item_id'),

        );

    }



simply saying,I want to get the sql like:

select * from hs_mall_trade join hs_mall_item on hs_mall_trade.tb_item_id = hs_mall_item.tb_item_id

but there is no primaryKey in hs_mall_item, only two unique keys:auction_id and tb_item_id,and the auction_id is before tb_item_id,we can’t use foreign key here,because it’s a waste of performance.

but what I get is:

select * from hs_mall_trade join hs_mall_item on hs_mall_trade.tb_item_id=hs_mall_item.auction_id

I’ve already set the foreignKey attribute in my Model,why does AR still use auction_id as the foreignKey?

And I also set primaryKey in my HsMallItem model like that:




public function primaryKey()

    {

        return "tb_item_id";

    }



It doesn’t work too…

I know there must be some way to solve my problem in Yii (according to the experience several times before :rolleyes:).

And I tried a method which does work , but I don’t want to modify the code of the framework.

My modification is:




#CActiveFinder.php line 975 add two lines:

if( isset($pke->_table->columns[$fk]) )

    $pk = $fk;

else if(is_array($pke->_table->primaryKey)) // composite PK

    $pk=$pke->_table->primaryKey[$i];

#....



if the $fk column name exists in the foreign table,then use this column as foreign key.

Is there a better way?

someone help!

give me some advice,please.

Thank you!