問題摘要: 我正在使用CodeIgniter的Active Record Class從3個表中進行連接。所有3個表都有'ID'作爲主鍵。我如何獲得正確的ID輸出?我試圖找出如何在select語句中做一個別名,但無法使其工作。CodeIgniter SELECT with 2 JOINS - 如何獲得正確的ID
的代碼:
(控制器)
public function index()
{
$data['call_reports'] = $this->call_reports_model->get_call_reports();
$data['title'] = 'All Call Reports';
$this->load->view('call_reports/templates/header', $data);
$this->load->view('call_reports/index', $data);
$this->load->view('call_reports/templates/footer');
}
(型號)
public function get_call_reports()
{
$this->db->select('*');
$this->db->from('call_reports');
$this->db->join('employees', 'employees.ID=call_reports.employee_ID');
$this->db->join('customers', 'customers.ID=call_reports.customer_ID');
$query = $this->db->get();
return $query->result_array();
}
(指數)
<?php foreach($call_reports as $call_reports_item): ?>
<p><?php echo $call_reports_item['name'] ?></p> <!—Result: name from employee table -->
<p><?php echo $call_reports_item['CUSNM'] ?></p> <!—Result: name from customer table -->
<?php echo $call_reports_item['ID'] ?> ***<!—RESULTS IN EMPLOYEE ID -->***
<?php endforeach ?>
的問題行代碼:
<?php echo $call_reports_item['ID'] ?> ***<!—RESULTS IN EMPLOYEE ID -->***
我希望這是呼叫報告ID而不是僱員ID。我該如何解決?
在此先感謝您的幫助。
您是否嘗試過使用'$ call_reports_item ['call_reports.ID']'?對數據庫中的每個主鍵使用「ID」名稱也是一個壞主意。 – Twister1002
@ Twister1002我嘗試過使用'$ call_reports_item ['call_reports.ID']'結果如下:消息:未定義的索引:call_reports.ID – JoeKincognito
'var_dump($ call_reports_item)';什麼東西在那裏? – Jessica