Where to put "virtual tables" in MVC?
October 20, 2016 9:41 AM   Subscribe

Short version: Should ORMs only map to actual tables in the actual database, or is it useful to add classes that create "virtual tables" based on filtering the records?

Long version: Let's say you have an app where there the database has a 'users' table and a 'groups' table. There are many groups, and each one is flagged as either a Member group, a Client group, or an Admin group. Any user that belongs to a Member group is a Member, any user that belongs to a Client group is a Client, &c. (So a user could be both a Member and a Client if that user belongs to two groups.)

I'm using CakePHP at the moment, so there's a UserTable class for the ORM, and there's also a UserController class, which directs to any of several user-based views.

So let's say each user type gets its own set of CRUD-based URLs:

/member/add
/member/delete
/member/update
/member/list
/client/add
...and so forth...

My question is this: Would it generally be better to create a MemberTable class in the ORM code, have it filter the 'users' table, and have MemberController act as if there was an actual table called 'members', or to have MemberController load UserTable and do the filtering there?
posted by lore to Computers & Internet (2 answers total)
 
Best answer: It's perfectly fine to have these virtual tables - you're providing access to a set of operations, not writing a thick proxy to your database driver. So design an API that makes sense for how it's used, then use whatever abstractions make sense to make it work. By the bye, your proposed URIs make it sound like a RESTful API may suit your needs perfectly.
posted by The Gaffer at 10:12 AM on October 20, 2016


Would it generally be better to create a MemberTable class in the ORM code

Why on earth should ORM code be doing work that the database itself can do better and more easily? Just use views.
posted by flabdablet at 6:19 AM on October 21, 2016


« Older I don't want to rip off the band-aid... Without...   |   Automated WooCommerce Newer »
This thread is closed to new comments.