调用视图代码中的模型函数 [英] Calling model function in view codeigniter

查看:137
本文介绍了调用视图代码中的模型函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的MVC,我正在移植一个非MVC风格编写的项目到MVC,但我困在一个问题,有必要在View中调用Model函数。



场景:



表1 - 产品:

包含 product_id product_name 等,每个产品可以有多个版本。



表2 - 版本:

包含 version_id version_name ,..., product_id 等。



在每个产品标题下,我必须显示该产品的版本列表,在非MVC风格它是相当简单,我可以使用下面的代码片段在视图:

  foreach($ product as $ row)
{
echo $ row ['product_name'];
if($ main-> getVersionList($ vresult,$ row [product_id]))
{
foreach($ vresult as $ vrow)
{
echo $ vrow ['version_name'];
}
}
}

数组从控制器到视图,但是每个需要生成对应每个产品的每个版本数组呢?



strong>



这是我在控制器中的最终工作解决方案(使用地图):

  $ this-> load-> model('product_mod'); 
$ data ['products'] = $ this-> product_mod-> getProductList();
$ data ['versions'] = array();
foreach($ data ['products'] as $ product)
{
$ data ['versions'] [$ product ['product_id']] = $ this-& > getVersionList($ product ['product_id']);
}


解决方案

MVC或不是MVC



我应该注意的第一件事是 不可能在PHP中编写经典的MVC 。事实上,类似于MVC的PHP框架(如CodeIgniter或Yii)实现了 MVP ,其中:




  • 视图是被动的并且不知道模型

  • 演示者(控制器)更改模型状态,读取信息并将其传递给查看



=http://stackoverflow.com/users/727208/teresko>tereško



CodeIgniter方法



但是,特别是在CodeIgniter中,您有3个步骤:




  • 创建模型以查询数据库并返回数据(作为数组或对象)

  • 创建控制器以加载 (模型的一个方法),并将返回的数据传递给视图

  • 创建

  • $ b 考虑到上述方法,您需要从您的模型中的数据库中获取结果:



    application / models / product.php

      class Product extends CI_Model 
    {
    public function get_product($ product_id)
    {
    $ this-> db-> select('*') - > from('products');
    $ this-> db-> where('product_id',$ product_id);
    $ this-> db-> join('versions','versions.product_id = products.product_id');
    $ query = $ this-> db-> get();
    return $ query-> first_row('array');
    }
    }

    然后获取并传递Controller中的结果: / p>

    application / controllers / products.php

      class Products extends CI_Controller 
    {
    public function view($ product_id)
    {
    $ this-> load-&产品');
    //从数据库获取结果
    $ data ['product'] = $ this-> product-> get_product($ product_id);
    //将结果传递给视图
    $ this-> load-> view('product_view',$ data);
    }
    }

    最后,使用视图中返回的数据生成列表:



    application / views / product_view.php

      //使用$ product显示产品。 
    print_r($ product);


    I am new to MVC, I am porting a project written in non-MVC style to MVC, but I am stuck on a problem where it is necessary to call Model function in View.

    Scenario:

    Table1 - Products:
    contains product_id, product_name etc. and for each product there can be multiple versions.

    Table2 - Versions:
    contains version_id, version_name, ... , product_id etc.

    Now in the View I am displaying products and under each product heading I have to display version list of that product, in non-MVC style it was pretty simple, I can use the following code snippet in View:

    foreach ($product as $row) 
    {
        echo $row['product_name'];
        if ($main->getVersionList($vresult,$row["product_id"]))
        {
            foreach ($vresult as $vrow)
            {
              echo $vrow['version_name'];
            }
        }
    }
    

    Now, I can pass Product array from controller to view but what about each Version array which needs to be generated corresponding to each product?

    Update:

    This is my final working solution (used a map), in controller:

            $this->load->model ( 'product_mod' );
            $data ['products'] = $this->product_mod->getProductList ();
            $data ['versions'] = array ();
            foreach ( $data ['products'] as $product )
            {
                $data ['versions'] [$product['product_id']] =   $this->product_mod->getVersionList ( $product['product_id'] );
            }
    

    解决方案

    MVC or not to MVC

    The first thing I should note is that It is impossible to write classical MVC in PHP. In fact the MVC-like PHP frameworks such as CodeIgniter or Yii implements sort of MVP in which:

    • view is passive and unaware of model
    • presenter (controller) changes state of model, reads information and passes it to view

    Credits to tereško

    CodeIgniter Approach

    However, particularly in CodeIgniter, you have 3 steps:

    • Create a Model to query through the database and return the data (as an array or object)
    • Create a Controller to load and fetch the result from the Model (a method of the Model), and pass the returned data to the view
    • Create a View and use PHP loops to echo the result out, build the HTML.

    Getting all together

    Considering the above approach, you need to fetch the result from the database in your Model:

    application/models/product.php

    class Product extends CI_Model
    {
        public function get_product($product_id)
        {
            $this->db->select('*')->from('products');
            $this->db->where('product_id', $product_id);
            $this->db->join('versions', 'versions.product_id = products.product_id');
            $query=$this->db->get();
            return $query->first_row('array');
        }
    }
    

    Then fetch and pass the result within the Controller:

    application/controllers/products.php

    class Products extends CI_Controller
    {
        public function view($product_id)
        {
            $this->load->model('product');
            // Fetch the result from the database
            $data['product'] = $this->product->get_product($product_id);
            // Pass the result to the view
            $this->load->view('product_view', $data);
        }
    }
    

    Finally, use the returned data in the view, to generate the list:

    application/views/product_view.php

    // Use $product to display the product.
    print_r($product);
    

    这篇关于调用视图代码中的模型函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆