Yii Framework Forum: Yii Many To Many Relations - Yii Framework Forum

Jump to content

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

Yii Many To Many Relations

#1 User is offline   channasmcs 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 85
  • Joined: 06-March 13
  • Location:Srilanka

Posted 03 June 2014 - 01:13 AM

i have 3 data table

agents

CREATE TABLE IF NOT EXISTS `agents` (
`agentId` varchar(10) NOT NULL,
`position` varchar(20) NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`agentId`)
) ;
.....................
propertydetail

CREATE TABLE IF NOT EXISTS `propertydetail` (
`propertyDetailId` int(11) NOT NULL AUTO_INCREMENT,
`propertyTypeId` int(11) NOT NULL,
`propertyMethodId` int(11) NOT NULL,
`price` int(11) NOT NULL,
PRIMARY KEY (`propertyDetailId`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

IMPORT one property have many agent & one agent have many property

above 2 table join by following table

agentpropertydetail

CREATE TABLE IF NOT EXISTS `agentpropertydetail` (
`agentPropertyDetailId` int(11) NOT NULL AUTO_INCREMENT,
`agentId` varchar(10) NOT NULL,
`propertyDetailId` int(11) NOT NULL,
PRIMARY KEY (`agentPropertyDetailId`),
KEY `agentId` (`agentId`,`propertyDetailId`),
KEY `propertyDetailId` (`propertyDetailId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

..............................................

i wrote following relation for propertydetail & agents model

propertydetail Model


return array(
'agents' => array(self::MANY_MANY, 'Agents', 'agentpropertydetails(agentId, propertyDetailId)'),
);

.................

agents Model

return array(
'propertydetail' => array(self::MANY_MANY, 'Propertydetail', 'agentpropertydetails(agentId, propertyDetailId)'),);

..........

Agentpropertydetail model

return array(
'agent' => array(self::HAS_MANY, 'Agents', 'agentId'),
'propertyDetail' => array(self::HAS_MANY, 'Propertydetail', 'propertyDetailId'),
);



i used this code for View data from propertyDetail _view file

<?php echo CHtml::encode($model->agents->name); ?>
<?php echo CHtml::encode($model->agents->position); ?>


i was face following ERROR :unsure: :blink:

The relation "agents" in active record class "Propertydetail" is not specified correctly: the join table "agentpropertydetails" given in the foreign key cannot be found in the database.


plz help for any solution thank you YII :(
0

#2 User is offline   Diogo Alves 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 03-April 14

Posted 11 July 2014 - 02:06 PM

Hello,
When you generates a new model using Gii tool and you've many-to-many relation it creates to you an array indicating all of your related tables. Look at the first lines of your code and post it here please.
"be different is normal"
0

#3 User is offline   Pecos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 21-March 14

Posted 10 September 2014 - 02:01 PM

I think the name of the connection table is supposed to be "agentpropertydetail" without 's'. Correct me if I'm wrong. Also fairly new to this :)
0

#4 User is offline   Pecos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 22
  • Joined: 21-March 14

Posted 15 September 2014 - 05:02 AM

View PostPecos, on 10 September 2014 - 02:01 PM, said:

I think the name of the connection table is supposed to be "agentpropertydetail" without 's'. Correct me if I'm wrong. Also fairly new to this :)


Did some tests but it didn't make any difference what I named the table. Like this for instance:
'categories' => array(self::MANY_MANY, 'Category', 'monkey(product_id, category_id)'),


Worked just as fine as if I used the correct name of the table:
'categories' => array(self::MANY_MANY, 'Category', 'shop_product_to_category(product_id, category_id)'),


I'm confused. Perhaps it has to do with the rest of my code.
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