Building Doctrine queries with DQL and the query builder

Persistence in PHP with the Doctrine ORM

A chapter of my book Persistence in PHP with Doctrine ORM is available for free on the Pack website.

This chapter explains some interesting Doctrine concepts including DQL (the proprietary SQL-like language of the Doctrine ORM), entity repositories, the query builder and aggregate functions.

Read this free chapter on the Packt website.

Persistence in PHP with the Doctrine ORM: my new book in pre-order

Persistence in PHP with the Doctrine ORM

My new book about the Doctrine project is pre-orderable on the Packt website. It will be available in late december. A free sample chapter will be published shortly.

What you will learn from this book

  • Install Doctrine through the Composer dependency manager
  • Configure Doctrine Command Line Tools
  • Learn to manage relations between entities with different association types
  • Create data fixtures, a custom entity repository, and native SQL queries
  • Master the query builder to generate DQL queries
  • Get started with inheritance and lifecycle events

In Detail

Doctrine 2 has become the most popular modern persistence system for PHP. It can either be used as a standalone system or can be distributed with Symfony 2, and it also integrates very well with popular frameworks. It allows you to easily retrieve PHP object graphs, provides a powerful object-oriented query language called DQL, a database schema generator tool, and supports database migration. It is efficient, abstracts popular DBMS, and supports PHP 5.3 features.

Doctrine is a must-have for modern PHP applications.

Persistence in PHP with the Doctrine ORM is a practical, hands-on guide that describes the full creation process of a web application powered by Doctrine. Core features of the ORM are explained in depth and illustrated by useful, explicit, and reusable code samples.

Persistence in PHP with the Doctrine ORM explains everything you need to know to get started with Doctrine in a clear and detailed manner.

From installing the ORM through Composer to mastering advanced features such as native queries, this book is a full overview of the power of Doctrine. You will also learn a bunch of mapping annotations, create associations, and generate database schemas from PHP classes. You will also see how to write data fixtures, create custom entity repositories, and issue advanced DQL queries. Finally it will teach you to play with inheritance, write native queries, and use built-in lifecycle events. If you want to use a powerful persistence system for your PHP application, Persistence in PHP with the Doctrine ORM is the book you.

Approach

Persistence in PHP with the Doctrine ORM is a concise, fast, and focused guide to build a blog engine with advanced features such as native queries and lifecycle callbacks.

Who this book is for

This book is primarily intended for PHP developers and architects who want to increase their skills in the field of Persistence and ORM to map the data they are working on to objects they are using in programming. Basic knowledge of databases and PDO and working knowledge of PHP namespaces is a prerequisite.

Making the Sonata Project better

As a freelancer and with my company, Les-Tilleuls.coop, I built many applications relying on Sonata bundles for Symfony. I especially recommend the Admin generator and the Media manager.

As these components help me to build apps with ease and speed, it’s a fair reward to contribute back to them when I can. In addition to being the author and maintainer of the Doctrine MongoDB ODM compatibility layer of the Admin Bundle ; I’ve recently added some useful features to the Sonata Project and it’s time to give them more exposure.

ACL editor for the Admin Bundle

Sonata ACL editor

The ACL component of the Symfony framework is as powerful as complicated. The Sonata Admin bundle provides a useful integration with it allowing to restrict actions to authorized users only. What was missing was an interface to set the permissions. I’ve fixed that and this ACL editor is now provided with the Admin bundle. This is now matter of a click for an admin to give rights to users on entities.

If you want to use this feature in your project, refer to the Sonata Admin documentation that have been updated accordingly.

ACL list filtering for the admin

This is a perfect complement to the ACL editor previously described.

I’ve built a bundle called CoopTilleulsAclSonataAdminExtensionBundle (distributed separately) providing a Sonata admin extension that filter lists of entities. Only entities that the logged in user as the right to interact with are displayed.

It also works with the ACL system of the Symfony security component. The documentation is provided with the bundle.

Confirm dialog to avoid losing unsaved changes

Sonata confirm on exit

A common pitfall with web apps is that when a link is clicked, unsaved data entered in a form is loose without warning. Thanks to the integration of my small confirmExit plugin for jQuery, the Sonata Admin no longer suffers of this flaw. This feature can be disabled through a configuration parameter.

My jQuery plugin internally use the useful Window.onbeforeunload Javascript event.

Integration of Sonata Media in CKEditor

Sonata integration in CKEditor

The Sonata Media manager is the most powerful way to manage images, videos and files in Symfony powered applications. It supports YouTube, Vimeo and Dailymotion videos out of the box, image thumbnail generation and Amazon S3, FTP and CDN storage systems. It is highly extendable and is provided with a customized Sonata Admin UI.

What I missed was the ability to select and upload Media directly from CKEditor, the popular WYSYWG visual text editor. Those features is now available as a separate bundle (when I have time, I’ll move it directly in the SonataMedia bundle). The CoopTilleulsCKEditorSonataMediaBundle bundle allows to:

  • Upload an image through Sonata Media directly form the CKeditor’s image dialog
  • Select an existing Sonata Media file
  • Create a link to a media handled with Sonata through the CKeditor’s link button
  • Upload a file from the CKEditor’s link dialog

As always, the doc is included with the bundle.

I’ve also make some bug fixes on SonataAdmin, SonataMedia, SonataPage and SonataDoctrineORMAdminBundle. Hoping these enhancements will help some Symfony developers!