symfony/symfony — Issue #2443: Doctrine / Symfony Joining tables: unexpected behavior
Author: jwmkrezmien
Created: 2011-10-21 15:16:34+00:00
State: open

I am trying to achieve some efficiency gains by having several records joined from several tables in accordance to the article: http://symfony.com/doc/current/book/doctrine.html#joining-to-related-records. In my model, I have a Tracker that has one Linkset, which in turn has multiple Links. Now, in my Controller I want to loop through those links via:

foreach ($tracker->getLinkset()->getLinks() as $link) etc..

Moreover, the code in my Tracker repository is as follows:

public function findWithLinks(Tracker $tracker)
{
$query = $this->createQueryBuilder('t')
->select('t, ls, l')
->innerJoin('t.linkset', 'ls')
->leftJoin('ls.links', 'l')
->where('t.id = :trackerId')
->setParameters(array(
'trackerId' => $tracker->getId()
));

return $query->getQuery()->getResult();
}

What I would expect is, in the controller, when I loop through the foreach table, that Symfony does not make any additional calls to the database, because I have a Tracker with joined linkset and joined links. However, two additional queries are run to get the Linkset and the Links separately. Here are the mappings of the associated entities:

The Linkset entity:

/**
* @var Link
*
* @ORM\ManyToMany(targetEntity="Link", mappedBy="linkset")
*/
private $links;

The Link entity

/**
* @var Linkset
*
* @ORM\ManyToMany(targetEntity="Linkset", inversedBy="link")
* @ORM\JoinTable(name="linkcombo",
* joinColumns={
* @ORM\JoinColumn(name="link_id", referencedColumnName="id", onDelete="CASCADE")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="linkset_id", referencedColumnName="id", onDelete="CASCADE")
* }
* )
*/
private $linkset;