0

作为 FuelPHP 的新手,我有几个问题......

我开始使用 Oils 脚手架功能创建一个简单的博客应用程序。之后,我按照 ORM 文档设置了我的表之间的所有关系(表称为 'posts'、'categories' 和 'categories_posts' )。到目前为止,一切都完美无缺,例如选择具有关系的数据(帖子及其相关类别)。

但现在我坚持使用 Oil 生成的表单创建新帖子。我已经对其进行了修改,并为存储在数据库中的每个类别添加了一个复选框。提交表单会将记录插入到“posts”表中,但不会插入到“categories_posts”表中。

这是正确命名复选框的问题吗?还是我需要为“categories_posts”表编写模型?我错过了什么?

4

1 回答 1

1

您不需要为categories_posts表创建模型。

在Controller_Posts中的action_create()方法中,数据库插入代码应类似于:

try
{
    $post = Model_Post::forge();

    $post->title = Input::post('title');
    $post->text = Input::post('text');

    /* the next line will create the relation between the two tables */
    $post->categories[] = Model_Category::find(Input::post('category_id'));

    if ($post and $post->save())
    {
        /* the post has been saved */
    }
    else
    {
        /* something went wrong */
    }
}
catch (\Orm\ValidationFailed $e)
{
    /* validation error */
}

您可以在文档中查看建立和破坏 has-many 关系的示例。

于 2012-03-21T22:27:14.287 回答