The Query Builder
In order to get the exact reservation data that we want we are going to use the Doctrine Query Builder to help us.
The Query Builder allows you to write database queries focusing on the "entities" (essentially PHP classes that represent tables in a database) rather than dealing directly with SQL. This is particularly useful because it abstracts much of the SQL handling and lets you think more in terms of PHP objects and their relationships.
When you use the Doctrine Query Builder, you refer to fields by their names as defined in the entity classes. For example, if you have an entity User
with properties id
, name
, and email
, you can build a query to select users based on these properties without writing raw SQL. The fields in the query refer to these entity properties.
The Query Builder uses a fluent interface that allows for method chaining to build queries. This makes the process of constructing a query more intuitive and readable.
Since the queries are based on entities, Doctrine handles the mapping between your PHP classes (entities) and the database tables. This encapsulates a lot of the complexity involved in CRUD (Create, Read, Update, Delete) operations.
In summary, the Doctrine Query Builder lets you write database queries in an object-oriented fashion, using the properties and relationships defined in your entity classes instead of direct SQL. This approach helps maintain a clear separation between your database structure and your application logic, promoting a more structured and maintainable codebase.
Branch: https://github.com/GaryClarke/php-api-pro/tree/51-reservations-collection-query
0 comments