Yako
Yako

Reputation: 3484

Query result found but not displayed in Symfony

Well, I don't really see this one. This is a simple page where I try to display results of a joined query.

Here is the controller code :

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
    ));

Here is the query :

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();
}

Here is the twig code :

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

Here is the Page entity :

<?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;
}
}

And the MapItem entity :

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;
}
}

No result is displayed, but there should be one!

I don't have any exception, no typo mistakes I guess

I checked the profiler to read the actual queries performed ; I tested them with PhpMyAdmin, and none of them have no result.

It's a very simple and basic case. So, what did I did wrong ? Thanks :)

Upvotes: 1

Views: 825

Answers (1)

Snroki
Snroki

Reputation: 2444

So the thing is you've a one-to-many on your mapItems, so doctrine will return you an arrayCollection.

Your mapItems wasn't displayed in twig because you have to make your for loop on pageWithMapItems.mapItems, if you do it directly on pageWithMapItems it'll not work because your pageWithMapItems variable contain un object of page and not an array.

So this should work:

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

Hope i'm clear !

Upvotes: 3

Related Questions