[Solved] How to make a Relational Active Record?

I have the 2 Tables Product and Nutritions.




Product:

| product_id | Nutrition_nutritions_id | description|

| int        | int                     | varchar    |






Nutrition:

| nutritions_id | vitamina | .. |

|  int          |   int    | .. |



The Models include the needed Relationships:

ex. in Product: ‘nutritionsNutriton’ => array(self::BELONGS_TO, ‘Nutritions’, ‘nutritions_nutriton_id’),

Now i want to create a Datamodel that represents this Columns:




 product_id | description | vitamina |



for that i made




(1)$model=Products::model()->with('nutritionsNutriton')->findByPk((int)$id);

(2)$model=Products::model()->with('nutritionsNutriton')->findAllByPk((int)$id)



Both Statements are creating a valid SQL statement, that returns that what i wanted.

But as in the view i want to display the results with:




<?php $this->widget('zii.widgets.CDetailView', array(

	'data'=>$model,

	'attributes'=>array(

		'product_id',

		'nutritions_nutriton_id',

		'vitamina',			

	),

)); ?>



for (1) it says Property "Products.vitamina" is not defined." as it references the false table.

for (2) it creates the View but sets for all Properties the value "Not Set"

As i made the same as described here and read half the forum im out of ideas at the moment. So i would be pleased by getting some new input.

Take a look at this topic:

http://www.yiiframework.com/forum/index.php?/topic/13377-solved-how-do-i-search-on-a-join-in-adminphp/

It uses the search() function of the model, but there’s no reason why you cannot do the same thing in your loadModel function or elsewhere. ;)

It shows how to add related fields.

Found a simple Solution that works for me:

Using (1)$model=Products::model()->with(‘nutritionsNutriton’)->findByPk((int)$id);

and then using following for the view:




$this->widget('zii.widgets.cdetailview', array(

	'data'=>$model,

	'attributes'=>array(

		'nutritionsNutriton.vitamina', 

	),

));