Symfony2 datetime queryBuilder

后端 未结 3 1204
面向向阳花
面向向阳花 2021-02-05 08:01

I have 2 DateTime classes in Symfony2 project. I have entity Stat, in which have $date property.

/**
 * @ORM\\Column(type=\"da         


        
3条回答
  •  终归单人心
    2021-02-05 08:25

    A QueryBuilder is a good solution.

    But you can use

    ->andWhere($qb->expr()->between('s.date', ':date_from', 'date_to'))
    

    or

    ->andWhere($qb->expr()->andX(array(
        $qb->expr()->gte('s.date', ':date_from'),
        $qb->expr()->lte('s.date', ':date_to'))
    )
    

    The $qb->expr()->andX is usefull if you don't want to have some WTF when you add some andWhere or orWhere.

    Here you have doctrine2 documentation for queryBuilder

    You can also use setParameters method for your parameters

    ->setParameters(array(
        'date_from' => $date_from,
        'date_to'   => $date_to,
    ))
    

提交回复
热议问题