查找结果但未在Symfony中显示

时间:2012-10-11 09:25:14

标签: symfony doctrine-orm twig

好吧,我真的没看到这一个。 这是一个简单的页面,我尝试显示已加入查询的结果。

这是控制器代码:

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

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

1 个答案:

答案 0 :(得分:2)

所以问题是你的mapItems上有一对多,所以doctrine会返回一个arrayCollection。

你的mapItems没有在twig中显示,因为你必须在pageWithMapItems.mapItems上进行for循环,如果你直接在pageWithMapItems上它,它将无法工作,因为你的pageWithMapItems变量包含非对象的页面而不是数组。

所以这应该有效:

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

希望我很清楚!