• <bdo id='55fTx'></bdo><ul id='55fTx'></ul>

      <legend id='55fTx'><style id='55fTx'><dir id='55fTx'><q id='55fTx'></q></dir></style></legend>
    1. <tfoot id='55fTx'></tfoot>

      <small id='55fTx'></small><noframes id='55fTx'>

    2. <i id='55fTx'><tr id='55fTx'><dt id='55fTx'><q id='55fTx'><span id='55fTx'><b id='55fTx'><form id='55fTx'><ins id='55fTx'></ins><ul id='55fTx'></ul><sub id='55fTx'></sub></form><legend id='55fTx'></legend><bdo id='55fTx'><pre id='55fTx'><center id='55fTx'></center></pre></bdo></b><th id='55fTx'></th></span></q></dt></tr></i><div id='55fTx'><tfoot id='55fTx'></tfoot><dl id='55fTx'><fieldset id='55fTx'></fieldset></dl></div>

        将自定义 DQL 函数与 Doctrine 和 Symfony2 一起使用时出错

        时间:2024-08-09

        <i id='AbPGq'><tr id='AbPGq'><dt id='AbPGq'><q id='AbPGq'><span id='AbPGq'><b id='AbPGq'><form id='AbPGq'><ins id='AbPGq'></ins><ul id='AbPGq'></ul><sub id='AbPGq'></sub></form><legend id='AbPGq'></legend><bdo id='AbPGq'><pre id='AbPGq'><center id='AbPGq'></center></pre></bdo></b><th id='AbPGq'></th></span></q></dt></tr></i><div id='AbPGq'><tfoot id='AbPGq'></tfoot><dl id='AbPGq'><fieldset id='AbPGq'></fieldset></dl></div>
        <legend id='AbPGq'><style id='AbPGq'><dir id='AbPGq'><q id='AbPGq'></q></dir></style></legend>
              <tfoot id='AbPGq'></tfoot>
                <bdo id='AbPGq'></bdo><ul id='AbPGq'></ul>

                <small id='AbPGq'></small><noframes id='AbPGq'>

                    <tbody id='AbPGq'></tbody>
                • 本文介绍了将自定义 DQL 函数与 Doctrine 和 Symfony2 一起使用时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我使用 Symfony 2 和 ORM 原则.我想创建和注册一个自定义 DQL 函数.其实我想用SQL函数CAST"我的要求,像这样:

                  I use Symfony 2 and the ORM Doctrine. I want to create and register a custom DQL function. In fact, I want to use the SQL function "CAST" in my request, like this :

                      $qb = $this->_em->createQueryBuilder();
                      $qb->select('d')
                         ->from('TestMyBundleEntityMyEntity', 'd')
                         ->orderBy('CAST(d.myField AS UNSIGNED)', 'ASC')
                  
                      return $qb->getQuery()->getResult();
                  

                  为此,我创建了一个扩展FunctionNode"的CastFunction":

                  For this, I have created a "CastFunction" who extend "FunctionNode" :

                  namespace TestMyBundleDQL;
                  
                  use DoctrineORMQueryASTFunctionsFunctionNode;
                  use DoctrineORMQueryLexer;
                  use DoctrineORMQuerySqlWalker;
                  use DoctrineORMQueryParser;
                  
                  class CastFunction extends FunctionNode
                  {
                      public $firstDateExpression = null;
                      public $secondDateExpression = null;
                  
                      public function parse(DoctrineORMQueryParser $parser)
                      {
                          $parser->match(Lexer::T_IDENTIFIER);
                          $parser->match(Lexer::T_OPEN_PARENTHESIS);
                          $this->firstDateExpression = $parser->ArithmeticPrimary();
                          $parser->match(Lexer::T_IDENTIFIER);
                          $this->secondDateExpression = $parser->ArithmeticPrimary();
                          $parser->match(Lexer::T_CLOSE_PARENTHESIS);
                      }
                  
                      public function getSql(DoctrineORMQuerySqlWalker $sqlWalker)
                      {
                          return sprintf('CAST(%s AS %s)', $this->firstDateExpression->dispatch($sqlWalker), $this->secondDateExpression->dispatch($sqlWalker));
                      }
                  }
                  

                  当然,我已经在我的 config.yml 中注册了这个类:

                  Of course, I have registered this class in my config.yml :

                  doctrine:
                      orm:
                          dql:
                              string_functions:
                                  CAST: TestMyBundleDQLCastFunction
                  

                  现在,当我尝试我的请求时,我收到以下错误:

                  Now, when I try my request, I obtain the following error:

                  [Semantical Error] line 0, col 83 near 'UNSIGNED)': Error: 'UNSIGNED' is not defined."

                  "[Semantical Error] line 0, col 83 near 'UNSIGNED)': Error: 'UNSIGNED' is not defined."

                  我搜索但我不知道问题出在哪里!

                  I search but I don't where is the problem!

                  你有想法吗?

                  推荐答案

                  经过多次搜索,终于找到了解决方案.我有两个问题:首先我的解析函数错误,其次,我在 orderBy 中调用了一个 SQL 函数(谢谢 Cerad).

                  After several search, I have finally found the solution. I had two problems: first my parse function was wrong, second, I called a SQL function in my orderBy (thank you Cerad).

                  所以,这是我正确的课程:

                  So, here is my correct class:

                  namespace YpokYPoliceBundleDQL;
                  
                  use DoctrineORMQueryASTFunctionsFunctionNode;
                  use DoctrineORMQueryLexer;
                  use DoctrineORMQuerySqlWalker;
                  use DoctrineORMQueryParser;
                  
                  class CastFunction extends FunctionNode
                  {
                      public $firstDateExpression = null;
                      public $unit = null;    
                  
                      public function parse(DoctrineORMQueryParser $parser)
                      {
                          $parser->match(Lexer::T_IDENTIFIER);
                          $parser->match(Lexer::T_OPEN_PARENTHESIS);
                          $this->firstDateExpression = $parser->StringPrimary();
                  
                          $parser->match(Lexer::T_AS);
                  
                          $parser->match(Lexer::T_IDENTIFIER);
                          $lexer = $parser->getLexer();
                          $this->unit = $lexer->token['value'];
                  
                          $parser->match(Lexer::T_CLOSE_PARENTHESIS);
                      }
                  
                      public function getSql(DoctrineORMQuerySqlWalker $sqlWalker)
                      {
                          return sprintf('CAST(%s AS %s)',  $this->firstDateExpression->dispatch($sqlWalker), $this->unit);
                      }
                  }
                  

                  现在,我可以在我的存储库中完美地使用 SQL 函数CAST"了:

                  And now, I can use perfectly the SQL function 'CAST' in my repository:

                  $qb = $this->_em->createQueryBuilder();
                  $qb->select('d, CAST(d.myField AS UNSIGNED) AS sortx')
                     ->from('TestMyBundleEntityMyEntity', 'd')
                     ->orderBy('sortx', 'ASC')
                  
                  return $qb->getQuery()->getResult();
                  

                  最好的问候

                  这篇关于将自定义 DQL 函数与 Doctrine 和 Symfony2 一起使用时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何创建学说实体的模拟对象? 下一篇:集成 ZF/Doctrine2:我在哪里放置我的模型/实体和代理类

                  相关文章

                  • <bdo id='GavQN'></bdo><ul id='GavQN'></ul>
                • <tfoot id='GavQN'></tfoot>
                • <i id='GavQN'><tr id='GavQN'><dt id='GavQN'><q id='GavQN'><span id='GavQN'><b id='GavQN'><form id='GavQN'><ins id='GavQN'></ins><ul id='GavQN'></ul><sub id='GavQN'></sub></form><legend id='GavQN'></legend><bdo id='GavQN'><pre id='GavQN'><center id='GavQN'></center></pre></bdo></b><th id='GavQN'></th></span></q></dt></tr></i><div id='GavQN'><tfoot id='GavQN'></tfoot><dl id='GavQN'><fieldset id='GavQN'></fieldset></dl></div>

                      <small id='GavQN'></small><noframes id='GavQN'>

                    1. <legend id='GavQN'><style id='GavQN'><dir id='GavQN'><q id='GavQN'></q></dir></style></legend>