We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Do I really need the Models if I'm building just an API?

I'm using Phalcon to building just a REST API and I wanted to know the advantages and disadvantages of using the Models. Is it just creating more overhead then I really need? Also if I need to write a bunch of custom queries to do joins, what is the best way to handle in Phalcon? I like the query builder, but is it just as easy to write out the queries out in phql instead?

Disadvantages of using models:

  • If your domain is not complex enough It will not be worth adding a higher layer of abstraction
  • More abstraction layers represent less control over the way in which you interact with databases
  • Less freedom to use SQL extensions provided by the database engine

Advantages of using models:

  • Tables are represented by classes and objects represent rows. This abstraction allows you to think the system in an OOP way
  • Classes can be organized in namespaces allowing you to place models in logical units: Basic\Customers, Invocing\Invoices, etc.
  • You can encapsulate logic and business rules in models, something that would be hard to do if you use plain PDO
  • If you need to make joins or select specific columns you can use PHQL
  • More security features available or built-in