需要一些帮助理解模型和数据提供
问题描述:
我是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(即有两个不好的做法)?我能从模型中获得我需要的信息吗?我对模型和数据提供者之间的差异感到困惑。
答
这是您使用或不使用数据提供者的选择。我正在使用数据提供者时,我需要分页或GridView,每隔一段时间我只使用模型中的数据。
数据提供者提供了额外的数据控制选项,但如果您只需要数据,我认为您必须使用模型。
您是否显示旧的购物清单?还是仅仅是现在的?你确切的意思是保存的购物清单数据?你能告诉我recipe_multiplier有什么意义吗?购物清单与配方如何完全相关,在逻辑上说,你如何解释这种关系? – 2012-02-10 18:10:44
这个问题太广泛而模糊,无法回答。我建议Mewzer花费一些时间阅读Yii中的ActiveRecord关系,这可能有助于清除事情:http://www.yiiframework.com/doc/guide/1.1/en/database.arr – thaddeusmt 2012-02-12 17:11:05