1

好吧,这里必须遗漏一些东西。

我的 SQL 工作正常:

SELECT t1.id, t1.material, t2.id, t2.material, t3.id, t3.material, t4.id, t4.material
FROM ml_levels t1 

LEFT JOIN ml_levels t2 
ON t1.parentID = t2.id

LEFT JOIN ml_levels t3
ON t2.parentID = t3.id

LEFT JOIN ml_levels t4
ON t3.parentID = t4.id

WHERE t1.id = 286

这返回

id   |  material  | id  |  material         |  id  |  material            | id |  material
-------------------------------------------------------------------------------------------
286  |  4Layer    | 209 |  Protective Film  |  60  |  Specialty Products  | 1  |  Protect 

但是,我的 ActiveRecord 只返回 t4.id 和 t4.material:

    $this->db->select('t1.id, t1.material, t2.id, t2.material, t3.id, t3.material, t4.id, t4.material');
    $this->db->from('ml_levels AS t1')->where('t1.id',286);
    $this->db->join('ml_levels AS t2','t1.parentID = t2.id','left');
    $this->db->join('ml_levels AS t3','t2.parentID = t3.id','left');
    $this->db->join('ml_levels AS t4','t3.parentID = t4.id','left');

    return $this->db->get()->row();

这返回:

id   |  material  |
-------------------
1    |  Protect   |

$this->db->last_query() 返回:

SELECT `t1`.`id`, `t1`.`material`, `t2`.`id`, `t2`.`material`, `t3`.`id`, `t3`.`material`, `t4`.`id`, `t4`.`material` 
FROM (`ml_levels` AS t1) 
LEFT JOIN `ml_levels` AS t2 ON `t1`.`parentID` = `t2`.`id` 
LEFT JOIN `ml_levels` AS t3 ON `t2`.`parentID` = `t3`.`id` 
LEFT JOIN `ml_levels` AS t4 ON `t3`.`parentID` = `t4`.`id` 
WHERE `t1`.`id` = 286 

如果我在数据库上运行这个 SQL,它会准确地返回原始查询返回的内容。

4

1 回答 1

1

Codeigniter 仅返回没有表标签的字段名称,因此您只找到两个字段,因为只有idmaterial。换线:

 $this->db->select('t1.id, t1.material, t2.id, t2.material, t3.id, t3.material, t4.id, t4.material'); 

和:

 $this->db->select('t1.id AS t1id, t1.material AS t1material, t2.id AS t2id, t2.material AS t2material, t3.id AS t3id, t3.material AS t3material, t4.id AS t4id, t4.material AS t4material'); 
于 2012-08-15T21:40:53.047 回答