CodeIgniter Forums
How to join tables using ORM Datamapper CodeIgniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: How to join tables using ORM Datamapper CodeIgniter (/thread-60730.html)



How to join tables using ORM Datamapper CodeIgniter - El Forum - 06-16-2014

[eluser]imohammad[/eluser]
I've spent quite a few hours trying to find out a way of implementing this query:

Code:
SELECT * FROM
        (SELECT * FROM bf_snippets where id IN
        (
            SELECT snippet_id FROM bf_bookmark WHERE user_id = {$user_id}
        )) snippets_table
        JOIN
        (
            SELECT id, language FROM bf_categories
        ) categories_table
        ON snippets_table.category_id = categories_table.id
        JOIN
        (SELECT id , username FROM bf_users) users_table
        ON users_table.id = snippets_table.user_id

To ORM Datamapper in Codeigniter. I have 3 models ( user, bookmark, snippet ).

And the relationships are as follows (correct me if it's wrong):

Code:
user HAS MANY bookmark and snippet

bookmark HAS ONE user and snippet

snippet HAS MANY bookmark

snippet HAS ONE user
How to implement it using Datamapper in CI? Please note that bookmark is the relation between snippet and user.

Bookmark table has the following attributes (ID, User_ID, Snippet_ID)