For a many to many relation to work between the Post and Category models, you should have three tables in your database: posts, categories and categories_posts.
The first two shouldn't need explanation and the third one is to handle the many/many relation between the two models. It's structure should be similar to this:
CREATE TABLE `categories_posts`
(
`category_id` BIGINT UNSIGNED NOT NULL,
`post_id` BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (`category_id`, `post_id`)
);
Being $post a Model_Post object and $post->categories an array for associated categories, we can start working.
To start associating, we forge a new Model_Category object and add it to the array:
// forge object
$category = Model_Category::forge();
// set name
$category->name = 'Brand New Category';
// associate with Posts
$post->categories[] = $category;
/**
* we can repeat the last three steps as many times as we want
* or as many times as we need
*/
// finally, save the relation
if ($post->save(true, true)) {
echo "Everything OK";
} else {
echo "Houston, we've got a problem!";
}
Notice the two boolean parameters passed to the save() method. They are to cascade through the relations and to use transactions respectively. It's a good idea to use that when relating models in one go.
You should read the ORM documentation, were you'll find a similar example on many to many relations, among other things.