CodeIgniter模型:仅用于数据库连接? [英] CodeIgniter Models: only for database connection?

查看:125
本文介绍了CodeIgniter模型:仅用于数据库连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我刚刚加入了一家公司。他们使用CodeIgniter PHP和在模型函数内部,只允许数据库查询。循环和其他用法不允许这样做。

Recently, i just joined a company. They are using CodeIgniter PHP and inside the model functions, only database queries are allowed. Loopings and other usages are not allowed to do that.

它不同于我所知道的。
应该是这样吗?

It's different from what i know. Should it be like that?

推荐答案

好的是和否,模型的主要作用是抽象数据库层到框架,所以理论上来说,你将只使用模型来查询数据库(使用活动记录类或本机SQL)并将结果返回到控制器,但是在模型中看到一些类似的代码是不常见的/ p>

Well yes and no, the main role of models is to abstract the database layer to the framework, so theoretically speaking, you would only use models to query the database (with active record class or native SQL) and return the result to the controller ,BUT it is not uncommon to see some code like this in a model

<?php
//birds_model.php (Array of Strings)
class Birds_model extends CI_Model{
  function get_bird($q){
    $this->db->select('bird');
    $this->db->like('bird', $q);
    $query = $this->db->get('birds');
    if($query->num_rows > 0){
      foreach ($query->result_array() as $row){
        $row_set[] = htmlentities(stripslashes($row['bird'])); //build an array
      }
      echo json_encode($row_set); //format the array into json data for the ajax call 
    }
  }
} 


b $ b

代码ref: jquery -ui-auto-complete-in-codeigniter-with-database

说的也是,CodeIgniter有一个相当宽松的方法MVC,因为模型不是必需的。

that being said, also CodeIgniter has a fairly loose approach to MVC since Models are not required.

如果您不需要添加分隔,或发现维护模型需要更复杂,您可以忽略它们,并使用控制器和视图最小化构建您的应用程序。

If you don't need the added separation, or find that maintaining models requires more complexity than you want, you can ignore them and build your application minimally using Controllers and Views.

CodeIgniter还使您能够整合自己现有的脚本,甚至为系统开发核心库,使您能够以最有意义的方式工作。

CodeIgniter also enables you to incorporate your own existing scripts, or even develop core libraries for the system, enabling you to work in a way that makes the most sense to you.

参考文献: CodeIgniter用户指南

我希望能帮助您理解codeigniter背后的逻辑,你会习惯它,只是为了让你知道opencart例如是使用一个非常相似的MVC框架,所以学习codeigniter是非常好的扩大你的MVC开发人员的技能。

I hope that helps you understanding the logic behind codeigniter and you would get used to it ,and just to let you know that opencart for example is using a very similar MVC framework , so learning codeigniter is really nice for broadening your MVC developer skills.

干杯!

这篇关于CodeIgniter模型:仅用于数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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