title: codeigniter join many to many LIMIT
body: <p>I have three tables:</p>
  
  <pre><code>products: model_id, model(unique),
  groups: group_id, code(unique), 
  products_groups: pg_id, model_id_fk, group_id_fk
  </code></pre>
  
  <p>products_groups table is for 'many-to-many' relationship between tables 'products' and 'groups'.</p>
  
  <p>My left join:</p>
  
  <pre><code>$this-&gt;db-&gt;select('model_id, model, code');
  $this-&gt;db-&gt;from('products');
  $this-&gt;db-&gt;join('products_group', 'products_group.model_id_fk = products.model_id', 'left');
  $this-&gt;db-&gt;join('groups', 'groups.group_id = products_groups.group_id_fk', 'left');
  $query = $this-&gt;db-&gt;get()-&gt;result_array();
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>|-------------------------|
  | model_id | model | code |
  |-------------------------| 
  | 0        | 1000  | 15   | 
  | 0        | 1000  | 20   | 
  | 0        | 1000  | 30   | 
  | 1        | 2000  | 15   | 
  | 1        | 2000  | 20   |
  |-------------------------|
  </code></pre>
  
  <p>What I want:</p>
  
  <pre><code>|-------------------------------|
  | model_id | model | code       |
  |-------------------------------|
  | 0        | 1000  | 15,20,30   |
  | 1        | 2000  | 15,20      |
  |-------------------------------|
  </code></pre>
  
  <p>Solution(?):
  The first thing that came to mind, is to do 'order by products.model_id' and then go through the results row by row, merge rows that have the same 'model_id' and concate their 'code' values.</p>
  
  <p>Problem:
  I want to do pagination and if I do a limit on the query, the merging of rows will mess up the number of rows.</p>
  
  <p>So I need suggestions, how do I do this ?</p>
  
  <p>P.S. This is only a part of the info, the 'products' table has one more 'many-to-many' relationship with a second table, 'one-to-many' relationship with a third one. Maybe one more 'many-to-many' relationship will be added.</p>