Yii Framework Forum: Many_Many Relation On A Non Pk Foreign Key - Yii Framework Forum

Jump to content

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

Many_Many Relation On A Non Pk Foreign Key Rate Topic: -----

#1 User is offline   Tanguy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 07-September 12

Posted 11 February 2013 - 01:49 PM

Hello,

I'm trying to define a relation with a N..N table, but the problem is that the middle table doesnt use the PK of a table:

Exemple

Table user :
id (identity)
code AK/FK   <-- this code is only generated when an user is validated...
...


Table commission :
id (identity)
name


Table commissionfilter :
idcommission
codeperson


User Model relations (disabled to fix it) :
'commissions' => array(self::MANY_MANY, 'Commission', 'commissionfilter(codeperson, idcommission)'),

Is there a way to force (like HAS_MANY or HAS_ONE relations) the key used in the user table

I tried to make a manual relation with :

	public $_commissions;
...
	/**
	 * Complex definition of "commissions" relation via commissionfilter
	 */
	public function getCommissions()
	{
		$criteria = new CDbCriteria;

		$code = $this->code;
		if (!empty($code)) {
			$criteria->join =
				'LEFT JOIN commissionfilter CF ON CF.codeperson= '.$code;
			$criteria->condition = 't.id = CF.idcommission';
		} else {
			$criteria->condition = 't.id = 0';
		}

		return Commission::model()->findAll($criteria);
	}


but i can't use User::model()->with('commissions')->findByPk($id); in this case...

>> Relation "commissions" is not defined in active record class "User".

but hmmm, it seems to work without the with()... so, is there a way to make a USER defined relation to keep the optional loading system ?

Edit: in fact the relation seems to works now... confused

This post has been edited by Tanguy: 11 February 2013 - 02:08 PM

0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,085
  • Joined: 16-February 11
  • Location:Japan

Posted 12 February 2013 - 07:02 AM

Hello Tanguy, welcome to the forum.

View PostTanguy, on 11 February 2013 - 01:49 PM, said:

'commissions' => array(self::MANY_MANY, 'Commission', 'commissionfilter(codeperson, idcommission)'),

Is there a way to force (like HAS_MANY or HAS_ONE relations) the key used in the user table


Did you try this?
'commissions' => array(self::MANY_MANY, 'Commission', 'commissionfilter(array(codeperson=>code), idcommission)'),


It's not very clear from the description of the guide if custom FK->PK pair is also supported in MANY_MANY relation or not. In fact I don't know myself. But I think it's worth trying.
http://www.yiiframew...ng-relationship
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