简体   繁体   English

CodeIgniter:在每个库中加载模型,或者将模型从控制器内部“传递”到库

[英]CodeIgniter: load model in each library or kind of 'pass' the model from within the controller to a library

im currently working with CodeIgniter. 我目前正在使用CodeIgniter。 My Front Controller loads 1 of 2 Library depending on the input he gets. 我的Front Controller会根据他获得的输入加载2个库中的1个。 Both of the librarys need access to the model. 这两个库都需要访问模型。 My question: Should I load the model in each library (plus get_instance() of CI in every Library to even get access to the CI load func) or is there any "better" solution to load the model in the Frontcontroller und 'pass' the model to either one or the other library. 我的问题:我应该在每个库中加载模型(在每个库中加上CI的get_instance()甚至可以访问CI加载函数),还是有任何“更好”的解决方案在Frontcontroller中加载模型并“通过”模型到一个或另一个库。 I even check the autoload possibilty but even then I dont have direct access from within the librarys. 我什至检查了自动加载的可能性,但即使如此,我也没有从库中直接访问。 I guess it isnt the best solution to load the model in every library (like, what if i need more libraries and so on) , but im kind of stuck right now.. :-( 我猜这不是在每个库中加载模型的最佳解决方案(例如,如果我需要更多的库等等),但是我现在卡住了.. :-(
In a nutshell: 简而言之:
- load the model in the FrontController and maybe "pass" the model as an argument to any library I need? -将模型加载到FrontController中,并可能将该模型作为参数传递给我需要的任何库?
- or get the instance of CI within any library und then load the model (and write redundant code) -或在任何库中获取CI的实例,然后加载模型(并编写冗余代码)
- or is there even a way better solution? -还是有更好的解决方案?

thx in advance :-) 提前谢谢:-)

edit: 编辑:

I go with: 我去:
public function __construct() 公共功能__construct()
{ {
$this->ci_instance =& get_instance(); $ this-> ci_instance =&get_instance();
$this->ci_instance->load->model('my_model'); $ this-> ci_instance-> load-> model('my_model');
$this->_model = $this->ci_instance->my_model; $ this-> _ model = $ this-> ci_instance-> my_model;
} }

and then use the functions like this: 然后使用如下功能:
$mysql_result = $this->_model->get_testdata(); $ mysql_result = $ this-> _ model-> get_testdata();

It is a little bit shorter than the solution from the tank auth lib (but almost the same). 它比tank auth lib中的解决方案要短一些(但几乎相同)。 :-) But the Tank Auth Lib helped me too (to see how the pros do it). :-)但是Tank Auth Lib也帮了我(看看专业人士是如何做到的)。 Thx for that too dm03514! 太感谢了dm03514!

get_instance is a native way suggested by official docs: get_instance是官方文档建议的本地方法:

for easier access to model object I would define a property in your library: 为了更轻松地访问模型对象,我将在您的库中定义一个属性:

public $_model = FALSE;

somewhere in your library consructor: 库管理器中的某个位置:

$CI =& get_instance();
$CI->load->model('model_name');
$this->_model = $CI->model_name;

then you can access the model in any library method by calling $this->_model->method(); 然后您可以通过调用$this->_model->method();在任何库方法中访问模型$this->_model->method();

Whenever I have big questions like this I usually look at some popular libraries, such as Tank Auth . 每当遇到这样的大问题时,我通常都会查看一些流行的库,例如Tank Auth Libraries/models are loaded within the class. 库/模型被加载到该类中。 This makes your library self sufficient and encolsed, which is not dependent on your intiliaztion paramters. 这使您的库自给自足且充满凝聚力,而这并不依赖于您的初始化参数。

Reloading a library after it is already loaded is just a conditional in Loader.php and is not that performance heavy. 在库已经加载后重新加载库只是Loader.php一个条件,并不是那么重的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM