MANY:MANY model, view, controller

I am just learning yii (actually I am an older ‘procedural style’ programmer-now-student doing this outside of class … my first framework experience and my first oop experience not to mention this CMV thing, so please bare with me) and jumped straight into a self::MANY_MANY situation. Three tables with unique id’s and two junction tables as follows (home grown notation):

	foods 1:M birdsfood M:1 birds 1:M birdshabitat M:1 habitat

FROM …\protected\models\birds.php

public function relations()


	return array(

	  'habitat' => array(self::MANY_MANY, 'Habitat','BirdsHabitat(birdid, habitatid)'),

	  'food' => array(self::MANY_MANY, 'Food', 'BirdsFood(birdID, foodID)'),	



This works beautifully (I think)… I redefined the junction tables so their foreign key field names matched their relational table key field names. Not sure, but think the FK’s got yii confused. Turned on the log thing (in protected\config\main.php to check out the select statement yii was creating. Openned up a mysql client session, cut and pasted the yii created query to test it. To be honest, I didn’t believe it would work because it includes something I don’t understand - perhaps someone could explain the habitat_habitat thing below:

Querying SQL: SELECT `habitat`.`habitatID` AS `t1_c0`, `habitat`.`habitat`

AS `t1_c1` FROM `habitat` `habitat`  INNER JOIN `BirdsHabitat`

`habitat_habitat` ON (`habitat_habitat`.`birdid`=:ypl0) AND


FROM protected\views\birds\_view.php:


$habitats = $data->habitat;

&#036;allHabitats = &quot;&quot;;

foreach (&#036;habitats as &#036;myHabitat):

	if (&#036;allHabitats == &quot;&quot;)

		&#036;allHabitats = &#036;allHabitats . &#036;myHabitat-&gt;habitat;


		&#036;allHabitats = &#036;allHabitats . ', ' . &#036;myHabitat-&gt;habitat;



<b><?php echo CHtml::encode($data->getAttributeLabel(‘habitats’)); ?>:</b>

<?php echo CHtml::encode($allHabitats); ?>

<br />

So I got this to work after two days of searching the net. Maybe I saw the solution 10 times w/o getting it … I dunno. But my FIRST BIG QUESTION is why does this work:

&#036;habitats = &#036;data-&gt;habitat; 

and this doesn’t:

&#036;habitats = Birds::model()-&gt;habitat;

And my SECOND BIG QUESTION is: I want these related table values (foods and habitats) available in the admin (cgridview, I believe) and possible other views as well. And I don’t want to dupicate this code for every view. How and where do I do this? In the controller? Somewhere in site\index?

Third: Can I create a (super) class of with ‘fields’ ID and NAME and subclasses of foods and habitats and then if so, how to create and implement these in yii so methods are inherited and correct oop is applied?

Thanks in advance for your help esp with big q #1.

the abester

I removed your duplicate message. You should be able to edit the header text so no need to start a new thread with the same content.

Also, for better readability, use the <> symbol to enclose code snippets in code tags.