I have two models, Model_Post
and Model_Category
. I've managed to "find" all the related data (easy as $post->$categories), but now I need to create relationships (in the posts_categories table) between a post and multiple categories upon the post's create/update/delete.
Here's Model_Post
protected static $_many_many = array(
'categories' => array(
'table_through' => 'posts_categories',
'key_through_from' => 'post_id',
'model_to' => 'Model_Category'
)
);
Model_Category
protected static $_properties = array(
'id',
'name',
'created_at',
'updated_at'
);
protected static $_many_many = array(
'posts' => array(
'table_through' => 'posts_categories',
'key_through_from' => 'id',
'key_through_to' => 'post_id',
'model_to' => 'Model_Post'
)
);
posts_categories table fields: id, name
.
I'm stuck here. How should I build the query?
$post->categories = Model_Category::forge()->set(array(
// I can't get any further
),
);
Do I need to make a Model for the relationship table as well?