Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

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.

share|improve this answer
    
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. –  Snowman Apr 9 '14 at 19:03

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.

share|improve this answer
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? –  GlenH7 Mar 31 '14 at 11:23

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.

share|improve this answer
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? –  MichaelT 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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