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: 80
  • 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

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