So I have the following example tables:
Table A:
id
name
type
hat_size
created
Table B:
id
name
type
favorite_food
created
Table C:
id
name
type
some_other_thing
created
Let’s say that I want to create a search with results from models A, B, and C using id, name, type, and created.
My goal would be to search all three tables, ordered by one of the fields (let’s use created as the ORDER_BY field), and limited to say, 20 results.
Normally I would simply do a Union of these, order by the field, and LIMIT the result.
Now, I know I can just create a raw query to do this… but that means that I have to rewrite the paginate methods, always sanitize, etc. Plus it’s clunky and not necessarily portable across multiple SQL databases. I’d prefer to do this a more MVC friendly way, using the models themselves, if possible.
What’s the best way I can approach this with CakePHP?
Thank you,
James
One option is to create a
VIEWwhich holds theUNIONquery. And then perform your Cake query on the view.I’m starting to avoid views recently, however, because of the difficulty in transferring them between servers (and the annoying
DEFINERcommand that is usually included in exports).