many-to-many relationship in CI (not using ORM)
Posted
by
Ross
on Stack Overflow
See other posts from Stack Overflow
or by Ross
Published on 2011-01-04T13:51:05Z
Indexed on
2011/01/04
13:54 UTC
Read the original article
Hit count: 105
I'm implementing a categories system in my CI app and trying to work out the best way of working with many to many relationships.
I'm not using an ORM at this stage, but could use say Doctrine if necessary.
Each entry may have multiple categories.
I have three tables (simplified)
Entries: entryID, entryName
Categories: categoryID, categoryname
Entry_Category: entryID, categoryID
my CI code returns a record set like this:
entryID, entryName, categoryID, categoryName
but, as expected with Many-to-Many relationships, each "entry" is repeated for each "category".
What would the best way to "group" the categories so that when I output the results, I am left with something like:
Entry Name
Appears in Category: Foo, Bar
rather than:
Entry Name
Appears in Category: Foo
Entry Name
Appears in Category: Bar
I believe the option is to track if the post ID matches a previous entry, and if so, store the respective category, and output it as one, rather than several, but am unsure of how to do this in CI.
thanks for any pointers (I appreciate this is may be a vague/complex question without a better knowledge of the system).
© Stack Overflow or respective owner