I’m starting a new project and I’m about to take perhaps the most important design decision.
I’ll be working with PHP to create dynamic queries on the fly. (I want to provide the users with someting similar to the dynamic tables in MS Excel).
I have like 25 tables with about 4-6 columns each, that I’ll be joining depending on user selection (most operations will be calculating AVG, SUM, COUNT, etc depending on some filters of values that are probably 5 to 6 table joins away).
I’m thinking making all these JOINS and SELECTS on the fly as well as the GROUPS BY but it looks like it’s going to be quite complicated, so I started thinking about using views to reduce the complexity of the code that dynamicaly builds the query.
I also thought avoiding GROUPS BY in the query and calculate aggregate funcitions using loops and an array in the code.
I would like to know which of these approaches would you recommend or if you have any sugestion or tip will be greatly appreciated.
The only answer that is valid is to create your own framework for that. I’ve done that quite a few times. What you want looks more or less like a complexe report generator that generates reports on the fly but you want to create a complexe query generator with visual aids for the client.
The first thing i’d do is use a model that represents each table and offers mechanisms to describe the table fields so you can show the user the fields. Then create a linking mechanism in your models that says: if i link this table and this table, what is the JOIN that i should use.
Let your user select the models to your, columns to use and then use your models to create the query for you. It actually works well but takes quite some time to do.
Good luck