查询结果在 Symfony 中找到但未显示



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

这是控制器代码:

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 bndmyBundleEntity;
use DoctrineORMMapping as ORM;
/**
* bndmyBundleEntityPage
*
* @ORMTable()
* @ORMEntity(repositoryClass="bndmyBundleEntityPageRepository")
*/
class Page
{
/**
 * @var integer $id
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @ORMOneToMany(targetEntity="bndmyBundleEntityRoute", mappedBy="page")
 */
private $routes;
/**
 * @ORMOneToMany(targetEntity="bndmyBundleEntityMapItem", mappedBy="page")
 */
private $mapItems;
/**
 * @var smallint $number
 *
 * @ORMColumn(name="number", type="smallint")
 */
private $number;
/**
 * @var string $background
 *
 * @ORMColumn(name="background", type="string", length=255, nullable="true")
 */
private $background;
/**
 * @var string $type
 *
 * @ORMColumn(name="type", type="string", length=30)
 */
private $type;
/**
 * @var string $description
 *
 * @ORMColumn(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 DoctrineCommonCollectionsArrayCollection();
    $this->mapItems = new DoctrineCommonCollectionsArrayCollection();
}
/**
 * Add routes
 *
 * @param bndmyBundleEntityRoute $routes
 */
public function addRoute(bndmyBundleEntityRoute $routes)
{
    $this->routes[] = $routes;
}
/**
 * Get routes
 *
 * @return DoctrineCommonCollectionsCollection 
 */
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 bndmyBundleEntityMapItem $mapItems
 */
public function addMapItem(bndmyBundleEntityMapItem $mapItems)
{
    $this->mapItems[] = $mapItems;
}
/**
 * Get mapItems
 *
 * @return DoctrineCommonCollectionsCollection 
 */
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;
}
}

和地图项实体:

namespace bndmyBundleEntity;
use DoctrineORMMapping as ORM;
/**
 * bndmyBundleEntityMapItem
 *
 * @ORMTable()
 * @ORMEntity(repositoryClass="bndmyBundleEntityMapItemRepository")
 */
class MapItem
{
/**
 * @var integer $id
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @ORMManyToOne(targetEntity="bndmyBundleEntityPage", inversedBy="mapItems")
 * @ORMJoinColumn(nullable=false)
 */
private $page;
/**
 * @var string $type
 *
 * @ORMColumn(name="type", type="string", length=255)
 */
private $type;
/**
 * @var string $latlng
 *
 * @ORMColumn(name="latlng", type="text")
 */
private $latlng;
/**
 * @var string $description
 *
 * @ORMColumn(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 bndmyBundleEntityPage $page
 */
public function setPage(bndmyBundleEntityPage $page)
{
    $this->page = $page;
}
/**
 * Get page
 *
 * @return bndmyBundleEntityPage 
 */
public function getPage()
{
    return $this->page;
}
}

不显示任何结果,但应该有一个!

我没有任何例外,我猜没有错别字错误

我检查了分析器以读取执行的实际查询;我用PhpMyAdmin测试了它们,没有一个没有结果。

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

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

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

所以这应该有效:

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

希望我清楚!

相关内容

  • 没有找到相关文章

最新更新