2

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.

closed as primarily opinion-based by user22815, user53019, Ixrec Nov 11 '15 at 23:42

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

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.

  • 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? – user2225945 Mar 27 '14 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. – GrandmasterB Mar 27 '14 at 20:48
  • +1 for locality of reference between a DAO and the SQL that populates/persists that DAO. – user22815 Apr 9 '14 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.

  • 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 Mar 31 '14 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.

  • 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 Apr 9 '14 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(); } – Mohsin Inayat Khan Apr 13 '14 at 17:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.