3

When I create PHP projects, is it better to put all database (e.g. MySql) queries in a single file (i.e. "queries.php")? Or is it better to place the respective queries in any "model" classes (i.e. User.php) that I may be creating? Is it bad practice to put database queries anywhere in any PHP file?

In the queries.php I would be placing functions for calling the queries.

3 Answers 3

4

I'll leave whats 'bad practice' to the bloggers to fight over.

I personally keep queries within the business-logic objects. So yes, User, Order, Sale, etc. I'd find it very inconvenient if they they were all in a separate file. That would be unnecessary added complexity and frankly kind of annoying to have to keep referring back to the file.

If for some reason you are worried about finding queries across multiple files, grep can be very helpful.

3
  • Thank you for the input. Where should I put model-less queries, then? Or is it always best practice to create a model class for each MySQL table? Commented Mar 27, 2014 at 20:32
  • 1
    Its not typical that I have a one-to-one dynamic between classes and tables like that. But in general I keep as much SQL in the business logic classes as I can. If you were to look at how I arranged things in code, my 'controllers' arent usually even aware that a database is being used. All database queries are done inside business classes. Commented Mar 27, 2014 at 20:48
  • +1 for locality of reference between a DAO and the SQL that populates/persists that DAO.
    – user22815
    Commented Apr 9, 2014 at 19:03
-1

Usually, it boils down to choice, when I work with MVC, I tend to keep separate files for all my database objects other than that I've all my database objects/model within a single file.

MVC favours the idea of separation,separate models, views and controllers from each other. Over the years i've experienced a growing pattern where individual models are kept in separate files. You can check Ext Js 4 mvc application for reference.

I've adopted this same pattern for most of my MVC based application, locating files for editing have never been easier than this and I know where exactly to find which file.

New Programmers were also able to work with the codebase, they just knew where to find what.

When not using MVC, I tend to have all my models within a single file to avoid the overhead of importing/including these files when needed.

For a bigger codebase, sticking with MVC and keeping separate files will help when it comes to maintenance and for a smaller codebase, a single file will suffice.

1
  • 5
    Programmers.SE is focused on high quality Q&A, and encourages longer answers that explain the why behind the what. Would you consider editing your answer to explain why you separate files for MVC work but use a single file for other projects?
    – user53019
    Commented Mar 31, 2014 at 11:23
-1

You can create a base Model which will have the general queries and other models can be inherited from the base model and will contain specific queries to that model.

2
  • 1
    Could you elaborate upon this? How does one create a base Model with the associated queries? As this is tagged with php, a code sample in php demonstrating this could be useful to help in the elaboration. Does this add any additional functionality that the OP should be aware of?
    – user40980
    Commented Apr 9, 2014 at 18:08
  • Base model will not have direct quires but it will have basic CRUD operation for all the models that inherit from it. For example to get records by primary function get($primary_key){ return $this->db->where($this->primary_key, $id)->get($this->_table)->row(); } Commented Apr 13, 2014 at 17:06

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.