Doctrine QueryBuilder table alias

The Doctrine project, best known for its Object Relational Mapper, also includes a database abstraction layer, used by the ORM. This abstraction layer is called DBAL, for DataBase Abstraction Layer.

Quickly after I started using DBAL in some Wikibase components, I got annoyed at how single table single table selects needed to be created. The QueryBuilder has a “from” method, in which one needs to specify the table name. The annoying bit is that you also had to specify a table alias, and then use this alias in the select and where calls as well.

dbal-old

In an hour of free time and several commits I made the parameter optional, enabling one to do the same without having to specify the alias:

dbal-new

This change will be part of the upcoming Doctrine DBAL 2.5.

Jeroen De Dauw is founder and CEO of Professional Wiki and builds NeoWiki. More about me. Reply by email or on Mastodon, Bluesky or X.

2 comments on “Doctrine QueryBuilder table alias”

  1. I just had question, in your second block code us selected id and name what if you wanted to select all the columns what parameter would u pass the select method. With an alias you just sent the alias u

    1. Rather than doing

      ->select(‘u.*’)->from(‘users’, ‘u’)

      You can instead do

      ->select(‘users.*’)->from(‘users’)

      I’ve added some extra tests to verify this works fine.

Comments are closed.