CDbException in MANY to MANY relationship

Hello all,

my problem is that I got a problem with my MANY to MANY relationship.

I want to find all "Ansprechpartner" (contact persons) for my "Firmen" (Companies).

Model Firma (Company)




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(

			'adressen' => array(self::HAS_MANY, 'Adresse', 'firma_id'),

			'bewerbungenAnFirma' => array(self::HAS_MANY, 'BewerbungAnFirma', 'firma_id'),

			'ansprechpartner' => array(self::BELONGS_TO, 'Ansprechpartner', 'ansprechpartner_id'),

			'ansprechpartners' => array(self::MANY_MANY, 'Ansprechpartner', 'firma_has_ansprechpartner(firma_id, ansprechpartner_id)'),

			'firmentermine' => array(self::HAS_MANY, 'Firmentermin', 'firma_id'),

		);

	}



Model Ansprechpartner (contact person)




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(

			'firma' => array(self::HAS_MANY, 'Firma', 'ansprechpartner_id'),

			'firmen' => array(self::MANY_MANY, 'Firma', 'firma_has_ansprechpartner(ansprechpartner_id, firma_id)'),

			'firmentermine' => array(self::HAS_MANY, 'Firmentermin', 'ansprechpartner_id'),

			'schulen' => array(self::HAS_MANY, 'Schule', 'ansprechpartner_id'),

			'schuleHasAnsprechpartners' => array(self::HAS_MANY, 'SchuleHasAnsprechpartner', 'ansprechpartner_id'),

		);

	}



Now I want to use the relationship with the command:




$this->data->ansprechpartners



but I only get the exception that my relation isn’t define in the right way: The used JOIN-table in the foreign key “firma_has_ansprechpartner” wasn’t found in the database.

But this is my database…

I hope you can help me ;D

Hi,

Can you print the error message and paste your full model’s code?

If you created the table recently, you can try disable the cache of database (removing the code below from your main.php file):


'schemaCachingDuration' => 3600, 

Hey,

thanks for your reply.

I don`t have such a function in my main.php …

In the Attachment you`ll see my Exception

Thanks

That`s the Exception I get when I call the relation ot itself.

Translation: “The relation ‘ansprechpartners’ of the AR ‘Firma’ isnt defined right: The used Join-table 'firma_has_ansprechpartner' in the foreign key wasnt find in the database”

But I have the table in the database and I generated the relations with Gii and it doesn`t work :unsure: :huh:

I solved it!!!

I had to set the databasename in front of the foreign-key call…




'ansprechpartners' => array(self::MANY_MANY, 'Ansprechpartner', 'akquise.firma_has_ansprechpartner(firma_id, ansprechpartner_id)'),