Doctrine2 Criteria Expression Between Two Times
I have a company "Company" with one to many associations during opening hours. The open time object has two fields (start and end) of the time type. Now I want to write a proprietary isCurrentlyOpen method that returns a boolean, but I can't get it ...
Company
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;
}
+3
source to share
No one has answered this question yet
Check out similar questions: