Welcome Guest, Not a member yet? Register   Sign In
Active record class join multiple results from one table to multiple results from another
#1

[eluser]Dandy_andy[/eluser]
I am trying to use the active record class to perform a join involving a multiple results from one table and multiple results from another. Basically, I have two tables; one called 'content_events' and the other 'loc_keywords'. The event_id in the main table (content_events) has a keyword reference in another table, but there can be more than one keyword reference assigned to each event_id. I have tried to explain this in the tables below where the content_event.event_id of 2 has 3 possible matches in the keywords table. My question is, how can I perform a search to return results for multiple id's and also include the keywords associated with each content_event.event_id? Is there a simple way to do this using the active record class join? My current attempt (shown below) is returning just the first keyword value per row result so I'm part way there, but just need to combine the keywords into an array somehow. Can I do this without using a php loop for each result?

Code:
$this->db->select('content_events.event_id, content_events.field1, content_events.field2, loc_keywords.keyword');
  $this->db->from('content_events');
  $this->db->join('loc_keywords', 'loc_keywords.event_id = content_events.event_id', 'left');
  $this->db->order_by('created', 'DESC');
  $this->db->limit(5);
  $query = $this->db->get();
  print_r($query->result_array()); exit();

Code:
CONTENT EVENTS
event_id  |  field1      |  field 2
----------------------------
1      |   value1_1    |  value 1_2
2      |   value2_1    |  value 2_2
3      |   value3_1    |  value 3_2
4      |   value4_1    |  value 4_2

Code:
LOC_KEYWORD
event_id  |  keyword
----------------------------
1         |  apple
2         |  pear
2         |  pineapple
2         |  strawberry
3         |  peach
2         |  banana
I'm not fully clued up with MYSQL queries and the active record class seems to make the job for me a lot easier. Any help would be greatly appreciated.
#2

[eluser]Dandy_andy[/eluser]
SOLVED!...

I managed to solve this myself with some very basic syntax and it works. For anyone who is interested and may have the same problem, this is what I did...

Code:
$this->db->select('content_events.event_id, content_events.field1, content_events.field2, loc_keywords.keyword');
  $this->db->from('content_events');
  $this->db->join('loc_keywords', 'loc_keywords.event_id = content_events.event_id', 'left');
  $this->db->select('GROUP_CONCAT(loc_keywords.keyword) AS keywordlist');
  $this->db->group_by('content_events.event_id');
  $this->db->order_by('created', 'DESC');
  $this->db->limit(5);
  $query = $this->db->get();
  print_r($query->result_array()); exit();

I added the GROUP_CONCAT using the active query SELECT and then used the GROUP_BY query to ensure the results where grouped by ID row. It's taken me some time to figure out but it works and the syntax is a lot simpler than some of the replies I have been getting from other sites.




Theme © iAndrew 2016 - Forum software by © MyBB