I'm new to Yii here. In the documentation, it seems to imply that gii will create the relations for related models. But when using gii to generate models from DB, it doesn't seem to be the case. For example, I have a user table and a profile table with a column "user_id INTEGER DEFAULT 0", but the relations array is empty in the generated model. Did I do something wrong, or gii just doesn't automatically recognize the relations? Thanks,
Asked
Active
Viewed 5,040 times
1
-
4if you define relations in database, yes it will – Kamil Klimek Feb 06 '12 at 17:55
-
3Is the foreign key constraint in the database set correctly? `user_id INTEGER DEFAULT 0` to me does not imply there is a foreign key constraint on this field – T I Feb 06 '12 at 18:00
-
awesome, thanks! I thought yii would infer from the column name (coming from CakePHP) – Anh Pham Feb 06 '12 at 18:47
2 Answers
3
Gii will create relations for MyISAM tables if you include a format like the following in the comment of the referenced columns:
CONSTRAINT FOREIGN KEY (name_of_this_field) REFERENCES related_table_name(related_field_name)

Bill
- 93
- 1
- 10
1
Gii can automatically create relations for generated models if corresponding tables in your DB have foreign keys, but not all storage engines support foreign keys. For example, if you use MyISAM tables in MySQL, you have no way to define it.

Nayjest
- 1,089
- 8
- 14