我有两个模型,Model_Post
和Model_Category
。我已经设法“找到”所有相关数据(就像 $post->$categories 一样简单),但现在我需要在帖子的创建/更新/删除时在帖子和多个类别之间创建关系(在 posts_categories 表中) .
这是 Model_Post
protected static $_many_many = array(
'categories' => array(
'table_through' => 'posts_categories',
'key_through_from' => 'post_id',
'model_to' => '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 表字段:id, name
.
我被困在这里。我应该如何构建查询?
$post->categories = Model_Category::forge()->set(array(
// I can't get any further
),
);
我还需要为关系表制作模型吗?