php CodeIgniter 在同一个控制器中加载多个模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10909697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter load multiple model in same controller
提问by user1033600
These is the controller
这些是控制器
class Dashboard extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model("admin/post_model");
$this->load->model("admin/comment_model");
}
public function index(){
$data['post_res'] = $this->post_model->getPost();
$data['com_res'] = $this->post_model->getComments();
}
}
I cannot load 2 model in the same controller. It gives me an error
我无法在同一个控制器中加载 2 个模型。它给了我一个错误
Fatal error: Call to a member function getComments() on a non-object in C:\xampp\htdocs\blog\application\controllers\ram-admin\dashboard.php on line 13
How can I possibly load the models?
我怎么可能加载模型?
Thanks you so much in advance!
非常感谢你!
回答by Query Master
Try this
尝试这个
class Dashboard extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model("admin/post_model","post_model");
$this->load->model("admin/comment_model","comment_model");
}
public function index(){
$data['post_res'] = $this->post_model->getPost();
$data['com_res'] = $this->comment_model->getComments();
}
回答by Faizan Khattak
Check if the model correctly extends the CI_Modelfor comment_modeland post_model
检查模型是否正确扩展了CI_Modelforcomment_model和post_model
example:
例子:
class comment_model extends CI_Model{
}
class post_model extends CI_Model{
}
回答by Rooneyl
getComments() is comment_model, not post_model..
getComments() 是comment_model,而不是 post_model ..
You can name your models by passing a second parameter;
您可以通过传递第二个参数来命名模型;
$this->load->model('admin/comment_model', 'comments');
$data['com_res'] = $this->comments->getComments();
回答by user1033600
This is weird
这很奇怪
I just put this line of code
我只是把这行代码
$this->load->model("admin/comment_model","comment_model");
before this one
在这之前
$this->load->model("admin/page_model","page_model");
And it works fine now :)
现在它工作正常:)
Thank you for all the response!
谢谢大家的回复!
回答by Rohan Patil
For multiple models, you can do this:
对于多个模型,您可以这样做:
$models = array(
'menu_model' => 'mmodel',
'user_model' => 'umodel',
'admin_model' => 'amodel',
);
foreach ($models as $file => $object_name)
{
$this->load->model($file, $object_name);
}
But as mentioned, you can create file application/core/MY_Loader.php and write your own method for loading models. I think this might work (not tested):
但如前所述,您可以创建文件 application/core/MY_Loader.php 并编写自己的加载模型的方法。我认为这可能有效(未测试):
class MY_Loader extends CI_Loader {
function model($model, $name = '', $db_conn = FALSE)
{
if (is_array($model))
{
foreach ($model as $file => $object_name)
{
// Linear array was passed, be backwards compatible.
// CI already allows loading models as arrays, but does
// not accept the model name param, just the file name
if ( ! is_string($file))
{
$file = $object_name;
$object_name = NULL;
}
parent::model($file, $object_name);
}
return;
}
// Call the default method otherwise
parent::model($model, $name, $db_conn);
}
}
Usage with our variable from above:
与上面的变量一起使用:
$this->load->model($models);
You could also allow a separate DB connection to be passed in an array, but then you'd need to have a multidimensional array, and not the simple one we used. It's not too often you'll need to do that anyways.
您还可以允许在数组中传递一个单独的数据库连接,但是您需要一个多维数组,而不是我们使用的简单数组。无论如何,您不需要经常这样做。
回答by Niranjan Sahoo
Just use the model names in an array like above.
只需在像上面这样的数组中使用模型名称。
$this->load->model(array("admin/post_model", "admin/comment_model"));

