Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to execute JOIN query with 3 tables, order by count of ID
    primarykey
    data
    text
    <p>I have 3 tables: <strong>Event, EventSeans, Firms</strong>. I need to return Firm, and order by count Event in this Firm Event entity:</p> <pre><code> /** * @ORM\OneToMany( * targetEntity="EventSeans", * cascade={"persist", "remove", "merge"}, * mappedBy="event" * ) * @ORM\OrderBy({"start_time"= "ASC"}) */ protected $seanses; </code></pre> <p>EventSeans entity:</p> <pre><code> /** * @ORM\ManyToOne( * targetEntity="Application\GidBundle\Entity\Firm") */ protected $place; /** * @ORM\ManyToOne( * targetEntity="Event", * inversedBy="seanses" * )* */ protected $event; </code></pre> <p>I tried this:</p> <pre><code> $qb -&gt;select('f, count(e.id) cnt') -&gt;from('AfishaBundle:Event', 'e') -&gt;from('GidBundle:Firm', 'f') -&gt;leftJoin('e.seanses', 's') -&gt;where('s.place = f.id') -&gt;orderBy('cnt','ASC') -&gt;setMaxResults(3) ; </code></pre> <p><strong>EDIT</strong> i add relation in Firm Entity</p> <pre><code> /** * @ORM\OneToMany( * targetEntity="Application\AfishaBundle\Entity\EventSeans", * cascade={"persist", "remove", "merge"}, * mappedBy="place" * ) * @ORM\OrderBy({"start_time"= "ASC"}) */ protected $seanses; </code></pre> <p>and my query is </p> <pre><code> $qb -&gt;select('f, count(e.id) cnt') -&gt;from('GidBundle:Firm', 'f') -&gt;leftJoin('f.seanses','s') -&gt;leftJoin('s.event', 'e') -&gt;orderBy('cnt', 'DESC') -&gt;groupBy('f.id') -&gt;setMaxResults(20) ; </code></pre> <p>But cnt - is a count of my seanses... It's nothing changes:(</p> <p><strong>add</strong></p> <pre><code>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`); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload