INNER JOIN Results from Select Statement using Doctrine QueryBuilder

A big thanks to @AdrienCarniero for his alternative query structure for sorting the highest version with a simple JOIN where the entity’s timeMod is less than the joined table timeMod. Alternative Query SELECT view_version.* FROM view_version #inner join to get the best version LEFT JOIN view_version AS best_version ON best_version.viewId = view_version.viewId AND best_version.timeMod > … Read more

Use a DATE() function in a WHERE clause with DQL

You can achieve what you want by using a custom function: use Doctrine\ORM\Query\AST\Functions\FunctionNode; use Doctrine\ORM\Query\Lexer; use Doctrine\ORM\Query\SqlWalker; use Doctrine\ORM\Query\Parser; class DateFunction extends FunctionNode { private $arg; public function getSql(SqlWalker $sqlWalker) { return sprintf(‘DATE(%s)’, $this->arg->dispatch($sqlWalker)); } public function parse(Parser $parser) { $parser->match(Lexer::T_IDENTIFIER); $parser->match(Lexer::T_OPEN_PARENTHESIS); $this->arg = $parser->ArithmeticPrimary(); $parser->match(Lexer::T_CLOSE_PARENTHESIS); } } Then registering this function in your code: … Read more

How to convert this to Doctrine 2 QueryBuilder format?

try this : 1) create your subquery $subquery = $this->_em->createQueryBuilder() ->select(‘t.id’) ->from(‘yourBundle:Task’, ‘t’) ->innerjoin(‘t.user’,’u’) ->where(‘u.id = 1’) ->getDQL(); 2) after create your query $query = $this->_em->createQueryBuilder(); $query->select(‘l’) ->from(‘yourBundle:Layer’, ‘l’) ->where($query->expr()->notIn(‘l.id’, $subquery)) ….; I tested it and it works 🙂

Can I access discriminator field from php in doctrine2?

Extending what beberlei said, you could declare some constants in the Attribute class, and an abstract getType() function. Then, overload it in every derived attribute class. Something like: abstract class Attribute { const TYPE_BOOL = 0; const TYPE_INT = 1; … abstract public function getType(); } class BooleanAttribute extends Attribute { public function getType() { … Read more

How can I use SQL’s YEAR(), MONTH() and DAY() in Doctrine2?

You can add Doctrine extension so you can use the MySql YEAR and MONTH statement by adding this configuration if you’re on Symfony: doctrine: orm: dql: string_functions: MONTH: DoctrineExtensions\Query\Mysql\Month YEAR: DoctrineExtensions\Query\Mysql\Year now you can use the MONTH and YEAR statements in your DQL or querybuilder. Note: The extension supports MySQL, Oracle, PostgreSQL and SQLite.

Doctrine query builder using inner join with conditions

I’m going to answer my own question. innerJoin should use the keyword “WITH” instead of “ON” (Doctrine’s documentation [13.2.6. Helper methods] is inaccurate; [13.2.5. The Expr class] is correct) no need to link foreign keys in join condition as they’re already specified in the entity mapping. Therefore, the following works for me $qb->select(‘c’) ->innerJoin(‘c.phones’, ‘p’, … Read more

The differences between GeneratedValue strategies

Check the latest doctrine documentation Here is a summary : the list of possible generation strategies: AUTO (default): Tells Doctrine to pick the strategy that is preferred by the used database platform. The preferred strategies are IDENTITY for MySQL, SQLite and MsSQL and SEQUENCE for Oracle and PostgreSQL. This strategy provides full portability. SEQUENCE: Tells … Read more