まあ、これはあまり見かけません。これは、結合されたクエリの結果を表示しようとする単純なページです。
コントローラコードは次のとおりです。
public function pageApproachUpdateAction($pageId)
{
$em=$this->getDoctrine()->getEntityManager();
$pageWithMapItems = $em->getRepository('bndmyBundle:Page')->getPageWithMapItems($pageId);
return $this->render('bndmyBundle:test.html.twig', array(
'pageWithMapItems' => $pageWithMapItems
));
クエリは次のとおりです。
public function getPageWithMapItems($pageId) {
$qb = $this->createQueryBuilder('p')
->leftJoin('p.mapItems', 'm')
->where('p.id = :pageId')
->setParameter('pageId', $pageId)
->addSelect('m');
return $qb->getQuery()
->getSingleResult();
}
これが小枝のコードです:
<body>
{% for mapitem in pageWithMapItems %}
item {{mapitem.id}}<br/>
{% else %}
No result
{% endfor %}
</body>
これがPageエンティティです:
<?php
namespace bnd\myBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* bnd\myBundle\Entity\Page
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="bnd\myBundle\Entity\PageRepository")
*/
class Page
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="bnd\myBundle\Entity\Route", mappedBy="page")
*/
private $routes;
/**
* @ORM\OneToMany(targetEntity="bnd\myBundle\Entity\MapItem", mappedBy="page")
*/
private $mapItems;
/**
* @var smallint $number
*
* @ORM\Column(name="number", type="smallint")
*/
private $number;
/**
* @var string $background
*
* @ORM\Column(name="background", type="string", length=255, nullable="true")
*/
private $background;
/**
* @var string $type
*
* @ORM\Column(name="type", type="string", length=30)
*/
private $type;
/**
* @var string $description
*
* @ORM\Column(name="description", type="text", nullable="true")
*/
private $description;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set background
*
* @param string $background
*/
public function setBackground($background)
{
$this->background = $background;
}
/**
* Get background
*
* @return string
*/
public function getBackground()
{
return $this->background;
}
/**
* Set type
*
* @param string $type
*/
public function setType($type)
{
$this->type = $type;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
public function __construct()
{
$this->routes = new \Doctrine\Common\Collections\ArrayCollection();
$this->mapItems = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add routes
*
* @param bnd\myBundle\Entity\Route $routes
*/
public function addRoute(\bnd\myBundle\Entity\Route $routes)
{
$this->routes[] = $routes;
}
/**
* Get routes
*
* @return Doctrine\Common\Collections\Collection
*/
public function getRoutes()
{
return $this->routes;
}
/**
* Set number
*
* @param smallint $number
*/
public function setNumber($number)
{
$this->number = $number;
}
/**
* Get number
*
* @return smallint
*/
public function getNumber()
{
return $this->number;
}
/**
* Add mapItems
*
* @param bnd\myBundle\Entity\MapItem $mapItems
*/
public function addMapItem(\bnd\myBundle\Entity\MapItem $mapItems)
{
$this->mapItems[] = $mapItems;
}
/**
* Get mapItems
*
* @return Doctrine\Common\Collections\Collection
*/
public function getMapItems()
{
return $this->mapItems;
}
/**
* Set description
*
* @param text $description
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* Get description
*
* @return text
*/
public function getDescription()
{
return $this->description;
}
}
そして、MapItemエンティティ:
namespace bnd\myBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* bnd\myBundle\Entity\MapItem
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="bnd\myBundle\Entity\MapItemRepository")
*/
class MapItem
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="bnd\myBundle\Entity\Page", inversedBy="mapItems")
* @ORM\JoinColumn(nullable=false)
*/
private $page;
/**
* @var string $type
*
* @ORM\Column(name="type", type="string", length=255)
*/
private $type;
/**
* @var string $latlng
*
* @ORM\Column(name="latlng", type="text")
*/
private $latlng;
/**
* @var string $description
*
* @ORM\Column(name="description", type="string", length=255)
*/
private $description;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set type
*
* @param string $type
*/
public function setType($type)
{
$this->type = $type;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Set latlng
*
* @param string $latlng
*/
public function setLatlng($latlng)
{
$this->latlng = $latlng;
}
/**
* Get latlng
*
* @return string
*/
public function getLatlng()
{
return $this->latlng;
}
/**
* Set description
*
* @param string $description
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set page
*
* @param bnd\myBundle\Entity\Page $page
*/
public function setPage(\bnd\myBundle\Entity\Page $page)
{
$this->page = $page;
}
/**
* Get page
*
* @return bnd\myBundle\Entity\Page
*/
public function getPage()
{
return $this->page;
}
}
結果は表示されませんが、表示されるはずです。
例外はありません。タイプミスはないと思います。
プロファイラーをチェックして、実行された実際のクエリを読み取りました。PhpMyAdminでテストしましたが、いずれも結果がありません。
これは非常に単純で基本的なケースです。それで、私は何を間違えましたか?ありがとう :)