교리 2에서 날짜 사이의 항목 선택
저는 이 정도의 실수라면 정신이 이상해질 것이기 때문에 고치지 못할 것입니다.이틀 사이에 항목을 선택하려고 합니다. 아래 예제는 모든 실패를 보여줍니다.
선택 1.
$qb->where('e.fecha > ' . $monday->format('Y-m-d'));
$qb->andWhere('e.fecha < ' . $sunday->format('Y-m-d'));
결과(0개 항목):
SELECT r0_.id_reservacion AS id_reservacion0, r0_.fecha AS fecha1, r0_.cliente AS cliente2
FROM reservacion r0_
WHERE (r0_.fecha > 2012 - 07 - 16) AND (r0_.fecha < 2012 - 07 - 22)
opt 2
$qb->add('where', 'e.fecha between 2012-01-01 and 2012-10-10');
결과(0개 항목):
SELECT r0_.id_reservacion AS id_reservacion0, r0_.fecha AS fecha1, r0_.cliente AS cliente2
FROM reservacion r0_ WHERE r0_.fecha
BETWEEN 2012 - 01 - 01 AND 2012 - 10 - 10
현재 항목이 포함된 표입니다.
id fecha cliente
1 2012-07-16 00:00:00 2
2 2012-07-16 13:00:00 4
3 2012-07-22 23:00:00 4
편집 1
의심을 피하기 위해 sql을 평가하기 위해 다음 쿼리를 실행했습니다.
$qb->where('e.fecha > ' . $sunday->format('Y-m-d'));
결과(3개 항목):
SELECT r0_.id_reservacion AS id_reservacion0, r0_.fecha AS fecha1, r0_.cliente AS cliente2
sql이 문제가 아닌 것 같습니다.FROM 예약 r0_ WHERE r0_.fecha > 2012 - 07
둘 중 하나를 할 수 있습니다.
$qb->where('e.fecha BETWEEN :monday AND :sunday')
->setParameter('monday', $monday->format('Y-m-d'))
->setParameter('sunday', $sunday->format('Y-m-d'));
아니면…
$qb->where('e.fecha > :monday')
->andWhere('e.fecha < :sunday')
->setParameter('monday', $monday->format('Y-m-d'))
->setParameter('sunday', $sunday->format('Y-m-d'));
쿼리 작성자 표현식을 사용하는 것이 올바른 방법이라고 생각합니다.
$now = new DateTimeImmutable();
$thirtyDaysAgo = $now->sub(new \DateInterval("P30D"));
$qb->select('e')
->from('Entity','e')
->add('where', $qb->expr()->between(
'e.datefield',
':from',
':to'
)
)
->setParameters(array('from' => $thirtyDaysAgo, 'to' => $now));
http://docs.doctrine-project.org/en/latest/reference/query-builder.html#the-expr-class
편집: 이 방법은 데이터베이스 소프트웨어와 독립적이라는 장점이 있습니다. 이런 종류의 일을 처리하기 위한 추상화 계층이 있기 때문에 Distrine이 날짜 유형을 처리하도록 해야 합니다.
문자열 변수를 'Y-m-d' 형식으로 추가하는 것과 같은 작업을 수행하면 MySQL이 아닌 다른 데이터베이스 플랫폼으로 이동할 때 끊어집니다.
--- 다른 예:
이 예제에서는 보다 큰 접근법과 작은 접근법을 사용하여 중간 조건을 만듭니다.
if ($updateDateTime instanceof DateTime) {
$qb->andWhere(
$qb->expr()->gte('c.updated', ':updateDateTimeStart'),
$qb->expr()->lt('c.updated', ':updateDateTimeEnd'),
);
$updateDateTimeImmutable = DateTimeImmutable::createFromMutable($updateDateTime);
$start = $updateDateTimeImmutable->setTime(0,0,0, 0);
$end = $start->modify('+1 day');
$qb->setParameter('updateDateTimeStart', $start, Types::DATE_IMMUTABLE);
$qb->setParameter('updateDateTimeEnd', $end, Types::DATE_IMMUTABLE);
}
편집: 더 나은 솔루션에 대한 기타 답변 참조
제가 제안한 원래의 신입 접근 방식은 (opt1)이었습니다.
$qb->where("e.fecha > '" . $monday->format('Y-m-d') . "'");
$qb->andWhere("e.fecha < '" . $sunday->format('Y-m-d') . "'");
그리고 (opt2):
$qb->add('where', "e.fecha between '2012-01-01' and '2012-10-10'");
그것은 빠르고 쉬웠고 원래의 포스터를 즉시 보여주었습니다.
그래서 인정된 대답.
댓글에 의하면 오답이지만 쉽게 범할 수 있는 실수이기 때문에 '하지 말아야 할 일'로 여기에 남깁니다.
내 날짜 $jour를 파라미터에 어떻게 포맷하는지 보세요.expr()->like를 사용하는지 expr()->lte를 사용하는지에 따라 다릅니다.
$qb
->select('e')
->from('LdbPlanningBundle:EventEntity', 'e')
->where(
$qb->expr()->andX(
$qb->expr()->orX(
$qb->expr()->like('e.start', ':jour1'),
$qb->expr()->like('e.end', ':jour1'),
$qb->expr()->andX(
$qb->expr()->lte('e.start', ':jour2'),
$qb->expr()->gte('e.end', ':jour2')
)
),
$qb->expr()->eq('e.user', ':user')
)
)
->andWhere('e.user = :user ')
->setParameter('user', $user)
->setParameter('jour1', '%'.$jour->format('Y-m-d').'%')
->setParameter('jour2', $jour->format('Y-m-d'))
->getQuery()
->getArrayResult()
;
언급URL : https://stackoverflow.com/questions/11553183/select-entries-between-dates-in-doctrine-2
'programing' 카테고리의 다른 글
링크하는 동안 전역 변수에 대한 정의되지 않은 참조 (0) | 2023.10.15 |
---|---|
워드프레스:로그인 페이지에서 "비밀번호 분실" 텍스트를 사용자 정의하려면 어떻게 해야 합니까? (0) | 2023.10.15 |
Angularjs에서 모든 쿠키를 제거하는 방법? (0) | 2023.10.15 |
각 루프에 대한 자바스크립트 간의 차이.각 루프에 대해 in 및 angular.? (0) | 2023.10.15 |
C/C++ unsigned integer overflow (0) | 2023.10.15 |