刚接触 MVC/Codeigniter,但仍然不完全理解所有概念。我希望我的问题有点清楚,但这就是我想要做的:
我的应用程序基本上是一个多页表单,用户填写该表单,最后有一个他可以根据所有输入查看的报告。报表流程基本是这样的
'Create New Report' -> 'fill customer info' -> 'fill user info' -> 'Enter Data' -> 'Preview Report'
以上所有步骤本质上都是 View ,并且有相应的 Controller 和模型。
每次创建新报告时,我都会使用报告 Controller 在数据库中插入带有新报告 ID 的行:
class Reports extends CI_Controller { public function index() { //list all current existing reports //model controller simple get to return all reports in db $data['results'] = $this->report_model->get_reports(); $this->load->view('header'); $this->load->view('reports_view', $data); $this->load->view('footer'); } public function createReport() { //create new id for report when user initiates new report //Enter placeholders in reports db until further info recieved $data = array( 'customer_id' => 1, 'user_id' => 1, 'data_id' => 1 ); //will be done in model just temporarily in controller $this->db->insert('reports', $data); } }
选择“创建新报告”后,您将进入“填写客户信息” View ,这是一个包含姓名、联系人等的简单表单。客户 Controller 和模型会将信息存储到客户数据库并创建新 ID为客户。
客户 Controller :
class Customer extends CI_Controller {
public $customerData;
public function index()
{
$this->load->view('header');
$this->load->view('customer_view');
$this->load->view('footer');
}
public function saveCustomerInfo() {
$customerData = array(
'customerName' => $this->input->post('customer_name'),
'customerPhone' => $this->input->post('customer_phne'),
'customerEmail' => $this->input->post('customer_email'),
'customerAddress' => $this->input->post('customer_addr'),
'customerCity' => $this->input->post('customer_city'),
'customerstate' => $this->input->post('customer_state')
);
$output = $this->customer_model->save_customers($customerData);
}
}
- 我对接下来的两个 View 也做了同样的事情。
来自客户、用户和数据的所有信息都通过其他表中的主键链接到我的报告表,报告表基本上如下所示。
*---------------------------------------------------------*
| report_id | (fk)customer_id | (fk)user_id | (fd)data_id |
*---------------------------------------------------------*
| 1 | 4 | 7 | 3 |
| 2 | 2 | 9 | 1 |
| 3 | 1 | 1 | 6 |
我想知道如何存储当前正在进行的报告的 $report_id ,以便我可以将其传递给客户/用户/数据 Controller ,以便填写表格的这些部分并写入我可以更新报告表并用当前客户、用户和数据信息替换最初的占位符。
我不太想为此起诉 session ,所以这是最好的方法。可能是一个非常简单的答案,但我是新手,因此非常感谢您的帮助。
最佳答案
在模型中,一旦完成 $this->db->insert(...);
,您就可以获得最后一条记录的 ID 并返回它,本质上是在执行 返回 $this->db->insert_id();
如果您从 Controller 调用保存函数:
$report_id = $this->Reports_model->save(...);
你的模型代码确实如此
$this->db->insert(...);
return $this->db->insert_id();
然后您的 Controller 将获得最后插入的 ID。然后,您可以通过执行以下操作将其从 Controller 传递到您的 View :
$this->load->view('path/to/view', ['report_id' => $report_id]);
然后,您可以在 View 中使用变量$report_id
,您可以将其添加到隐藏字段。
您还可以使用 Controller 中的 $report_id 将其添加到 session 中,这样人们就无法检查 HTML 并篡改您的 ID。
关于php - CodeIgniter - 如何在整个程序中使用对象变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35776365/