EntityRepository and EntityManager have slightly different versions of the createQueryBuilder() function. Whereas the EntityManager’s version takes no arguments, EntityRepository’s version expects an ‘
$alias‘ parameter. What is going on?!
The EntityRepository class wraps the EntityManager’s call, as such:
The function’s phpdoc reveals the reasoning behind the parameter count differences, EntityRepository returns a version of createQueryBuilder() customised for itself. We no longer need to specify the primary table we’re selecting from. Instead we must supply an
$alias parameter which would usually be later supplied to the
Also note the above means all columns are selected from the entity by default.
When using the Symfony2 Framework it is often better to create custom exceptions to handle unique circumstances rather than incorrectly utilising one of the many pre-existing Symfony exception types. Using an interface improves code cohesion, allows the adding of custom functionality as needed and is super easy to setup.
The first step is to create a custom exception interface.
Then create a custom exception class (perhaps in an Exception directory if it suits your project’s structure):
class NewTypeOfException extends \Exception implements AppBundleExceptionInterface
This exception can now be thrown and caught as desired, just be sure to name your exceptions well. You may be throwing them long after they were originally created!