如何在findOneBy中使用'compare'句子?

时间:2013-05-01 10:11:32

标签: mysql doctrine-orm

$em = $this->getDoctrine()->getEntityManager();
$repository = $em->getRepository('UserBundle:items');
$items = $repository->findOneBy(
    array('isCancel' => false,
          'user' => $username,
          array('toDate' => 'ASC'));

我想将比较功能添加为

  where registeredtime > now()

我可以将比较句与findOneBy一起使用吗?

或者我必须编写整个sql?

2 个答案:

答案 0 :(得分:1)

您可以按照以下方式执行此操作

$em = $this->getDoctrine()->getEntityManager();
$repository = $em->getRepository('UserBundle:items');
$items = $repository->findOneBy(
    array('isCancel' => false,
    'user' => $username,
    array('toDate' => 'ASC'));
$criteria = Criteria::create()->where(Criteria::expr()->gt("registeredtime", now()));
$filteredItems = $items->matching($criteria);

答案 1 :(得分:1)

你可以尝试一下:

...

$startDate = new DateTime();

$qb   = $repository->createQueryBuilder('e');
$event = $qb
    ->andWhere($qb->expr()->eq('e.start_date', ':start_date'))
    ->setParameter('start_date', $startDate)
    ->getQuery()
        ->getOneOrNullResult();