Reputation: 539
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.
Thank you, Jakub
Upvotes: 0
Views: 2531
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