Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用3个表执行联接查询,按ID计数排序_Php_Symfony_Doctrine Orm_Dql - Fatal编程技术网

Php 如何使用3个表执行联接查询,按ID计数排序

Php 如何使用3个表执行联接查询,按ID计数排序,php,symfony,doctrine-orm,dql,Php,Symfony,Doctrine Orm,Dql,我有三张桌子: 事件、事件、公司。 我需要返回公司,并在该公司按计数事件订购 事件实体: /** * @ORM\OneToMany( * targetEntity="EventSeans", * cascade={"persist", "remove", "merge"}, * mappedBy="event" * ) * @ORM\OrderBy({"start_time"= "ASC"}) */ protected $seanses; EventS

我有三张桌子: 事件、事件、公司。 我需要返回公司,并在该公司按计数事件订购 事件实体:

 /**
 * @ORM\OneToMany(
 *     targetEntity="EventSeans",
 *     cascade={"persist", "remove", "merge"},
 *    mappedBy="event"
 *  )
 *  @ORM\OrderBy({"start_time"= "ASC"})
 */
protected $seanses;
EventSeans实体:

 /**
 * @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-是我的一个seanses计数。。。没有什么变化:(

添加

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`);

如果我认为您的实体关系定义良好

以下代码应返回与每个公司相关的事件计数

FirmRepository中添加一个包含

       $query = $this->createQueryBuilder('f')
           ->select(' f, count(distinct e.id)')
           ->leftJoin('f.seanses', 's')
           ->leftJoin('s.event', 'e')
           ->groupBy('f.id')
           ->getQuery()
           ->getArrayResult();
其中
事件
定义了从
事件到
事件的多对一关系,如下所示:

/**
 * @ORM\ManyToOne(targetEntity="Application\GidBundle\Entity\Event")
 */
protected $event;

公司实体没有“seanses”字段。Symfony说“Class Application\GidBundle\entity\Firm没有名为seanses的关联”我尝试了这个,但是count返回seanses的计数$qb->select('f,count(e.id)))->from('GidBundle:Firm','f')->from('AfishaBundle:Event','e'))->leftJoin('e.seanses','s')->where('s.place=f.id')->groupBy('f.id');我想我不太明白你的意思:(我已经更新了我的帖子,介绍了EventSeans()之间的所有关系),事件和公司从您的问题中,我了解到公司和事件实体都有seanses属性!请查看您的问题。否。只有事件具有seanses属性。每个事件都有许多事件seanses。每个事件seans都有许多公司。但公司和事件seanses之间没有关系。