学说选择查询日期sqlite(syfmony)

时间:2018-11-13 12:47:03

标签: php symfony doctrine-orm sqlite

我想通过带学说绑定的php datetime对象查询数据库日期字段。我在日期数据库字段类型上没有任何结果。它适用于datetime db字段类型。 在mysql和postgres上有效。

在Sqlite上,我得到了这种奇怪的行为。该问题似乎与仅在日期字段进行相等比较有关。

我在哪里错了?

这是我的实体类:

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * App\Entity\Customer
 *
 * @ORM\Entity()
 * @ORM\Table(name="Customer")
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"base":"BaseCustomer" "extended":"Customer"})
 */
class BaseCustomer
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="date")
     */
    protected $dateofbirth;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    protected $lastlogin;

在控制器中:

    $datestrchk = "1980-02-05";

    $datedtchk = \DateTime::createFromFormat("Y-m-d H:i:s", $datestrchk . " 00:00:00");
    $qb = $this->get("doctrine")->getManager()->createQueryBuilder()
            ->select(array("Customer"))
            ->from("App:Customer", "Customer")
            ->where("Customer.dateofbirth = :data")
            ->setParameter("data", $datedtchk);
    $ret = $qb->getQuery()->getResult();
    echo count($ret); // NOT FOUND

    $datedtchk = \DateTime::createFromFormat("Y-m-d", $datestrchk);
    $qb = $this->get("doctrine")->getManager()->createQueryBuilder()
            ->select(array("Customer"))
            ->from("App:Customer", "Customer")
            ->where("Customer.dateofbirth = :data")
            ->setParameter("data", $datedtchk);
    $ret = $qb->getQuery()->getResult();
    echo count($ret); // NOT FOUND

    $qb = $this->get("doctrine")->getManager()->createQueryBuilder()
            ->select(array("Customer"))
            ->from("App:Customer", "Customer")
            ->where("Customer.dateofbirth = :data")
            ->setParameter("data", $datestrchk);
    $ret = $qb->getQuery()->getResult();
    echo count($ret); // OK

    $date1start = \DateTime::createFromFormat("Y-m-d H:i:s", "1980-02-04 23:59:59");
    $date1end = \DateTime::createFromFormat("Y-m-d H:i:s", "1980-02-05 00:00:00");

    $qb = $this->get("doctrine")->getManager()->createQueryBuilder()
            ->select(array("Customer"))
            ->from("App:Customer", "Customer")
            ->where("Customer.dateofbirth >= :data1start AND Customer.dateofbirth <= :data1end")
            ->setParameter("data1start", $date1start)
            ->setParameter("data1end", $date1end);
    $ret = $qb->getQuery()->getResult();
    echo count($ret); // OK

    $date2start = \DateTime::createFromFormat("Y-m-d H:i:s", "1980-02-05 00:00:00");
    $date2end = \DateTime::createFromFormat("Y-m-d H:i:s", "1980-02-05 00:00:01");

    $qb = $this->get("doctrine")->getManager()->createQueryBuilder()
            ->select(array("Customer"))
            ->from("App:Customer", "Customer")
            ->where("Customer.dateofbirth >= :data2start AND Customer.dateofbirth <= :data2end")
            ->setParameter("data2start", $date2start)
            ->setParameter("data2end", $date2end);
    $ret = $qb->getQuery()->getResult();
    echo count($ret); // NOT FOUND

代码:https://github.com/ComuneFI/BiCoreBundle (客户->客户)

Symfony 3.4.18

Sqlite数据库版本3.22

ubuntu 18.04上的Php 7.2.12

1 个答案:

答案 0 :(得分:0)

相同的问题。尝试->setParameter("data2start", $date2start->format('Ymd H:i:s'))