Relations are not properly generated

Hi all,

I have an issue with my databse relations.

I have two tables player and coach and created two additional tables with a Many_to_Many relation between player and coach.

It seems like yii is not able to generate the relation for the Many_to_many relations… any idea how to fix that issue?




--

-- Tabellenstruktur für Tabelle `user`

--


DROP TABLE IF EXISTS `user`;

CREATE TABLE IF NOT EXISTS `user` (

  `id` bigint(20) NOT NULL AUTO_INCREMENT,

  `username` varchar(128) COLLATE latin1_bin NOT NULL,

  `password` varchar(128) COLLATE latin1_bin NOT NULL,

  `email` varchar(128) COLLATE latin1_bin NOT NULL,

   ....

  PRIMARY KEY (`id`),

  UNIQUE KEY `username` (`username`),

  UNIQUE KEY `email` (`email`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 COLLATE=latin1_bin COMMENT='Nur ein Dummy um die Beziehungen zu zeigen...' AUTO_INCREMENT=3 ;




--

-- Tabellenstruktur für Tabelle `player`

--


CREATE TABLE IF NOT EXISTS `player` (

  `id` bigint(11) NOT NULL AUTO_INCREMENT,

  `email` varchar(128) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,

  `created` datetime NOT NULL,

  PRIMARY KEY (`id`),

  UNIQUE KEY `email` (`email`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 COLLATE=latin1_bin;


--

-- Daten für Tabelle `player`

--


--

-- Tabellenstruktur für Tabelle `ratinghashtable`

--


CREATE TABLE IF NOT EXISTS `ratinghashtable` (

  `id_player` bigint(20) NOT NULL,

  `id_user` bigint(20) NOT NULL,

  `hash` varchar(128) COLLATE latin1_bin NOT NULL,

  `validto` datetime NOT NULL,

  PRIMARY KEY (`id_player`,`id_user`),

  UNIQUE KEY `hash` (`hash`),

  KEY `id_user` (`id_user`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin;


--

-- Daten für Tabelle `ratinghashtable`

--

CREATE TABLE IF NOT EXISTS `coachrating` (

  `id_user` bigint(20) NOT NULL,

  `id_player` bigint(20) NOT NULL,

  `rating` tinyint(4) NOT NULL,

  `comment` text NOT NULL,

  `created` datetime NOT NULL,

  PRIMARY KEY (`id_user`,`id_player`),

  KEY `id_player` (`id_player`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin;





--

-- Constraints der Tabelle `coachrating`

--

ALTER TABLE `coachrating`

  ADD CONSTRAINT `coachrating_ibfk_2` FOREIGN KEY (`id_player`) REFERENCES `player` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,

  ADD CONSTRAINT `coachrating_ibfk_1` FOREIGN KEY (`id_user`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;


--

-- Constraints der Tabelle `ratinghashtable`

--

ALTER TABLE `ratinghashtable`

  ADD CONSTRAINT `ratinghashtable_ibfk_2` FOREIGN KEY (`id_user`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,

  ADD CONSTRAINT `ratinghashtable_ibfk_1` FOREIGN KEY (`id_player`) REFERENCES `player` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;



Any Idea why Yii is not generating the Relations properly?

what has Yii generated and what did you expect it to generate

Gii is generating the following:

in Coachrating:




/**

	 * @return array relational rules.

	 */

	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(

			

		);

	}



and for the other new table Ratinghashtable




		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			


		);



The users table just get this:




'players' => array(self::MANY_MANY, 'Player', 'ratinghashtable(id_user, id_player)'),



What I am expecting is, that I can access from the user table all the coachratings, all the created hash values for players and all players.

Same thing from player side: I want to get all coaches, all coachratings and all hash values

But it seems there is an issue during the generation of the relations…

any suggestions?

short term solution you write the relations yourself by editing the respective controller

Thanks for the fast reply.

I implemented the custom relation but when I access the relation property I am getting null returned.

In my Ratinghashtable




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(

			'player' => array(self::BELONGS_TO, 'Player', 'id_player'),

			'coach' => array(self::BELONGS_TO, 'User', 'id_player'),


		);

	}



and in my User




		return array(

			'players' => array(self::MANY_MANY, 'Player', 'ratinghashtable(id_user, id_player)'),

			'coachratings' => array(self::HAS_MANY, 'Coachrating', 'id_user'),

			'hashtable' => array(self::HAS_MANY, 'Ratinghashtable', 'id_user'),



If I try to access the coach property from Ratinghashtable I am getting null.

Controller:




$modelCoach = $modelRatinghashtable->coach;



View:


<?php CVarDumper::dump($modelCoach);?>

Any idea what the issue is?

Looks like you have a typo. Should be like this




'coach' => array(self::BELONGS_TO, 'User', 'id_user'),



As an alternative you may look into how to use through.

/Tommy

yeah thats it.

thanks!