• <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 一起使用时出错

        Error when use custom DQL function with Doctrine and Symfony2(将自定义 DQL 函数与 Doctrine 和 Symfony2 一起使用时出错)

        <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 一起使用时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  • <bdo id='GavQN'></bdo><ul id='GavQN'></ul>
                      <tbody id='GavQN'></tbody>
                  • <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>