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

idCodeigniterは、テーブルラベルなしのフィールド名のみを返すため、とだけがあるため、2つのフィールドしか見つかりませんでしmaterialた。行を変更します。

 $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 に答える