2012-03-21 72 views
0

完全被新FuelPHP我有几个问题...ORM,CRUD与多对多关系中使用FuelPHP

我开始创建使用油脚手架功能简单的博客应用程序中插入的记录。之后,我在ORM文档之后建立了我的表格之间的所有关系(表格被称为'posts','categories'和'categories_posts')。迄今为止,Everthing完美无缺地工作,例如,选择关系数据(帖子及其相关类别)。

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

这是一个正确命名复选框的问题吗?或者我需要为'categories_posts'表编写一个模型?我错过了什么?

回答

1

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

Controller_Postsaction_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 */ 
} 

您可以检查例子建立并上破documentation有一对多的关系。

+0

正是我在找的东西......谢谢! – gregory 2012-04-04 12:59:11