복붙노트

[SQL] 어떻게 Doctrine2에서 SQL의 YEAR (), MONTH () 및 DAY ()를 사용할 수 있습니까?

SQL

어떻게 Doctrine2에서 SQL의 YEAR (), MONTH () 및 DAY ()를 사용할 수 있습니까?

나는 네이티브 SQL에서이 같을 것이다 쿼리를 수행 할 :

SELECT
    AVG(t.column) AS average_value
FROM
    table t
WHERE
    YEAR(t.timestamp) = 2013 AND
    MONTH(t.timestamp) = 09 AND
    DAY(t.timestamp) = 16 AND
    t.somethingelse LIKE 'somethingelse'
GROUP BY
    t.somethingelse;

이 같은 교리의 쿼리 빌더에서이를 구현하려고하는 경우 :

$qb = $this->getDoctrine()->createQueryBuilder();
$qb->select('e.column AS average_value')
   ->from('MyBundle:MyEntity', 'e')
   ->where('YEAR(e.timestamp) = 2013')
   ->andWhere('MONTH(e.timestamp) = 09')
   ->andWhere('DAY(e.timestamp) = 16')
   ->andWhere('u.somethingelse LIKE somethingelse')
   ->groupBy('somethingelse');

나는 오류 예외가

어떻게 교리 쿼리 빌더 내 쿼리를 구현할 수있다?

노트:

해결법

  1. ==============================

    1.당신이 심포니에 있다면이 구성을 추가하여 MySQL의 연도와 월 문을 사용할 수 있도록 교리 확장을 추가 할 수 있습니다 :

    당신이 심포니에 있다면이 구성을 추가하여 MySQL의 연도와 월 문을 사용할 수 있도록 교리 확장을 추가 할 수 있습니다 :

    doctrine:
        orm:
            dql:
                string_functions:
                    MONTH: DoctrineExtensions\Query\Mysql\Month
                    YEAR: DoctrineExtensions\Query\Mysql\Year
    

    지금 당신은 당신의 DQL 또는 querybuilder의 연도와 월 문을 사용할 수 있습니다.

  2. ==============================

    2.심포니 4에서는 DoctrineExtensions를 설치해야합니다 :

    심포니 4에서는 DoctrineExtensions를 설치해야합니다 :

    composer require beberlei/DoctrineExtensions
    

    그리고 다음과 같은 교리의 설정 파일 (설정 / 패키지 / doctrine.yaml)을 편집 :

    doctrine:
        orm:
            dql:
                string_functions:
                    MONTH: DoctrineExtensions\Query\Mysql\Month
                    YEAR: DoctrineExtensions\Query\Mysql\Year
    
  3. ==============================

    3.orocrm / 교리 확장도 좋은 프로젝트가 될 것 같다

    orocrm / 교리 확장도 좋은 프로젝트가 될 것 같다

    그것은 모두의 MySQL을 지원하며, PostgreSQL은 .. 목표는 크로스 DB가 될 것입니다

  4. ==============================

    4.심포니 4 :

    심포니 4 :

        doctrine:
            orm:
                dql:
                    datetime_functions:
                        DAY: DoctrineExtensions\Query\Mysql\Day
                        MONTH: DoctrineExtensions\Query\Mysql\Month
                        YEAR: DoctrineExtensions\Query\Mysql\Year
    
        public function somex()
        {
            $em = $this->getDoctrine()->getManager();
    
            $emConfig = $em->getConfiguration();
            $emConfig->addCustomDatetimeFunction('YEAR', 'DoctrineExtensions\Query\Mysql\Year');
            $emConfig->addCustomDatetimeFunction('MONTH', 'DoctrineExtensions\Query\Mysql\Month');
            $emConfig->addCustomDatetimeFunction('DAY', 'DoctrineExtensions\Query\Mysql\Day');
    
            $day = '22';
            $month = '4';
    
            $qb = $em->createQueryBuilder()
                ->select('u')
                ->from('App\Entity\User', 'u')
                ->where('DAY(u.somedate) = :day')
                ->andwhere('MONTH(u.somedate) = :month')
                ->setParameter('month', $day)
                ->setParameter('day', $month)
            ;
            $trab = $qb->getQuery()->getResult();
    
    
            return $this->render('intranet/somex.html.twig', [
                'trab' => $trab
            ]);
        }
        ````
    
  5. ==============================

    5.이 페이지로 이동하여 데이터베이스 시스템을 선택하고 원하는 기능을 선택

    이 페이지로 이동하여 데이터베이스 시스템을 선택하고 원하는 기능을 선택

    ... 당신은 ... 나는이 GitHub의 파일에 정확히 같은 이름을 사용하여 내 doctrine.yaml 파일을 수정하고 일을 같이 나도 같은 문제가 있었다.

  6. from https://stackoverflow.com/questions/18826836/how-can-i-use-sqls-year-month-and-day-in-doctrine2 by cc-by-sa and MIT license