我使用的是ZF2版本2.5.0和教条版本~~ 2.5
我有三个实体,即事件,排名和国家如下:
<?php
namespace SampleProject\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use SampleProject\Entity\Country;
use SampleProject\Entity\Ranking;
class Event
{
/**
*
* @ORM\Id
* @ORM\Column(name="eventID", type="integer", precision=0, nullable=false)
* @var int $eventID
*/
protected $eventID;
/**
* @ORM\Column(name="name", type="string", length=50, precision=0, nullable=false)
* @var string $name
*/
protected $name;
/**
* @ORM\OneToMany(targetEntity="SampleProject\Entity\Ranking", mappedBy="event")
* @var ArrayCollection $rankings
*/
protected $rankings;
/**
* @ORM\ManyToOne(targetEntity="SampleProject\Entity\Country", inversedBy="events", fetch="EXTRA_LAZY", cascade={"detach"})
* @ORM\JoinColumn(name="countryIso", referencedColumnName="countryIso", nullable=false)
* @var \SampleProject\Entity\Country $country
*/
protected $country;
/**
* Initializes doctrine collections, called from constructor in entity class
*/
protected function initializeCollections()
{
$this->rankings = new ArrayCollection();
}
public function setEventID($eventID)
{
$this->eventID = (int)$eventID;
return $this;
}
public function getEventID()
{
return $this->eventID;
}
public function setName($name)
{
$this->name = (string)$name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setCountry(Country $country)
{
$this->country = $country;
return $this;
}
public function getCountry()
{
return $this->country;
}
public function addRanking(Ranking $ranking)
{
$this->rankings[] = $ranking;
return $this;
}
public function getRankings()
{
return $this->rankings;
}
}
?>
<?php
namespace SampleProject\Country;
use SampleProject\Entity\Event;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
class Country
{
/**
*
* @ORM\Id
* @ORM\Column(name="countryIso", type="string", length=3, precision=0, nullable=false)
* @var string $countryIso
*/
protected $countryIso;
/**
* @ORM\Column(name="name", type="string", length=50, precision=0, nullable=false)
* @var string $name
*/
protected $name;
/**
* @ORM\OneToMany(targetEntity="SampleProject\Entity\Event", mappedBy="country", fetch="EXTRA_LAZY")
* @var ArrayCollection $events
*/
protected $events;
/**
* Initializes doctrine collections, called from constructor in entity class
*/
protected function initializeCollections()
{
$this->events = new ArrayCollection();
}
public function setCountryIso($countryIso)
{
$this->countryIso = (string) $countryIso;
return $this;
}
public function getCountryIso()
{
return $this->countryIso;
}
public function setName($name)
{
$this->name = (string) $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function addEvent(Event $event)
{
$this->events->add($event);
return $this;
}
public function getEvents()
{
return $this->events;
}
}
?>
<?php
namespace SampleProject\Ranking;
use Doctrine\ORM\Mapping as ORM;
use SampleProject\Entity\Event;
class Ranking
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
* @ORM\Column(type="integer", precision=0, nullable=false)
* @var int $rankingID
*/
protected $rankingID;
/**
* @ORM\Column(name="rank", type="integer", precision=0, nullable=true)
* @var int $rank
*/
protected $rank;
/**
* @ORM\ManyToOne(targetEntity="SampleProject\Entity\Event", inversedBy="rankings", fetch="LAZY")
* @ORM\JoinColumn(name="eventID", referencedColumnName="eventID", nullable=false)
* @var \SampleProject\Entity\Event $event
*/
protected $event;
/**
* @ORM\Column(name="locale", type="string", length=5, precision=0, nullable=true)
* @var string $locale
*/
protected $locale;
public function getRankingID()
{
return $this->rankingID;
}
public function setRank($rank)
{
$this->rank = (int) $rank;
return $this;
}
public function getRank()
{
return $this->rank;
}
public function setEvent(Event $event)
{
$this->event = $event;
return $this;
}
public function getEvent()
{
return $this->event;
}
public function getLocale()
{
return $this->locale;
}
public function setLocale($locale)
{
$this->locale = $locale;
}
}
?>
一个事件属于一个国家,一个国家可以有多个事件。
一个事件具有多个排名,具体取决于区域设置,即(en,nl等)。
我正在使用ZF2和doctrine按排名按升序排列事件列表。
在内部执行以下查询以显示列出的事件:
SELECT a0_.name AS name_3, a0_.countryIso AS countryIso_37, COALESCE(a1_.rank, 99999) AS sclr_34
FROM Event a0_
LEFT JOIN Ranking a1_ ON a0_.eventID = a1_.eventID AND (a1_.locale = 'en' OR a1_.locale IS NULL)
INNER JOIN Country a2_ ON a0_.countryIso = a2_.countryIso
GROUP BY a0_.eventID
ORDER BY sclr_34 ASC, a0_.name ASC
所以结果如下:
event1 BEL 1
event2 AUS 2
event3 AUS 3
event4 AUS 4
event5 NLD 5
event6 NLD 6
event7 ESP 7
但是现在,我希望每个国家/地区显示每个事件的事件。所以输出就像:
event1 BEL 1
event2 AUS 2
event5 NLD 5
event7 ESP 7
所以,我相信查询现在如下:
SELECT * FROM (
SELECT a0_.name AS name_3, a0_.countryIso AS countryIso_37, COALESCE(a1_.rank, 99999) AS sclr_34
FROM Event a0_
LEFT JOIN Ranking a1_ ON a0_.eventID = a1_.eventID AND (a1_.locale = 'en' OR a1_.locale IS NULL)
INNER JOIN Country a2_ ON a0_.countryIso = a2_.countryIso
GROUP BY a0_.eventID
ORDER BY sclr_34 ASC, a0_.name ASC
) as t
GROUP BY t.countryIso_37 ORDER BY t.sclr_34 ASC limit 6;
但是,对于ZF2和学说,我无法准备上述查询。
如何使用ZF2和doctrine实现这一目标?