1

I am not sure what keywords to use to search for this answer, its simple but nothing found.

Model are used for sql queries as far as I know. No logic.

So then what about filters?

For example

function getItems($partnerUserId) {
    $param = "";
    $params = array();
    if ($partnerUserId !== '') {
        $param = "AND z.x = ?";
        $params[] = $partnerUserId;
    }

    $sql = "SELECT ...
      FROM z
      WHERE z.a = 1
      $param";

     return DB::connection($connection)->select($sql, $params); 
}

And in real world example there get much more those statements. Is this how model should be or I should do this logic in controller and then pass filter strings as parameters to the model function?

tereško
  • 58,060
  • 25
  • 98
  • 150
Dariux
  • 3,953
  • 9
  • 43
  • 69
  • Construction of SQL queries should happen in the specific [data mapper](http://martinfowler.com/eaaCatalog/dataMapper.html), which is responsible for persistence of a particular domain object within the model layer. – tereško Oct 13 '14 at 15:57
  • Ok, some frameworks, like PHP laravel comes without datamaper as I understood from this: http://stackoverflow.com/questions/16923350/laravel-4-with-data-mapper so then framework creators assume that it is not necessary. So how then - without installing additional plugins? – Dariux Oct 13 '14 at 16:01
  • You do not need to use libraries to implement data mapper pattern. And frameworks have nothing to do with this. – tereško Oct 13 '14 at 16:31

1 Answers1

0

this is basic layered architecture , construction of query should be done in data mapper or dao classes. if it is some redundant code or complex code, you can create util with static method and call from data mapper layer.

Panther
  • 3,312
  • 9
  • 27
  • 50