需要一些帮助来理解模型和数据提供者

发布于 2025-01-04 14:34:06 字数 1761 浏览 0 评论 0原文

我是 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 (即两者都有不好的做法)?我可以从模型中获取我需要的信息吗?我对模型和数据提供者之间的差异感到困惑。

I am new to Yii and am trying to understand it by creating a hobby web site to manage my food shopping. I am trying to create a page which lists my recipes and has a +/- button next to each recipe which when pressed updates a shopping list on the same page. All of the recipe ingredients and the shopping list is stored in an SQL database.

So far, I have a controller called ShoppingListController and in my actionIndex() function (which shows the recipes and shopping list) I am creating two dataproviders and a model (for the +/- buttons which are a form) and passing them to my view as follows:

        // 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
        ));

I am a bit confused - I seem to need the model to pass into the +/- forms, I need the first data provider so I can display my shopping list and I need the second data provider so that I can display my recipe information.

However, I am wondering do I actually need the shoppingListModel and the shoppingDataProvider (i.e. is having both bad practice)?. Could I get the information I need just from the model?. I am confused by the difference between a model and a data provider.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧竹 2025-01-11 14:34:06

使用或不使用数据提供者是您的选择。当我需要分页或网格视图时,我使用数据提供程序,而每次我只使用模型中的数据。

数据提供者提供了额外的数据控制选项,但如果您只需要数据,我认为您必须使用模型。

That is your choice that use or not to use a dataprovider. I am using dataproviders just when I need pagination or a gridview, every other time I just use data from models.

Dataproviders give additional control options on data, but if you need just data I think you must use models.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文