Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM many:many - relation
  • hi, I'm looking for a code example for following situation, maybe somebody could help me out: i'd like to build orm model relations for two tables, connected through a third n:m connection table. how do i use the $_many_many and $_many_through properties for this? does somebody have a quick usage example?
    thanks in advance & best wishes: jan.
  • You need to create an in between table, by default this would be named the plural of both in alfabetical order. For instance many-many with Model_User & Model_Group would need a table named 'groups_users' (you can change this by setting a 'table_through' property for the many-many relationship). That table needs 2 columns, each named as a normal foreign key (Model_User gets user_id and Model_Group gets group_id) and for speed you should make both primary key. Both models need to be related to eachother after that:
    // in Model_User
    protected static $_many_many = array('group');
    
    // in Model_Group
    protected static $_many_many = array('user');
    

    And that's pretty much it. Many-Through relationships need a model in between but I wouldn't use that if you don't need it, partially because saving/deleting those isn't possible directly (yet, it's planned for 1.0 final or 1.1).
  • ok, thank you! and how would i have to set the properties for fields and tables if my naming convention differs from the fuel default?
  • Full config with default values:
    // in Model_User
    protected static $_many_many = array(
        'groups' => array(
            'key_from' => 'id',
            'key_through_from' => 'user_id',
            'model_to' => 'Model_Group',
            'key_to' => 'id',
            'key_through_to' => 'group_id',
            'table_through' => 'groups_users',
        )
    );
    
    // in Model_Group
    protected static $_many_many = array(
        'users' => array(
            'key_from' => 'id',
            'key_through_from' => 'group_id',
            'model_to' => 'Model_User',
            'key_to' => 'id',
            'key_through_to' => 'user_id',
            'table_through' => 'groups_users',
        )
    );
    
  • ok, thanks a lot, this should be solving my problem.. :)
  • // Model_Product
    protected static $_many_many = array('category');
    
    // Model_Category
    protected static $_many_many = array('product');
    
    // pivot table categories_products
    // product_id
    // category_id
    
    Once I have the product model with its related category models, how do I add a
    new category to the product model and save?
  • $product = Model_Product::find(1, array('related' => array('categories')));
    $category = Model_Category::find(5);
    
    $product->categories[] = $category;
    $product->save();
    
    // after fetching and after saving you can be sure that the categories are indexed by their primary key
    // thus removing the just added category is easy:
    unset($product->categories[5]);
    $product->save();
    
  • Thanks Jelmer, you make it look so easy. ;)
  • Apologies for bumping such an old thread, but seemed more relevant to post here then to create my own about. Using the code example above, is it possible to automate this anymore?
    I've got my categories / products setup similar with a many to many relationship and I've got the forms for adding a product setup and adding the product with the following code:
    try
    {
     $item = Model_Product::forge(Input::post('data'));
     if($item->save())
     {
     // success message
     }
    }
    catch (Orm\ValidationFailed $e) {
    // caught exception - error message 
    }
    

    Part of my form has a multi select for selecting which categories the product appears in. Is there a way (i.e. naming convention / style) to make it work out using the code above that in then needs to insert the category id numbers into my categories_products table? Or would I have to do it as described above and copy the categories, unset and then save separately?

Howdy, Stranger!

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

In this Discussion