Вопрос по symfony – Как сравнить поле даты и времени из Doctrine2 с датой?

11

Я хочу получить предметы, которые были созданы сегодня с помощью QueryBuilder из Doctrine2. Я хочу сравнить поле createAt (Datetime) с параметром today (Дата). Возможно ли сделать это одним запросом?

$qb = $this->createQueryBuilder('i');
$qb->innerJoin('i.type', 'it');
$qb->andWhere('it.name = :type');
$qb->andWhere('i.createdAt < :today');
// i.createdAt == datetime and :today parameter is a date

Ваш Ответ

6   ответов
13

DATESUBSTRING

SELECT SUBSTRING(i.dateTimeField,1,10) FROM tableName i

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
5

today

$today = new \DateTime();
$qb->setParameter('today', $today->format('Y-m-d'));

'Y-m-d'

20

$qb->select('p')
   ->where('YEAR(p.postDate) = :year')
   ->andWhere('MONTH(p.postDate) = :month')
   ->andWhere('DAY(p.postDate) = :day');

$qb->setParameter('year', $year)
   ->setParameter('month', $month)
   ->setParameter('day', $day);

    $datetime = new \DateTime("now");
    $month = $datetime->format('m');
    echo $month;

doctrine:


orm:
    auto_generate_proxy_classes: %kernel.debug%
    entity_managers:
        default:
            auto_mapping: true
            dql:
                datetime_functions:
                    month: Xy\TestBundle\Dql\Month
                    year: Xy\TestBundle\Dql\Year
                    day: Xy\TestBundle\Dql\Day
Error: User Rate Limit Exceeded gperriard
Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededHereError: User Rate Limit Exceeded
2

public function getWithStartingPremium(\DateTime $datetime)
{
    $qb = $this->createQueryBuilder('c');

    return $qb
        ->innerJoin('c.collegePremium', 'cp')
        ->where($qb->expr()->eq('cp.start', ':date'))
        ->setParameters(array(
            'date' => $datetime->format('Y-m-d'),
        ))
        ->getQuery()
        ->getResult();
}
12

DATE_DIFF(date1, date2)

$result = $this->createQueryBuilder('l')
    ->where('DATE_DIFF(l.startDate, CURRENT_DATE()) = 0')
13

$today_startdatetime = \DateTime::createFromFormat( "Y-m-d H:i:s", date("Y-m-d 00:00:00") );
$today_enddatetime = \DateTime::createFromFormat( "Y-m-d H:i:s", date("Y-m-d 23:59:59") );

$em = \Zend_Registry::get('em');
$qb_1 = $em->createQueryBuilder();
$q_1 = $qb_1->select('wsh')
    ->from('\Entities\wishes', 'wsh')
    ->where('wsh.created_at >= :today_startdatetime')
    ->andWhere('wsh.created_at <= :today_enddatetime');


$q_1->setParameter('today_startdatetime', $today_startdatetime);
$q_1->setParameter('today_enddatetime', $today_enddatetime);
$result= $q_1->getQuery ()->getResult ();
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Похожие вопросы