[英]Passing variables from Model to controller
I have variables that are created and used on my model that I need to be able to use on my controller how is that accomplished?我有在我的 model 上创建和使用的变量,我需要能够在我的 controller 上使用这些变量是如何实现的?
Edit:编辑:
Controller: http://pastebin.com/jhAwAVa6 Controller: http://pastebin.com/jhAwAVa6
Model: http://pastebin.com/9xXRyYAa Model: http://pastebin.com/9xXRyYAa
It's unclear from your question, what exactly you want to do.从您的问题中不清楚,您到底想做什么。
If it is about accessing model properties, the right way is using accessor methods:如果是关于访问 model 属性,正确的方法是使用访问器方法:
class Model extends CI_Model{
private $name;
public function getName() {return $this->name; /*any other logic here*/}
public function setName($value) {$this->name= $value; /*any other logic here*/}
}
You can not pass the variable from a model to controller.您不能将变量从 model 传递到 controller。 You can access public variables of a model through a controller.
您可以通过 controller 访问 model 的公共变量。
echo $this->model_name->variable_name;
Model (my_model) Model (my_model)
function useful_info()
{
$data = new stdClass();
$q = $this->db->get('users');
$data->users = $this->db->result();
$data->date = date('Y-m-d');
$data->info = array('whatever','more','anything');
return $data;
}
Controller Controller
function index()
{
$info = $this->my_model->useful_info();
foreach($info->users as $user)
{
echo $user->id;
}
echo $info->date;
if($info->info[0] == 'whatever')
{
// do something
}
}
You don't have to create an object (it can be a string, T/F, array, etc), but you usually need to return something from your model and library functions.您不必创建 object(它可以是字符串、T/F、数组等),但您通常需要从 model 和库函数返回一些内容。 And you can access what you return by returning it to a variable
$info = $this->my_model->useful_info();
您可以通过将其返回到变量
$info = $this->my_model->useful_info();
来访问返回的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.