Michaël Perrin

Sustainable web development.

Small new additions to Doctrine 2.5

As Doctrine ORM 2.5 was released very recently, I'd like to introduce two small contributions I made to this project that can be useful.

Accept collections as argument

PR: https://github.com/doctrine/doctrine2/pull/590

This is a very simple improvement that should make your Symfony search forms easier to implement!

You are already probably used to setting parameters directly like this:

$queryBuilder = $this
    ->where('model.category = :category')
    ->setParameter('category', $category)

In this example, the $category variable is an object. Doctrine automatically converts it to its primary key when it builds the query so that a query like category.id = :category_id is run.

But that was not possible for array collections, for WHERE IN clauses.

Before Doctrine 2.5:

// First explicit solution
$categoryIds = [];

foreach ($categories as $category) {  
    $categoryIds[] = $category->getId();

// Or, in a more concise way
$categoryIds = array_map(function($category) { return $category->getId(); }, $categories);

$queryBuilder = $this
    ->where('model.category IN (:category_ids)')
    ->setParameter('category_ids', $categoryIds)

As of Doctrine 2.5:

$queryBuilder = $this
    ->where('model.category IN (:categories)')
    ->setParameter('categories', $categories)

This is much easier to read and to write.

Better database schema handling

PR: https://github.com/doctrine/doctrine2/pull/881

Doctrine emulates database schemas (supported in PostgreSQL for instance) for database systems that don't support schemas (like SQLite).

This allows a better interoperability between database systems as it will be able to create schemas according to the mapping your have defined whatever the database system you chose.

That's fine, but it wasn't working properly in Doctrine 2.4. Defining schemas in your mapping was leading to several blocking errors for RDBMS like SQLite.

The PR solves these problems and also introduces an explicit way to define schemas for your entities if you are using the YAML format (the YAML format was not on par with annotations). Here is an example of this schema definition:

    type: entity
    table: mytable
    schema: myschema

Michaël Perrin

Read more posts by this author.

comments powered by Disqus