【问题标题】:Calling model function in view codeigniter在视图codeigniter中调用模型函数
【发布时间】:2014-03-06 22:29:49
【问题描述】:

我是 MVC 新手,我正在将一个以非 MVC 风格编写的项目移植到 MVC,但我遇到了一个问题,即需要在 View 中调用 Model 函数。

场景:

Table1 - 产品:
包含product_idproduct_name等,每个产品可以有多个版本。

Table2 - 版本:
包含version_idversion_name、...、product_id 等。

现在在视图中我正在显示产品,并且在每个产品标题下我必须显示该产品的版本列表,在非 MVC 样式中它非常简单,我可以在视图中使用以下代码 sn-p:

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

现在,我可以将产品数组从控制器传递给视图,但是每个需要生成对应于每个产品的 Version 数组呢?

更新:

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

        $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'] );
        }

【问题讨论】:

  • 刚刚更新了答案以满足您的需求。
  • 感谢您发布您的解决方案——数组可能很难弄清楚——有趣的是它通常实现起来多么简单:-)
  • @cartalot 在循环中运行 SQL 查询会导致开销,并且在您使用 RDBMS 时没有任何意义。正如我在my answer 中建议的那样,您可以JOIN 这些表来获取相关数据。

标签: php mysql database codeigniter model-view-controller


【解决方案1】:

MVC还是不转MVC

我首先要注意的是 It is impossible to write classical MVC in PHP。事实上,类似 MVC 的 PHP 框架,例如 CodeIgniter 或 Yii 实现了sort of MVP,其中:

  • 视图是被动的,不知道模型
  • presenter(控制器)更改模型状态,读取信息并将其传递给视图

感谢tereško

CodeIgniter 方法

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

  • 创建一个模型来查询数据库并返回数据(作为数组或对象)
  • 创建一个 Controller 以从 Model(Model 的一种方法)加载获取结果 ,并将返回的数据传递给视图
  • 创建一个视图并使用 PHP 循环来回显结果,构建 HTML。

齐心协力

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

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 中获取并传递结果:

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

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

application/views/product_view.php

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

【讨论】:

  • 任何对此答案投反对票的人,请考虑在您发现这无关或无用的情况下发表评论
  • 它似乎与使用上面的 foreach 循环解决方案并没有太大的区别,因为我必须为每个产品 id 调用上面的函数,就像在上面的函数中使用以下 where 条件:$this->db- >where('用户名', $用户名);结果是一个空数组。
  • @adnankamili 我假设您只想显示一种产品。为了显示整个产品,您应该删除 WHERE 子句并改用$query->result_array()。它提高了性能,您可以使用 CI 分析器来比较基准。在这里你可以检查SQL JOIN的功能。
  • AFAIK “视图是被动的,不知道模型” 在 CodeIngniter 3.1.9 中不正确。如果在控制器中加载模型,则可以在视图中使用它。例如。在 controller 你调用 $this->load->model('mymodel'); 然后在 view (由这样的控制器加载)你可以调用 $this->mymodel->someMethod();
【解决方案2】:

你应该在你的模型中做mysql查询,例如products_model(别忘了加载它)

示例查询:这只是从产品中选择 *

public function All_Products()
{
$this->db->select('');
$result = $this->db->get('products')->result_array();
return $result;
}

所以正如我所见,你是像我一样的 foreach 人,而不是使用大量查询。

在您的控制器中,您可以将 $products 加载到您的模型中。

$products = $this->products_model->All_Products();

我的解决方案是创建一个新数组,然后将新值放入其中。此外,您需要在模型中使用 getVersionList 函数。

$newArray = array ();
foreach ( $products as $values ) {
$version = $this->products_model->getVersionList($values['product_id']);
$tmp = array (
'product_id' => $values ['product_id'], 
'product_name' => $values ['product_name'], 
'version' => $version
);
array_push ( $newArray, $tmp );
}

因此,将您的新数组添加到 $data 中,您可以将它们放在您的视图文件中。

$data ['products'] = $newArray;

对不起,我没有弄清楚你所有的问题,但我认为教如何钓鱼比教鱼更好。

【讨论】:

    【解决方案3】:

    使用下面的代码,您可以在视图文件中调用模型的方法。它对我有用,也很简单。

    $CI =& get_instance();
    $CI->model_name->method_name();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      • 2011-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      • 1970-01-01
      相关资源
      最近更新 更多