Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Many to many and other question about ORM
  • Hi guys, - I'm currently trying Fuel and don't find anyway to delete a many to many relation ? I have a model user with a many to many relation to tool model - Then, maybe an issue but you can add the same relation twice or more time ?! Strange
    Tried with this code, executed many times with the same tool id
    $user = Model_User::find_by_id(Session::get('user_id'));
    $tool = Model_Tool::find_by_id($id);
    $user->tools = $tool;
    $user->save();
    

    - How can I fetch easily a extra colum on users_tools table ? Last question: - I want to fetch the first related objects ordered by a column for a loaded object like
    $tool = Model_Tool::find_by_id($id);
    ...
    $tool->languages->order_by('date', 'desc')->get_one();
    
    For the moment I use this probably bad method
    

    $lang = Model_Language::query()
     ->where('tool_id', $tool->id)
     ->order_by('date', 'desc')
     ->get_one();
    
    thanks edit: wow ??? <code> doesn't work
  • hum... After few tests, I don't understand why fetching an extra column value... The same for SETTING this column value. Can you help me ? context: Model_User (tables users), Model_Tool (table tools). Many to many relation store in users_tools table, it's contain 3 columns:
    - id_user
    - id_tool
    - created Now... how fetch created column in this case:
    <code>
    $user = Model_User::find_by_id($id);
    $user-> ??? $user->tools[x]->created doesn't work (I have an other created column in tools table).
    </code> And now how to setting up this extra column ? <code>
    $user = Model_User::find_by_id($idu);
    $tool = Model_Tool::find_by_id($idt); ??? $user->tools = array($tool);
    </code> thks
  • You can't fetch values from the in-between table, if you want that you need to create a model in between and fetch the relations nested. Like this: Model_User has many Model_Tool_Users
    Model_Tool has many Model_Tool_Users Model_Tool_User belongs to Model_User
    Model_Tool_User belongs to Model_Tool When setup like that you can fetch like this:
    $users = Model_User::find()->related('tool_users')->related('tool_users.tool')->get();
    
  • - I'm currently trying Fuel and don't find anyway to delete a many to many relation ? I have a model user with a many to many relation to tool model
    Easy: fetch the relationship and delete the ID of the relation you want removed from the array. Example:
    $user = Model_User::query()->related('articles')->where('id', 1)->get_one();
    unset($user->articles[5]); // removes relationship with article with ID 5
    $user->save();
    
    - Then, maybe an issue but you can add the same relation twice or more time ?! Strange
    Tried with this code, executed many times with the same tool id
    
    Have you tried it or are you just assuming without checking? The Orm will detect the relation is already there and won't save it a second time. Shouldn't even be possible btw, best way of setting up the in-between table is to have both the foreign-keys combined set as PK.
    How can I fetch easily a extra colum on users_tools table ?
    
    It will always fetch all columns, picking which column to fetch and not to fetch isn't possible yet. Will probably be in Fuel 1.1.
    I want to fetch the first related objects ordered by a column for a loaded object like
    You can only add conditions with eager loading, there's nothing bad about eager loading btw - if you're sure you're going to need the relation it will be far more efficient in most cases.
  • Jelmer Schreuder wrote on Tuesday 21st of June 2011:
    - I'm currently trying Fuel and don't find anyway to delete a many to many relation ? I have a model user with a many to many relation to tool model
    Easy: fetch the relationship and delete the ID of the relation you want removed from the array. Example:
    $user = Model_User::query()->related('articles')->where('id', 1)->get_one();
    unset($user->articles[5]); // removes relationship with article with ID 5
    $user->save();
    
    - Then, maybe an issue but you can add the same relation twice or more time ?! Strange
    Tried with this code, executed many times with the same tool id
    
    Have you tried it or are you just assuming without checking? The Orm will detect the relation is already there and won't save it a second time. Shouldn't even be possible btw, best way of setting up the in-between table is to have both the foreign-keys combined set as PK.
    How can I fetch easily a extra colum on users_tools table ?
    
    It will always fetch all columns, picking which column to fetch and not to fetch isn't possible yet. Will probably be in Fuel 1.1.
    I want to fetch the first related objects ordered by a column for a loaded object like
    You can only add conditions with eager loading, there's nothing bad about eager loading btw - if you're sure you're going to need the relation it will be far more efficient in most cases.
    Ok thanks for the reply. I'll test all of it but yes, I tried to setting up the same relation manytimes and yes it works :(

Howdy, Stranger!

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

In this Discussion