以下のコードから期待する結果を得るのに問題があります。
最初のステップは次のとおりです。
テーブルから1つの行をフェッチし、別のテーブルから複数の行を結合します...rsult配列は次のようになります。
array(
field_1,
field_2,
field_3,
joined_array(
field_a_array(
field_a_a,
field_a_b,
field_a_c
),
field_b_array(
field_b_a,
field_b_b,
field_b_c
)
)
)
私のクエリは次のようになります。
(codigniters dbクラスにとって、where、joinなどの位置は重要ではないようです)
$this->db->select('events.*, genres_x_events.*');
$this->db->from('events');
$this->db->where('events.slug', $slug);
$this->db->where('events.deleted', 0);
$this->db->join('genres_x_events', 'genres_x_events.event_slug = events.slug');
$query = $this->db->get();
私が直面している問題は、結合ごとに複数の行を取得することを期待しているときに、結合から1行しか取得できないことです。
編集:
last_query():
SELECT `events`.*, `genres_x_events`.`genre_slug`
FROM (`events`)
LEFT OUTER JOIN `genres_x_events` ON `genres_x_events`.`event_slug` = `events`.`slug`
WHERE `events`.`slug` = 'test'
AND `events`.`deleted` = 0
出力:
Array(
[0] => Array
(
[id] => 25
[headline] => test
[subheadline] =>
[slug] => test
[date] => 2012-08-10
[start_time] => 00:00:00
[end_time] => 00:00:00
[price] =>
[body] =>
[location_id] => 5
[genre_id] => 0
[creation_date] => 2012-08-10 14:26:33
[update_date] => 2012-08-10 14:26:41
[deleted] => 0
[genre_slug] => rock
)
[1] => Array
(
[id] => 25
[headline] => test
[subheadline] =>
[slug] => test
[date] => 2012-08-10
[start_time] => 00:00:00
[end_time] => 00:00:00
[price] =>
[body] =>
[location_id] => 5
[genre_id] => 0
[creation_date] => 2012-08-10 14:26:33
[update_date] => 2012-08-10 14:26:41
[deleted] => 0
[genre_slug] => metal
)
[2] => Array
(
[id] => 25
[headline] => test
[subheadline] =>
[slug] => test
[date] => 2012-08-10
[start_time] => 00:00:00
[end_time] => 00:00:00
[price] =>
[body] =>
[location_id] => 5
[genre_id] => 0
[creation_date] => 2012-08-10 14:26:33
[update_date] => 2012-08-10 14:26:41
[deleted] => 0
[genre_slug] => indie
)
)
出力が欲しい(そのようなものはクールだろう):
Array(
[0] => Array
(
[id] => 25
[headline] => test
[subheadline] =>
[slug] => test
[date] => 2012-08-10
[start_time] => 00:00:00
[end_time] => 00:00:00
[price] =>
[body] =>
[location_id] => 5
[genre_id] => 0
[creation_date] => 2012-08-10 14:26:33
[update_date] => 2012-08-10 14:26:41
[deleted] => 0
[genres] => Array(
[0] => rock
[1] => metal
[2] => indie
)
)
)