Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
relationship 1-* between 2 tables
  • Hi ! 
    I'm using fuelphp for a school project. I'm really satisfied of this framework but I have a problem to understand relationships between models.
    I have two tables : Users and Offices. 
    One user belongs to an office. An office has many users. So, it's a 1-* relation.
    I would like to make to type of requests :
    - getting an office with all the users.
    - getting a user with the office he belongs to.

    So, my question is : Do I have to put 2 attributes (attribute $_belongs_to in user and an attribute $_has_many in office) or just one ? 

    Thanks for your help and sorry for my poor level in English.

  • You would define:

    Office has_many User, and User belongs_to Office.

    You always define the relation both ways, it is important with cascading saves and deletes, and to know what are foreign key columns and how to treat them.

    The foreign key (let's say "office_id") is always in the table that has the belongs_to, in this case User.
  • Ok thanks.
    So, when I add a user, I haver to do : 

    $office = Model_Office::query()->get_one();
    $user = new Model_User( ... );
    $office->user[] = $user; // for the relation office has many users
    $user->office = $office; // for the relation user belongs to office
    $user->save();
    $office->save();

    Is that it ?
  • No.

    ORM enables cascading saves by default, so if you $office->save(), it will automatically save related objects, and ORM also deals with all foreign key business. So this is enough:

    $office = Model_Office::query()->get_one();
    $user = new Model_User( ... );
    $office->user[] = $user; // for the relation office has many users
    $office->save();
  • Thanks you, I understand now.
    I juste have a little problem. When I want to add a second user to an office, I have an SQL error which says that the ID already exists :
    Fuel\Core\Database_Exception [ 23000 ]:
    SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY' with query: "INSERT INTO `users` (`id`, `firstName`, `lastName`, `login`, `email`, `phoneNumber`, `password`, `created_at`, `updated_at`) VALUES ('1', 'name', 'lname', 'thelogin', 'mail@mail.fr', 'phone', 'azerty', 1392241692, null)"

    The code :
    $office = Model_Office::query()->get_one();
    $office->users[] = Model_User::forge(
    array( 'firstName'=> 'name',
    'lastName'=> 'lname',
    'login'=>'thelogin',
    'email'=>'mail@mail.fr',
    'phoneNumber'=>'phone',
    'password'=>'azerty'
    )
    );
    $office->save();


  • Where does the '1' come from?

    Normally your 'id' column is primary key, and defined as auto increment. Which means collisions can not occur because the database will assign the next free number.

    If you already have a record with id '1' in your 'Users' table, how did it get there?
  • The ID's in each table are defined as auto increment.
    I also set the following attribut in each table  :
    protected static $_primary_key = array('id');

    I don't understand why fuelphp indicates the id whereas it's auto increment.
  • That is what I don't understand either.

    In your code it is not assigned a value, and the ORM will not invent one.

    The only thing I can think of is an error in your relation definition, so that the ORM thinks it is a foreign key, and because of that it fills in the column?
  • In fact, the problem was in my relation definition, I didn't use the good PK.

    Thanks for your help.
  • HarroHarro
    Accepted Answer
    Cool. Glad you have it sorted.

Howdy, Stranger!

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

In this Discussion