Yii Many To Many Relations

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:

[color="#FF0000"]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.[/color]

plz help for any solution thank you YII :(

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.

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.