2012-03-21 71 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