BaNz
BaNz

Reputation: 202

Symfony/Doctrine : Query seems to be good but I still get a NULL

I've a problem with retrieving data with doctrine. the query generated seems to be what I need (From what I see in the _profiler/) but when I'm trying to display it I get a nice 'NULL'. I don't know what I'm missing, but it's really annoying... I really need you guys :)

My entity :

<?php
namespace Wk\SiteBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Wrote
*
* @ORM\Table(name="WROTE")
* @ORM\Entity(repositoryClass="Wk\SiteBundle\Entity\WroteRepository")
*/
class Wrote
{
/**
 * @var \Books
 *
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Books")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="book_id", referencedColumnName="book_id")
 * })
 */
private $book;

/**
 * @var \Authors
 *
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Authors")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="author_id", referencedColumnName="author_id")
 * })
 */
private $author;



/**
 * Set book
 *
 * @param \Books $book
 * @return Wrote
 */
public function setBook($book)
{
    $this->book = $book;

    return $this;
}

/**
 * Get book
 *
 * @return \Books 
 */
public function getBook()
{
    return $this->book;
}

/**
 * Set author
 *
 * @param \Authors $author
 * @return Wrote
 */
public function setAuthor($author)
{
    $this->author = $author;

    return $this;
}

/**
 * Get author
 *
 * @return \Authors 
 */
public function getAuthor()
{
    return $this->author;
}
}

My Repository :

class WroteRepository extends EntityRepository { 
public function authorFromBook($book) {
    return $this->createQueryBuilder('w')
        ->addSelect('a')
        ->leftJoin('w.author', 'a')
        ->where('w.book = :book')
            ->setParameter('book', $book)
        ->getQuery()
        ->getResult();
}
}

Action from the controller :

public function feedAction() {
    $user = $this->container->get('security.context')->getToken()->getUser();

    if (!is_object($user) || !$user instanceof Users) {
        throw new AccessDeniedException('This user does not have access to this section.');
    }

    $weezList = $this->getDoctrine()
         ->getManager()
         ->getRepository('WkSiteBundle:Weez')
         ->getWeezList($user->getId());

    foreach ($weezList as $weez) {
        $authorList = $this->getDoctrine()
            ->getManager()
            ->getRepository('WkSiteBundle:Wrote')
            ->authorFromBook($weez->getBook());

        $weez->setAuthorsList($authorList);          
    }

    $template_value = array(
        'weezList' => $weezList,
    );

    return $this->render('WkSiteBundle:Default:feed.html.twig', $template_value);
}

Twig :

 {% for weez in weezList %}
    {{ dump(weez.authorsList) }}
     {% for record in weez.authorsList %} 
        {% for au in record.author%}
            yo {{ au }}
        {% endfor %}
    {% endfor %} 
{% endfor %}

result :

array(1) { [0]=> object(Wk\SiteBundle\Entity\Wrote)#415 (2) {  ["book":"Wk\SiteBundle\Entity\Wrote":private]=> object(Proxies\__CG__\Wk\SiteBundle\Entity\Books)#422 (5) { ["__isInitialized__"]=> bool(true) ["bookId":"Wk\SiteBundle\Entity\Books":private]=> float(500) ["title":"Wk\SiteBundle\Entity\Books":private]=> string(16) "The Lean Startup" ["author":"Wk\SiteBundle\Entity\Books":private]=> int(214) ["image":"Wk\SiteBundle\Entity\Books":private]=> string(97) "http://bks7.books.google.fr/books?id=r9x-OXdzpPcC&printsec=frontcover&img=1&zoom=5&source=gbs_api" } ["author":"Wk\SiteBundle\Entity\Wrote":private]=> NULL } } 

You can see at the end :

["author":"Wk\SiteBundle\Entity\Wrote":private]=> NULL 

EDIT : As asked here are my authorsList stuff :

private $authorsList;

public function setAuthorsList($listAuthor) {
    $this->authorsList = $listAuthor;
}

public function getAuthorsList() {
    return $this->authorsList;
}

EDIT 2 : (Change I did with the help of a.aitboudad's answer)

  $qb = $this->_em->createQueryBuilder();

  $qb->select('a')
     ->from('WeezbookSiteBundle:Authors', 'a')
     ->where ('a.authorId IN (SELECT a2.authorId FROM WeezbookSiteBundle:Wrote w JOIN w.author a2 WHERE w.book = :book)')
        ->setParameter('book', $book);

  return  $qb->getQuery()
                ->getResult();

Upvotes: 0

Views: 378

Answers (1)

a.aitboudad
a.aitboudad

Reputation: 4092

You can use just one query to give the same result by using leftjoin in the getWeezList method:

public function getWeezList($id)
{
    $db = $this->createQueryBuilder('w')
        ->addSelect('a')
        ->leftjoin('w.author', 'a')
        ->where('w.id = :id').
    ...
    ...
    return $db->getQuery()->getResult();
}

in your controller delete this code:

foreach ($weezList as $weez) {
    $authorList = $this->getDoctrine()
        ->getManager()
        ->getRepository('WkSiteBundle:Wrote')
        ->authorFromBook($weez->getBook());

    $weez->setAuthorsList($authorList);          
}

Upvotes: 1

Related Questions