Various tips and tricks¶
Use Find usages of PhpStorm for examples! The source code of the Core is a great way to learn how specific methods of the API are used. In PhpStorm it is extremely helpful to right-click on a single method and list all method usages with Find usages. This is especially handy to quickly see usage examples for complex methods like
join()from the query builder.
DELETEstatements are often better to read and write using the Connection object instead of the query builder.
SELECT DISTINCT aFieldis not supported but can be substituted with a
getSql() and executeQuery() / executeStatement() can be used after each other during development to simplify debugging:
$queryBuilder ->select('uid') ->from('tt_content') ->where( $queryBuilder->expr()->eq( 'bodytext', $queryBuilder->createNamedParameter('lorem') ) ); debug($queryBuilder->getSql()); $result = $queryBuilder->executeQuery();
Doctrine DBAL throws exceptions if something goes wrong when calling executeQuery() or executeStatement(). The exception type is
\Doctrine\DBAL\Exception, which can be caught and transferred to a better error message if the application should expect query errors. Note that this is not good habit and often indicates an architectural flaw in the application at a different layer.
count() <database-query-builder-count>query types using the query builder normally call ->fetchOne() to receive the count value. The count() method of the Connection object does this automatically and returns the result of the count value directly.