Doctrine2标准表达两次之间

时间:2015-04-27 14:37:08

标签: datetime doctrine-orm

我有一个实体"公司"与开放时间的一对多关联。开放时间实体有两个类型时间字段(开始和结束)。现在我想写一个公司方法" isCurrentlyOpen"它返回一个布尔值,但我无法得到它......

公司

class Company {
    /**
     * @var \Gastronome\Entity\OpeningHours[]
     * 
     * @ORM\OneToMany(targetEntity="Gastronome\Entity\OpeningHours", mappedBy="gastronome", fetch="EAGER")
     * @ORM\OrderBy({"weekday" = "ASC", "start" = "ASC"})
     */
    private $openingHours;

    public function getOpeningHours() {
        return $this->openingHours;
    }

    public function getTodaysOpeningHours() {
        $criteria = Criteria::create()
            ->where(Criteria::expr()->eq('weekday', (int) date('w')));

        return $this->getOpeningHours()->matching($criteria);
    }

    public function isCurrentlyOpen() {
        $criteria = Criteria::create()
            ->where(Criteria::expr()->gt('start', new \DateTime()));
            ->andWhere(Criteria::expr()->lt('end', new \DateTime()));

        return count($this->getTodaysOpeningHours()->matching($criteria)) > 0;
    }
}

OpeningHours

class OpeningHours {

    /**
     * @ORM\Column(type="time", nullable=false)
     */
    private $start;

    /**
     * @ORM\Column(type="time", nullable=false)
     */
    private $end;

    /**
     * @ORM\Column(type="integer", nullable=false)
     */
    private $weekday;

    /**
     * @var \Gastronome\Entity\Gastronome
     * 
     * @ORM\ManyToOne(targetEntity="Gastronome\Entity\Gastronome", inversedBy="openingHours")
     * @ORM\JoinColumn(name="gastronome_id", referencedColumnName="id", nullable=false)
     */
    private $gastronome;
}

0 个答案:

没有答案