PHP & Codeigniter - 如何将参数传递给模型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1149098/
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
PHP & Codeigniter - how to pass parameters to a model?
提问by oym
I am using the following code to initialize a model from within my controller:
我正在使用以下代码从我的控制器中初始化模型:
$this->load->model('model_name');
Is it possible to modify the above line somehow so that the model constructor recieves a parameter? I want to use the following code in the model constructor:
是否可以以某种方式修改上面的行,以便模型构造函数接收参数?我想在模型构造函数中使用以下代码:
function __construct($param_var) {
parent::Model();
$this->$param_var = $param_var; //I'm not even sure this works in PHP..but different issue
}
This would be very helpful so that I can reuse my model classes. Thanks.
这将非常有用,这样我就可以重用我的模型类。谢谢。
UPDATE: (from one of the answers, my original question is solved..thanks!) Just to explain why I wanted to do this: the idea is to be able to reuse a model class. So basically to give a simple example I would like to be able to pass an "order_by" variable to the model class so that I can reuse the logic in the model class (and dynamically change the order-by value in the sql) without having to create a separate class or a separate function.
更新:(从一个答案中,我原来的问题已经解决了……谢谢!)只是为了解释我为什么要这样做:这个想法是为了能够重用模型类。所以基本上举一个简单的例子,我希望能够将“order_by”变量传递给模型类,以便我可以重用模型类中的逻辑(并动态更改 sql 中的 order-by 值)而无需创建一个单独的类或一个单独的函数。
Is this poor design? If so could you please explain why you wouldn't do something like this and how you would do it instead?
这是糟糕的设计吗?如果是这样,你能解释一下为什么你不会做这样的事情以及你会如何做吗?
采纳答案by Domenic
I see your reasoning for this, but may I suggest looking at Object-Relational Mappingfor your database needs. There is a user-made ORM library for CodeIgniter called DataMapperthat I've been using lately. You can use tables in your controllers as objects, and it may be a better fit for your problem.
我明白您对此的推理,但我建议您查看对象关系映射以满足您的数据库需求。我最近一直在使用一个名为DataMapper 的用于 CodeIgniter 的用户制作的 ORM 库。您可以将控制器中的表格用作对象,它可能更适合您的问题。
回答by jimyi
You can't pass parameters through the loadfunction. You'll have to do something like:
您不能通过load函数传递参数。您必须执行以下操作:
$this->load->model('model_name');
$this->model_name->my_constructor('stuff');
In the model:
在模型中:
function my_constructor($param_var) {
...
}
Response to update:
响应更新:
You could just pass the order_by value when you're calling your model function. I'm assuming in your controller action, you have something like $this->model_name->get($my_id);Just add your order_by parameter to this function. IMO this makes your model logic more flexible/reusable because the way you were doing it, I assume setting order_by in the constructor will set the order_by value for every function.
您可以在调用模型函数时传递 order_by 值。我假设在您的控制器操作中,您有类似的内容,$this->model_name->get($my_id);只需将您的 order_by 参数添加到此函数即可。IMO 这使您的模型逻辑更加灵活/可重用,因为您这样做的方式,我假设在构造函数中设置 order_by 将为每个函数设置 order_by 值。
回答by vinothvetrivel
Instead of using DataMapperi suggested to use IgnitedRecordbecause that the DataMapper is no longer maintained more over it has been replaced into Ruby
而不是使用的DataMapper我建议使用IgnitedRecord因为该DataMapper的不再保持更多的在它已经被替换成红宝石
回答by antelove
In model
在模型中
<?php
/* Load Model core model */
/* BASEPATH = D:\xampp\htdocs\ci_name_project\system\ */
include BASEPATH . 'core\Model.php';
class User_model extends CI_Model {
/* Properties */
private $name;
/* Constructor parameter overload */
public function __construct($name) {
$this->set_name($name);
}
/* Set */
public function set_name($name) {
$this->name = $name;
}
/* Get */
public function get_name() {
return $this->name;
}
}
in controller
在控制器中
<?php
class User_controller extends CI_Controller {
public function index() {
/* Load User_model model */
/* APPPATH = D:\xampp\htdocs\ci_name_project\application\ */
include APPPATH . 'models\User_model.php';
$name = 'love';
/* Create $object_user object of User_model class */
$object_user = new User_model($name);
echo $object_user->get_name(); // love
}
}

