views:

204

answers:

1

I have a data access layer that has separate classes for each table in the database. Each class makes objects that reference a row in the table, they have create, update, delete, and fetch functions. They all extend a DBObj class. My question is, say I want to write a query to SELECT * FROM table, and have that in a function. Where is the best place to put this? In the business layer or group all related functions in their respective data layer classes? Its in PHP if that matters, using MySQL.

+5  A: 

Put SQL queries in the data layer.

For a query like SELECT * FROM table, whether you make that a class method of the model itself, or a method of a model manager object, is a matter of style.

But business logic should be abstracted from the arbitrary details of the database schema. The business layer should be able to just ask the data layer for all instances of a model, without caring about the specific query that is going to be used to get that data.

Ben James