我有 3 张桌子: Event、EventSeans、Firms。我需要返回公司,并在此公司事件实体中按事件计数排序:
/**
* @ORM\OneToMany(
* targetEntity="EventSeans",
* cascade={"persist", "remove", "merge"},
* mappedBy="event"
* )
* @ORM\OrderBy({"start_time"= "ASC"})
*/
protected $seanses;
EventSeas 实体:
/**
* @ORM\ManyToOne(
* targetEntity="Application\GidBundle\Entity\Firm")
*/
protected $place;
/**
* @ORM\ManyToOne(
* targetEntity="Event",
* inversedBy="seanses"
* )*
*/
protected $event;
我试过这个:
$qb
->select('f, count(e.id) cnt')
->from('AfishaBundle:Event', 'e')
->from('GidBundle:Firm', 'f')
->leftJoin('e.seanses', 's')
->where('s.place = f.id')
->orderBy('cnt','ASC')
->setMaxResults(3)
;
编辑 我在公司实体中添加关系
/**
* @ORM\OneToMany(
* targetEntity="Application\AfishaBundle\Entity\EventSeans",
* cascade={"persist", "remove", "merge"},
* mappedBy="place"
* )
* @ORM\OrderBy({"start_time"= "ASC"})
*/
protected $seanses;
我的查询是
$qb
->select('f, count(e.id) cnt')
->from('GidBundle:Firm', 'f')
->leftJoin('f.seanses','s')
->leftJoin('s.event', 'e')
->orderBy('cnt', 'DESC')
->groupBy('f.id')
->setMaxResults(20)
;
但是cnt - 是我的感觉......它没有任何变化:(
添加
CREATE TABLE IF NOT EXISTS `afisha_event_seanses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`place_id` int(11) DEFAULT NULL,
`event_id` int(11) DEFAULT NULL,
`start_time` datetime NOT NULL,
`allday` tinyint(1) NOT NULL,
`cost` int(11) NOT NULL,
`hall` varchar(255) NOT NULL,
`region` varchar(255) NOT NULL,
`end_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_3164A4B7DA6A219` (`place_id`),
KEY `IDX_3164A4B771F7E88B` (`event_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
ALTER TABLE `afisha_event_seanses`
ADD CONSTRAINT `FK_3164A4B771F7E88B` FOREIGN KEY (`event_id`) REFERENCES `afisha_events` (`id`),
ADD CONSTRAINT `FK_3164A4B7DA6A219` FOREIGN KEY (`place_id`) REFERENCES `gid_firms` (`id`);