2012-02-10 86 views
3

我是Yii的新手,並且試圖通過創建一個愛好網站來管理我的食品購物來了解它。我正在嘗試創建一個列出我的食譜的頁面,並在每個食譜旁邊有一個+/-按鈕,當它們按下時更新同一頁面上的購物清單。所有配方成分和購物清單都存儲在SQL數據庫中。需要一些幫助理解模型和數據提供

到目前爲止,我有一個名爲ShoppingListController的控制器,並且在我的actionIndex()函數(顯示食譜和購物清單)中,我創建了兩個數據提供者和一個模型(對於表單中的+/-按鈕)和他們傳遞給我的看法如下:

 // Create a shopping list item model 
     $shoppingListModel = new TblShoppingListItem; 

     // Get the saved shopping list data from the shopping list table 
     $shoppingDataProvider = new CActiveDataProvider('TblShoppingListItem', array(
      'criteria'=>array(
       'select'=>array('recipe_id', 'recipe_multiplier')), 
      'pagination'=>array('pageSize'=>500) 
     )); 

     // Get the recipe ingredient data from the recipe models and order by recipe name   
     $recipeDataProvider = new CActiveDataProvider('TblRecipeIngredient', array(
      'criteria'=>array(
       'select'=>array('recipe_id', 'ingredient_id', 'ingredient_unit_id', 'ingredient_amount'), 
       'with'=>array('recipe','ingredient','ingredientUnit'), 
       'together'=>true, 
       'order'=>'recipe.name ASC'), 
      'pagination'=>array('pageSize'=>500) 
     )); 

     // Render the 'shoppinglist/show' page 
     $this->render('show', array(
      'recipeDataProvider'=>$recipeDataProvider, 
      'shoppingDataProvider'=>$shoppingDataProvider, 
      'shoppingListModel'=>$shoppingListModel 
     )); 

我有點困惑 - 我似乎需要的模型傳遞到+/-形式,我首先需要的數據提供者,所以我可以顯示我的購物清單,我需要第二個數據提供者,以便我可以顯示我的食譜信息。

但是,我想知道我是否確實需要shoppingListModel和shoppingDataProvider(即有兩個不好的做法)?我能從模型中獲得我需要的信息嗎?我對模型和數據提供者之間的差異感到困惑。

+1

您是否顯示舊的購物清單?還是僅僅是現在的?你確切的意思是保存的購物清單數據?你能告訴我recipe_multiplier有什麼意義嗎?購物清單與配方如何完全相關,在邏輯上說,你如何解釋這種關係? – 2012-02-10 18:10:44

+2

這個問題太廣泛而模糊,無法回答。我建議Mewzer花費一些時間閱讀Yii中的ActiveRecord關係,這可能有助於清除事情:http://www.yiiframework.com/doc/guide/1.1/en/database.arr – thaddeusmt 2012-02-12 17:11:05

回答

0

這是您使用或不使用數據提供者的選擇。我正在使用數據提供者時,我需要分頁或GridView,每隔一段時間我只使用模型中的數據。

數據提供者提供了額外的數據控制選項,但如果您只需要數據,我認爲您必須使用模型。