[英]Codeigniter - load a model in my library
I'm about to write my own library in codeigniter to check if the logged in user is an admin or not. 我将在codeigniter中编写我自己的库,以检查登录用户是否为管理员。 To do that I need to compare a value with the value of the typeAccount in the DB.
为此,我需要将一个值与数据库中typeAccount的值进行比较。 Because I'm still learning the MVC pattern I had a question about this before starting to write my library.
因为我仍在学习MVC模式,所以在开始编写我的库之前有一个问题。 Can I load a model in my library?
我可以在库中加载模型吗? Or should I communicate directly to my DB in my library?
还是应该直接与库中的数据库通信? Or is there a better way to approach this?
还是有更好的方法来解决这个问题?
Yes, you can load your model to into your library, simply add CI. 是的,您可以将模型加载到库中,只需添加CI。
class Validator
{
private $CI = null;
function __construct()
{
$this->CI =& get_instance();
}
public function validate_merchantaccount_status($param)
{
//Code Here
$this->CI->load->model('merchantaccount_model');
$res_merchant = $this->CI->merchantaccount_model->get_list($param);
}
}
Dont forget to make your model. 不要忘记制作模型。
Since the library is some kind of logic, you can see it as a piece of the Controller. 由于库是某种逻辑,因此您可以将其视为Controller的一部分。 The Controller usually just loads a Model to use it.
控制器通常只是加载一个模型以使用它。
So yes just load the Model from CodeIgniter instead of connecting to the database yourself. 因此,是的,仅从CodeIgniter加载模型,而不是自己连接数据库。
Make you models 让你成为模特
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class My_model extends CI_Model
{
/// Here the code
}
And then in controller use 然后在控制器中使用
$this->load->model('my_model');
Ant i think that is best approach :) 蚂蚁,我认为这是最好的方法:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.