Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Date 条令';s日期字段-如何编写查询_Date_Symfony_Doctrine Orm - Fatal编程技术网

Date 条令';s日期字段-如何编写查询

Date 条令';s日期字段-如何编写查询,date,symfony,doctrine-orm,Date,Symfony,Doctrine Orm,我用的是Symfony2和学说 我有一个日期字段,它是: /** * @ORM\Column(type="date") */ protected $date; 在我的表单中,我使用一个文本字段来避免使用Chrome默认日期选择器,但我在数据库中插入日期时间对象: if ($request->isMethod('POST')) { $form->bind($request); //Convert string date to DateTim

我用的是Symfony2和学说

我有一个日期字段,它是:

/**
  * @ORM\Column(type="date")
  */
 protected $date;
在我的表单中,我使用一个文本字段来避免使用Chrome默认日期选择器,但我在数据库中插入日期时间对象:

if ($request->isMethod('POST')) {
        $form->bind($request);

        //Convert string date to DateTime object and send it to database as object
        $dateObj = \DateTime::createfromformat('d-m-Y', $expense->getDate());
        $expense->setDate($dateObj);
        // ...
然后我想查找所有具有特定日期的项目:

public function findExpensesForDate($user, $date)
{
    $q = $this
        ->createQueryBuilder('e')
        ->where('e.date = :date')
        ->andWhere('e.user = :user')
        ->setParameter('date', $date)
        ->setParameter('user', $user)
         ->getQuery();

    return $q->getResult();
}
这样称呼它:

$expenses_for_today = $this->repository->findExpensesForDate($this->user, $today);

$today = new /DateTime();
并在

$today_obj = new /DateTime();
$today = $today_obj->format('Y-m-d'); 
那么,为什么当我把日期作为对象时,它不起作用呢?使用date Field的原因不是为了利用DateTime对象进行查询吗?我想我错过了一些琐碎和重要的事情,但我就是看不到什么,或者我对情况不是很了解。我的理解是这样的:字段的类型是date,所以我应该在其中插入DateTime对象,当查询时,我也应该向您提供DateTime对象。你能帮我修一下吗

备注:我尝试将字段更改为datetime:

 /**
  * @ORM\Column(type="datetime")
  */
 protected $date;
但是没有变化


使用字符串进行查询是否合适?这样查询时使用对象会有好处吗?

我认为这是因为DateTime对象太过具体,并且也有小时、分钟和秒,所以它不能等于数据库中的注册日期

如果要使用DateTime对象,需要获取一天的开始日期和结束日期,才能获取当天的所有结果!您必须比较一个日期间隔,才能得到它之间的所有日期

首先,获取当天的开始和结束日期(为了简化,我们将以第二天的开始为基础确定结束日期,并将其排除在请求中):

并修改您的方法:

public function findExpensesForDate($user, $fromDate, $toDate)
{
    $q = $this
        ->createQueryBuilder('e')
        ->where('e.date >= :fromDate')
        ->andWhere('e.date < :toDate')
        ->andWhere('e.user = :user')
        ->setParameter('fromDate', $fromDate)
        ->setParameter('toDate', $toDate)
        ->setParameter('user', $user)
         ->getQuery();

    return $q->getResult();
}

因此,您将获得2013-06-10 00:00:002013-06-11 00:00:00(不包括在内)之间的所有日期,因此当天的结果

正如其他人所提到的,这是由于日期时间中的时间造成的

但我为什么要回答

因为我想让您了解
$qb->expr()->between()
函数

下面的代码片段来自我的
ReservationRepository
,我需要在其中查找给定日期的预订(
$today
)。我的预订同时具有
dateFrom
dateTo
属性

    if(null !== $today) {
        $today0 = new \DateTime($today->format("Y-m-d"));
        $today23 = new \DateTime($today->format("Y-m-d"));

        $today0->setTime(0,0,0);
        $today23->setTime(23,59,59);

        $qb->where($qb->expr()->orX(
            $qb->expr()->orX(
                $qb->expr()->between('r.dateFrom', ':today0', ':today23'),
                $qb->expr()->between('r.dateTo', ':today0', ':today23')
            ),
            $qb->expr()->andX(
                $qb->expr()->lte('r.dateFrom', ':today23'),
                $qb->expr()->gte('r.dateTo', ':today0')
            )
        ));
        $qb->setParameter('today0', $today0);
        $qb->setParameter('today23', $today23);
    }

函数
between()
在本例中为我保存了两行代码,我发现它的可读性更强

对不起,我不明白您的要求:事实上,问题在于日期。以这种方式设置tint$toDate:
$toDate=new\DateTime()$toDate->修改(“+1天”)$toDate->setTime(0,0,0)修复了该问题。非常感谢你!
$expenses_for_today = $this->repository->findExpensesForDate($this->user, $fromDate, $toDate);
    if(null !== $today) {
        $today0 = new \DateTime($today->format("Y-m-d"));
        $today23 = new \DateTime($today->format("Y-m-d"));

        $today0->setTime(0,0,0);
        $today23->setTime(23,59,59);

        $qb->where($qb->expr()->orX(
            $qb->expr()->orX(
                $qb->expr()->between('r.dateFrom', ':today0', ':today23'),
                $qb->expr()->between('r.dateTo', ':today0', ':today23')
            ),
            $qb->expr()->andX(
                $qb->expr()->lte('r.dateFrom', ':today23'),
                $qb->expr()->gte('r.dateTo', ':today0')
            )
        ));
        $qb->setParameter('today0', $today0);
        $qb->setParameter('today23', $today23);
    }