simple database relation not working, I cant even start the project

HI, I am starter with yii, I cant get simple database relation 2 work:(

I have database tables: product(id,name,category_id) and category(id, name)

I used gii 2 generate code model and crud.

I added relations 2 Product.php model:

'category' => array(self::BELONGS_TO, 'Category', 'category_id'),

and relations for Category.php model:

'product'=>array(self::HAS_MANY, 'Product', 'category_id'),

Than 2 see this I added in view.php

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




and I see a that field with its label but for the value datagrid say: Not set

can anyone help me here?

I created database with phpmyadmin and set id’s as primary key

could you put here your schema! might be your problem with foreignkey, any way consider following scenario.

[color="#808080"][color=“red”][size=“2”]’[/size][/color][color=“red”][size=“2”]VarName[/size][/color][color=“red”][size=“2”]’[/size][/color][color=“gray”][size=“2”]=>[/size][/color][color=“black”][size=“2”]array[/size][/color][color=“olive”][size=“2”]([/size][/color][color=“red”][size=“2”]’[/size][/color][color=“red”][size=“2”]RelationType[/size][/color][color=“red”][size=“2”]’[/size][/color][color=“gray”][size=“2”], [/size][/color][color=“red”][size=“2”]’[/size][/color][color=“red”][size=“2”]ClassName[/size][/color][color=“red”][size=“2”]’[/size][/color][color=“gray”][size=“2”], [/size][/color][color=“red”][size=“2”]’[/size][/color][color=“red”][size=“2”]ForeignKey[/size][/color][color=“red”][size=“2”]’[/size][/color][color=“gray”][size=“2”], …[/size][/color][color=“green”][size=“2”]additional[/size][/color] [color=“green”][size=“2”]options[/size][/color][/color][color="#808080"][color=“olive”][size=“2”])[/size][/color][/color]

[font="Arial"][size="2"][color="#222222"]where VarName is the name of the relationship; RelationType specifies the type of the relationship, which can be one of the four constants: self::BELONGS_TO, self::HAS_ONE, self::HAS_MANY andself::MANY_MANY; ClassName is the name of the AR class related to this AR class; and ForeignKey specifies the foreign key(s) involved in the relationship. Additional options can be specified at the end for each relationship…[/color]


could you put here your schema!

what do I need to post, do you have in example of some shema pls?

Best Regards

such a simple question and no answer yet?

At a glance your code looks alright (IIRC). Have a look in the log file for the generated SQL. You may want to verify it by pasting it into phpMyAdmin.


+1 need schema… This happened to me because I was missing a Pk ( and I swore I had it too ) … but since you say you have it … output the create schema ?

I solved it, but not sure what was the issue

Thanks for Your help friends