2

I have a PHP MVC application. My 'M' has Domain, Mapper and Factory layers, and these are accessed via a Service layer.

Often I need to generate a list of id/value pairs for a html select box. To date, I have put the queries in the Mappers and returned simple array, but this doesn't feel right. The Mappers are for storing and retrieving objects, right?

I don't really want the overhead of retrieving a collection of objects, only to discard most of their goodness to produce a simple array. I was thinking about moving the queries to a Service, perhaps even having a service dedicated to lookups? This would mean I have SQL code in two places in my application, but it feels neater.

Is there a better option?

DatsunBing
  • 8,684
  • 17
  • 87
  • 172
  • 1
    This might be what you are looking for: http://stackoverflow.com/a/11943107/727208 . The bottom line - retrieval of single entry should be handled differently then same operation for list/collection of entries. I'm also starting to think, that there might be some approach to implement this using repositories & units of work, but I have not figured it out yes .. just thinking out loud. – tereško Jul 15 '13 at 11:38

1 Answers1

0

Since you will be writing a service layer for filtering out the correct collection of records while fetching them, call the service functions directly from the Controller. And the service will call the mapper function for retrieving and storing the records.

e.g Controller -> Service -> Mapper

This way the code structure would be kept neat and you would be also following the DRY (Don't Repeat Yourself) principle; there would be no need for having your SQL queries at two places.

dhrumilap
  • 142
  • 1
  • 17