[英]Pass variables between functions in model
I have such function in model. 我在模型中有这样的功能。
function news()
{
$data = array(
'title' => $this->input->post('title'),
'date' => $this->input->post('date'),
'newstext' => $this->input->post('newstext'),
);
$this->db->insert('news', $data);
}
And I want use this $data['title']
in another function inside the same model. 我想在同一模型的另一个函数中使用此$data['title']
。 How can I do that? 我怎样才能做到这一点?
首先,我认为最好将输入值存储在控制器中,而不要在模型中存储以遵循MVC模式,因此您的模型中只有数据库相关的操作,然后,您可以调用该函数,并将控制器中的另一个函数传递给两个函数输入您存储在控制器中的值。
The easiest way to accomplish that: 实现此目的的最简单方法是:
Declare a global variable right after the Model Class definition. 在“模型类”定义之后声明一个全局变量。
eg 例如
class ModalName extends CI_Model()
{
public $title;
function news()
{
$data = array(
'title' => $this->input->post('title'),
'date' => $this->input->post('date'),
'newstext' => $this->input->post('newstext'),
);
$this->db->insert('news', $data);
$this->title = $data['title'];
}
}
Then , 然后 ,
$this->title
will be available in every function inside your Model Class. $this->title
将在Model类中的每个函数中可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.