将 MySQL 转换为 Doctrine Query Builder.IF 和 CONCAT 的问题.或另一种选择子查询的方法

Converting MySQL to Doctrine Query Builder. Issues with IF and CONCAT. Or another approach for subqueries on select(将 MySQL 转换为 Doctrine Query Builder.IF 和 CONCAT 的问题.或另一种选择子查询的方法)

本文介绍了将 MySQL 转换为 Doctrine Query Builder.IF 和 CONCAT 的问题.或另一种选择子查询的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的类别有一个表,每个类别都有 id、name 和 parent_id.

I have a table for my categories, each category have and id, name and parent_id.

Select IF(a.parent_id IS NULL, a.name, CONCAT((SELECT b.name FROM category b WHERE b.id = a.parent_id), " / ", a.name) ) as n, a.id, a.parent_id
FROM category a
ORDER BY n

我想将它转换为我的 Doctrine2 查询生成器

I want to convert it to my Doctrine2 Query Builder

    $em = $this->getDoctrine()->getEntityManager();
    $qb = $em->createQueryBuilder();
    $q = $qb
            ->select("c.id")
            ->addSelect(
                "IF(c.parent_id IS NULL, c.name, CONCAT((" .
                $em->createQueryBuilder()
                   ->select("t.name")
                   ->from("MyBundle:Category", "t")
                   ->getQuery()->getDQL() .
                "), " / ", c.name) )"
            )
            ->from("MyBundle:Category", "c");
    echo $q->getQuery()->getSQL();
    exit;

类似的东西,但我不能使用 IFCONCAT.

Something like that, but I cant use the IF, and CONCAT.

推荐答案

好的,我找到了解决方案.

Ok I found the solution.

您可以使用 CASE 代替 IF.看看这个,但是当我使用 CASE 我不能 CONCAT 我的字段:

You can use CASE instead of IF. Check this out, but when I am using CASE I can't CONCAT my fields:

$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder();
$q = $qb
      ->select("c.id")
      ->addSelect("CASE WHEN (c.parent IS NULL) THEN c.name ELSE 'something' END")
      ->from("MyBundle:Category", "c")
      ->leftJoin("c.parent", "t");

echo $q->getQuery()->getSQL();

另一种解决方案是创建自己的 DQL 函数,例如 IF 并像这样使用它:

Another Solution is create your own DQL function, like IF and use it like this:

$em = $this->getDoctrine()->getEntityManager();
$qb = $em->createQueryBuilder();
$q = $qb
      ->select("c.id")
      ->addSelect("IF(c.parent IS NULL, c.name, CONCAT(CONCAT(t.name, ' / '), c.name))")
      ->from("MyBundle:Category", "c")
      ->leftJoin("c.parent", "t");

echo $q->getQuery()->getSQL();

要创建此 IF,您可以访问此链接并学习:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#adding-your-own-functions-to-the-dql-language

For create this IF you can go to this link and learn: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#adding-your-own-functions-to-the-dql-language

我将在这里发布我的 IF 课程和 config.yml 以帮助其他人.这是 IfFunction 类(我从 https:///github.com/beberlei/DoctrineExtensions/blob/master/src/Query/Mysql/IfElse.php):

I will post here my class for this IF and the config.yml to help other people. Here is the IfFunction class (I got that from https://github.com/beberlei/DoctrineExtensions/blob/master/src/Query/Mysql/IfElse.php):

<?php
namespace MyNameMiscBundleDoctrineORMQueryASTFunctions;

use DoctrineORMQueryASTFunctionsFunctionNode;
use DoctrineORMQueryLexer;

/**
 * Usage: IF(expr1, expr2, expr3)
 * 
 * If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2;
 * otherwise it returns expr3. IF() returns a numeric or string value,
 * depending on the context in which it is used. 
 * 
 * @author  Andrew Mackrodt <andrew@ajmm.org>
 * @version 2011.06.19
 */
class IfFunction extends FunctionNode
{
    private $expr = array();

    public function parse(DoctrineORMQueryParser $parser)
    {
        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->expr[] = $parser->ConditionalExpression();

        for ($i = 0; $i < 2; $i++)
        {
            $parser->match(Lexer::T_COMMA);
            $this->expr[] = $parser->ArithmeticExpression();
        }

        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

    public function getSql(DoctrineORMQuerySqlWalker $sqlWalker)
    {
        return sprintf('IF(%s, %s, %s)',
            $sqlWalker->walkConditionalExpression($this->expr[0]),
            $sqlWalker->walkArithmeticPrimary($this->expr[1]),
            $sqlWalker->walkArithmeticPrimary($this->expr[2]));
    }
}

之后,您需要像这样更新您的 config.yml(刚刚添加了最后 3 行):

After that you need to update your config.yml like this (just added the last 3 lines):

doctrine:
    dbal:
        driver:   "%database_driver%"
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true
        dql: #ADDED THIS LINE
            string_functions: #ADDED THIS LINE
                IF: MyNameMiscBundleDoctrineORMQueryASTFunctionsIfFunction #ADDED THIS LINE

谢谢

这篇关于将 MySQL 转换为 Doctrine Query Builder.IF 和 CONCAT 的问题.或另一种选择子查询的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将 MySQL 转换为 Doctrine Query Builder.IF 和 CONCAT 的问题.或另一种选择子查询的方法

基础教程推荐