Jakub
Jakub

Reputation: 539

Codeigniter - db -> join () double results merger

I am building a NOTE system.

I am having multiple tables (notes, tags, ...) and I am joining tags to my main notes with $this -> db -> join(); to get everything into one object.

When there are 2 or more tags for one note, then I get two or more rows with only different TAGS. The rest is the same. I need to merge it to have only one note entry.

$this -> db -> where ('user', USER_ID);
$this -> db -> join ('tags', 'tags.note_id = note.id', 'inner');
$query = $this->db->get('notes');

There may be also other tables with same character as TAGS, for example places. There may be more than one place for a note.

How do I proceed from now? I would like to have one object NOTE with parameters such as note_id, note_text, and join TAGS to it and probably if more than ONE tag, then OBJECT PARAMETER = ARRAY containing all the NOTES.

  1. How to achieve that?
  2. Is that actually good idea for further development to have it in one object? Or should I go foreach and list all the tags for each of the rows?
  3. When somebody is filtering according to the tags, where & how should I store one's filtering? I am so far using $this -> session;

Thank you, Jakub

Upvotes: 0

Views: 2531

Answers (1)

tonyjmnz
tonyjmnz

Reputation: 536

You might want to use MySQL's GROUP_CONCAT()

$this->db->select('n.*, GROUP_CONCAT(t.name)', false)
         ->from('notes n')->join('tags t', 't.note_id = n.id', 'inner')
         ->where('n.user', USER_ID)->get();

I used t.name but whatever the field name it is, you get the point.

Upvotes: 1

Related Questions