Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
What is the difference between $_belongs_to and $_has_one
  • From the docs



    Relations: Belongs To

    Has the primary key of the relation kept in its table, belongs to 1 related object. This is the other side of a HasOne or HasMany relation.



    Relations: Has One


    Specifies a one-to-one relationship to another model. The target model must include a "Belongs To" reference to the current model to allow the inverse relationship.

    In short: belongs_to is the child/dependent resource while has_one is the parent/depending resource
  • How would it be different if in a one to many relationship (such as state and city) I used:
     $_has_many (cities) in the state model, and 
     $_has_one (state) in the city model.
    .... instead of using $_belongs_to in city?

    Thanks

  • HarroHarro
    Accepted Answer
    technically there is no difference, but the ORM uses the difference to determine where the foreign key is.

    If you have a one-to-one relation, one side is a has_one, the other side is the belongs_to, and the belongs_to side always has the foreign key (this is the easiest to remember).

    In this respect, you define a has_one and a has_many the same way, both have a belongs_to and the child side of the relation.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion