我在模型中遇到 codeigniter 事件记录的问题。
问题是我的查询只返回一个表,我的连接表丢失了吗?
$this->db->select("page.ID, page.TITLE, category.TITLE");
$this->db->from('page');
$this->db->join('category','page.ID_CATEGORY = category.ID');
$query = $this->db->get();
return $query->result_array();
最佳答案
您需要为您的查询提供唯一的别名,例如数组索引是唯一的您不能在查询中的同一索引上有两个值 TITLE
将成为包含记录的数组的索引,因此请尝试以下查询,你当前的数组形式的结果看起来像
array(
[0] => array(
['ID'] => 1,
['title'] => test page
)
[1] => ...
)
因为列名标题在你的两个连接表中是相同的,所以你不能像下面那样在同一索引上有 2 个值的数组
array(
[0] => array(
['ID'] => 1,
['title'] => test page,
['title'] => test cat /* wrong index */
)
[1] => ...
)
查询
$this->db->select("page.ID, page.TITLE AS page_title, category.TITLE AS cat_title");
$this->db->from('page');
$this->db->join('category','page.ID_CATEGORY = category.ID');
$query = $this->db->get();
所以结果数组看起来像
array(
[0] => array(
['ID'] => 1,
['page_title'] => test page,
['cat_title'] => test cat,
)
[1] => ...
)
关于php - Codeigniter - join() 只返回一个 "first table",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23995668/