Difference between #6 and #7 of
Relations: BELONGS_TO versus HAS_ONE

Revision #7 has been created by Jorgee on Aug 20, 2013, 3:49:16 PM with the memo:

Corrected syntax
« previous (#6)


Title unchanged

Relations: BELONGS_TO versus HAS_ONE

Category unchanged


Yii version unchanged

Tags unchanged

relations, HAS_ONE, BELONGS_TO

Content changed

Relations 3 and 4 are essentially the same thing as each other: the linking field `user_id` is not in **this** model, but in the **related** model, and the primary key involved is in **this** model (`User`). The difference is that `HAS_MANY` returns an array of possibly multiple objects, while `HAS_ONE` returns a single object.

`HAS_ONE` is just a special case of `HAS_MANY`, and the circumstances where `HAS_ONE` makes sense are far more limited than `HAS_MANY` and `BELONGS_TO`.

 Weird example **

USER table
- id
- name
56 0
Viewed: 147 889 times
Version: 1.1
Category: FAQs
Written by: Steve Friedl
Last updated by: Jorgee
Created on: Apr 26, 2011
Last updated: 6 years ago
Update Article


View all history