Relations: BELONGS_TO versus HAS_ONE

Comparing #3 with #4

Revision #4 was created by Maurizio Domba Cerin Maurizio Domba Cerin on Apr 26, 2011, 8:30:42 AM.

removed "of"

Content

It's very common to see new Yii users confusing the relations `HAS_ONE` and `BELONGS_TO`, and getting it wrong means you won't get proper values back. And though we'll talk about `HAS_MANY` as well, we're specifically omitting the `MANY_MANY` relation because it's a whole different animal. Both `BELONGS_TO` and `HAS_ONE` are about linking two models together, and sound like the same thing, but they link in essentially opposite directions. Let's illustrate with three simple tables, each of which has a primary key of (`id`), and a number of linking fields (`user_id`) that reference the User table.
~~~
USER table
- id
- name
[...]