1

好吧,我真的没有看到这个。这是一个简单的页面,我尝试在其中显示连接查询的结果。

这是控制器代码:

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>

这是页面实体:

<?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 对它们进行了测试,没有一个没有结果。

这是一个非常简单和基本的案例。那么,我做错了什么?谢谢 :)

4

1 回答 1

3

所以问题是你的 mapItems 是一对多的,所以学说会返回一个 arrayCollection。

您的 mapItems 未显示在 twig 中,因为您必须在 pageWithMapItems.mapItems 上进行 for 循环,如果您直接在 pageWithMapItems 上执行此操作,它将不起作用,因为您的 pageWithMapItems 变量包含页面对象而不是数组。

所以这应该工作:

<body>
    {% for mapitem in pageWithMapItems.mapItems %}
        item {{mapitem.id}}<br/>
    {% else %}
         No result
    {% endfor %}
</body>

希望我清楚!

于 2012-10-11T15:14:44.937 回答