// in Model_User protected static $_many_many = array('group'); // in Model_Group protected static $_many_many = array('user');
// 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', ) );
// Model_Product protected static $_many_many = array('category'); // Model_Category protected static $_many_many = array('product'); // pivot table categories_products // product_id // category_idOnce I have the product model with its related category models, how do I add a
$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();
try { $item = Model_Product::forge(Input::post('data')); if($item->save()) { // success message } } catch (Orm\ValidationFailed $e) { // caught exception - error message }
It looks like you're new here. If you want to get involved, click one of these buttons!