2017-01-06 3 views
1

は、私が実際に必要なもの:QueryBuilderと先月から返すレコードをSymfony2のCURRENT_DATE()と先月のQueryBuilderの差を計算するにはどうすればよいですか?

で、または他の言葉で:は私がやった教義QueryBuilder

created_at >= DATE_FORMAT(NOW(), '%Y-%m-%d') - INTERVAL 1 MONTH 

に次のMySQLクエリを変換:

$clicks = $clickRepo->createQueryBuilder('c') 
     ->select('c.product, c.createdAt, c.title') 
     ->addSelect('COUNT(c.product)') 
     ->where('c.type = :pro') 
      ->setParameter('pro', 'product') 
     ->andWhere('c.createdAt >= DATE_DIFF(CURRENT_DATE(), :end)') 
      ->setParameter('end', new \DateTime('-30 days'), \Doctrine\DBAL\Types\Type::DATETIME) 
     ->andWhere('c.shop != :null') 
      ->setParameter('null', '0') 
     ->andWhere('c.visible = :one') 
      ->setParameter('one', '1') 
     ->groupBy('c.product') 
     ->setMaxResults(2) 
     ->getQuery()->getResult(); 

R結果:

array(1) { 
    [0]=> 
    array(4) { 
    ["product"]=> 
    int(3) 
    ["createdAt"]=> 
    object(DateTime)#3211 (3) { 
     ["date"]=> 
     string(26) "2016-02-19 13:27:45.000000" 
     ["timezone_type"]=> 
     int(3) 
     ["timezone"]=> 
     string(13) "Europe/Berlin" 
    } 
    ["title"]=> 
    string(23) "Title BlaBla" 
    ["clicks"]=> 
    string(1) "2" 
    } 
} 

テーブルの先頭にはレコードのみが返されます。

答えて

0

私はちょうど私の自身のDQL functionを登録。

DateAddFunction.php

# Bundle/ProductBundle/myDQL/DateAddFunction.php 
<?php 

namespace Bundle\ProductBundle\myDQL; 

use Doctrine\ORM\Query\Lexer; 
use Doctrine\ORM\Query\AST\Functions\FunctionNode; 

/** 
* DateAddFunction ::= 
*  "DATE_ADD" "(" ArithmeticPrimary ", INTERVAL" ArithmeticPrimary Identifier ")" 
*/ 
class DateAddFunction extends FunctionNode 
{ 
    public $firstDateExpression = null; 
    public $intervalExpression = null; 
    public $unit = null; 

    public function parse(\Doctrine\ORM\Query\Parser $parser) 
    { 
     $parser->match(Lexer::T_IDENTIFIER); 
     $parser->match(Lexer::T_OPEN_PARENTHESIS); 

     $this->firstDateExpression = $parser->ArithmeticPrimary(); 

     $parser->match(Lexer::T_COMMA); 
     $parser->match(Lexer::T_IDENTIFIER); 

     $this->intervalExpression = $parser->ArithmeticPrimary(); 

     $parser->match(Lexer::T_IDENTIFIER); 

     /* @var $lexer Lexer */ 
     $lexer = $parser->getLexer(); 
     $this->unit = $lexer->token['value']; 

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

    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) 
    { 
     return 'DATE_ADD(' . 
      $this->firstDateExpression->dispatch($sqlWalker) . ', INTERVAL ' . 
      $this->intervalExpression->dispatch($sqlWalker) . ' ' . $this->unit . 
      ')'; 
    } 
} 

config.yml

# app/config/config.yml 
doctrine: 
    orm: 
     auto_generate_proxy_classes: "%kernel.debug%" 
     auto_mapping: true 
     dql: 
      string_functions: 
       DATEADD: \Bundle\ProductBundle\myDQL\DateAddFunction 

クエリ

$clicks = $clickRepo->createQueryBuilder('c') 
    ->select('c.product, c.createdAt, c.title') 
    ->addSelect('COUNT(c.product)') 
    ->where('c.type = :pro') 
    ->setParameter('pro', 'product') 
    ->andWhere('c.createdAt >= DATEADD(CURRENT_DATE(), INTERVAL :minusOne MONTH)') 
    ->setParameter('minusOne', '-1') 
    ->andWhere('c.shop != :null') 
    ->setParameter('null', '0') 
    ->andWhere('c.visible = :one') 
    ->setParameter('one', '1') 
    ->groupBy('c.product') 
    ->setMaxResults(2) 
    ->getQuery()->getResult(); 
0

あなたは直接、最後の1ヶ月のデータを取得するためにmysqlの 'INTERVAL 1 MONTH' を使用することができます。

$clicks = $clickRepo->createQueryBuilder('c') 
      ->select('c.product, c.createdAt, c.title') 
      ->addSelect('COUNT(c.product)') 
      ->where('c.type = :pro') 
       ->setParameter('pro', 'product') 
      ->andWhere('c.createdAt >= (NOW() - INTERVAL 1 MONTH)') 

      ->andWhere('c.shop != :null') 
       ->setParameter('null', '0') 
      ->andWhere('c.visible = :one') 
       ->setParameter('one', '1') 
      ->groupBy('c.product') 
      ->setMaxResults(2) 
      ->getQuery()->getResult(); 

+0

1.私は質問のタイトルを編集しました。 DoctrineはCURRENT_DATE()のみをサポートします。 [Doctrine \ ORM \ Query \ QueryException] [構文エラー]行0、列168:エラー:期待されるDoctrine \ ORM \ Query \ Lexer :: T_CLOSE_PARENTHESIS、 1 ' –

+0

3.私は、あなたのフォームにも問題があることを警告しなければなりません。 c.createdAt> =(NOW()、INTERVAL 1 MONTH) –

1

DATE_DIFF()....それが役に立てば幸い2つの日付の間の時間を返します。 DATE_SUB()関数を使う必要があると思います。

->andWhere('c.createdAt >= DATE_SUB(CURRENT_DATE(), :end)')

+0

この場合、このエラーが発生します。 [Doctrine \ ORM \ Query \ QueryException] [構文エラー]行0、col170:エラー:期待されるDoctrine \ ORM \ Query \ Lexer :: T_COMMA、 ')' –

関連する問題