Coming from a Drupal background, I am quite thrilled at the idea of working with drupal 8 and symfony proper OOP and MVC practice. Right now I I am building a module with a service and a controller and I am at a loss regarding where to put my data fetching logic (that should be used by both). Understanding MVC policy I should put it in some kind of "model" structure but I can't find anything about that in Drupal documentation (sadly a bit lacking at the moment). So what is the Drupal way for this?

  • 1
    Start point for understand database layer in Drupal 8 - api.drupal.org/api/drupal/… There is a buch of module example that can help you - drupal.org/project/examples
    – Vagner
    Feb 26 '16 at 13:40
  • You may not even need a data fetching code because you could re-use the existing entity manager service to load entities.
    – mradcliffe
    Feb 26 '16 at 14:29
  • I do use entityQuery (and sql query) to fetch data but to me it is data fetching code.
    – Thony
    Feb 26 '16 at 16:54
  • As mradclif pointed out, use a service(custom or from core).
    – user21641
    Feb 27 '16 at 10:29

Your data-fetching logic should be in its own service(s).

Symfony and by extension Drupal are not traditional implementations of the MVC architecture as there's no separate Model layer, like for instance in CodeIgniter.

One of the most important features in Symfony and Drupal are services, which are (or should be :p ) independent and reusable implementations of various functionalities.

So if you want data-fetching logic, then you can create your own service that does that and use that service in your controller, instead of polluting it with the queries (entity queries or otherwise).

I'll leave with a comment from Fabien Potencier (the creator of Symfony):

"It's up to you to create your model by hand or use any other tool, like an ORM" [...]I don't like MVC because that's not how the web works. Symfony2 is an HTTP framework; it is a Request/Response framework."

This statement might be debatable, but what I want to show here is the intent behind the choice of leaving out a well defined Model layer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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