How can I use DATE() in Doctrine 2 DQL?

In addition to the accepted answer there are a tonne of pre-built custom functions available at https://github.com/beberlei/DoctrineExtensions . These can be then registered in your config like doctrine: orm: dql: string_functions: DATE: DoctrineExtensions\Query\Mysql\Date and can then be used in your DQL (as in your query) like DATE(jobs.endDate) AS endDate

Ignore a Doctrine2 Entity when running schema-manager update

Based on the original alswer of ChrisR inspired in Marco Pivetta’s post I’m adding here the solution if you’re using Symfony2: Looks like Symfony2 doesn’t use the original Doctrine command at: \Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand Instead it uses the one in the bundle: \Doctrine\Bundle\DoctrineBundle\Command\Proxy\UpdateSchemaDoctrineCommand So basically that is the class that must be extended, ending up in having: … Read more

Doctrine2 association mapping with conditions

You can use the Criteria API to filter the collection: <?php use Doctrine\Common\Collections\Criteria; class Article { /** * @ORM\OneToMany(targetEntity=”Comment”, mappedBy=”article”) */ protected $comments; public function getComments($showPending = false) { $criteria = Criteria::create(); if ($showPending !== true) { $criteria->where(Criteria::expr()->eq(‘approved’, true)); } return $this->comments->matching($criteria); } } This is especially nice, because Doctrine is smart enough to only … Read more

Query on a many-to-many relationship using Doctrine with Symfony2

You can write a join DQL query as below $em = $this->getContainer()->get(‘doctrine’)->getManager(); $repository = $em->getRepository(‘YourNamespaceYourBundle:User’); $query = $repository->createQueryBuilder(‘u’) ->innerJoin(‘u.groups’, ‘g’) ->where(‘g.id = :group_id’) ->setParameter(‘group_id’, 5) ->getQuery()->getResult(); Your mapping for groups property in User entity will handle join part itself you don’t have to mention the junction table in your DQL query

CASTING attributes for Ordering on a Doctrine2 DQL Query

You should be able to add your own function to implement this feature. The class would look something like this: namespace MyProject\Query; use Doctrine\ORM\Query\AST\Functions\FunctionNode; use Doctrine\ORM\Query\Lexer; use Doctrine\ORM\Query\Parser; use Doctrine\ORM\Query\SqlWalker; class CastAsInteger extends FunctionNode { public $stringPrimary; public function getSql(SqlWalker $sqlWalker) { return ‘CAST(‘ . $this->stringPrimary->dispatch($sqlWalker) . ‘ AS integer)’; } public function parse(Parser $parser) … Read more

Doctrine 2 ArrayCollection filter method

Doctrine now has Criteria which offers a single API for filtering collections with SQL and in PHP, depending on the context. https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#filtering-collections Update This will achieve the result in the accepted answer, without getting everything from the database. use Doctrine\Common\Collections\Criteria; /** * @ORM\Entity */ class Member { // … public function getCommentsFiltered($ids) { $criteria = … Read more