Yii Framework Forum: Can I define foreignKey when using Join? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Can I define foreignKey when using Join? Rate Topic: ***** 1 Votes

#1 User is offline   touya 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 07-December 09

Posted 05 January 2010 - 11:01 PM

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?
老婆是天使
0

#2 User is offline   touya 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 07-December 09

Posted 06 January 2010 - 09:50 PM

someone help!
give me some advice,please.
Thank you!
老婆是天使
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users