Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony/Doctrine : Query seems to be good but I still get a NULL
    primarykey
    data
    text
    <p>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 :)</p> <p>My entity : </p> <pre><code>&lt;?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-&gt;book = $book; return $this; } /** * Get book * * @return \Books */ public function getBook() { return $this-&gt;book; } /** * Set author * * @param \Authors $author * @return Wrote */ public function setAuthor($author) { $this-&gt;author = $author; return $this; } /** * Get author * * @return \Authors */ public function getAuthor() { return $this-&gt;author; } } </code></pre> <p>My Repository :</p> <pre><code>class WroteRepository extends EntityRepository { public function authorFromBook($book) { return $this-&gt;createQueryBuilder('w') -&gt;addSelect('a') -&gt;leftJoin('w.author', 'a') -&gt;where('w.book = :book') -&gt;setParameter('book', $book) -&gt;getQuery() -&gt;getResult(); } } </code></pre> <p>Action from the controller :</p> <pre><code>public function feedAction() { $user = $this-&gt;container-&gt;get('security.context')-&gt;getToken()-&gt;getUser(); if (!is_object($user) || !$user instanceof Users) { throw new AccessDeniedException('This user does not have access to this section.'); } $weezList = $this-&gt;getDoctrine() -&gt;getManager() -&gt;getRepository('WkSiteBundle:Weez') -&gt;getWeezList($user-&gt;getId()); foreach ($weezList as $weez) { $authorList = $this-&gt;getDoctrine() -&gt;getManager() -&gt;getRepository('WkSiteBundle:Wrote') -&gt;authorFromBook($weez-&gt;getBook()); $weez-&gt;setAuthorsList($authorList); } $template_value = array( 'weezList' =&gt; $weezList, ); return $this-&gt;render('WkSiteBundle:Default:feed.html.twig', $template_value); } </code></pre> <p>Twig : </p> <pre><code> {% for weez in weezList %} {{ dump(weez.authorsList) }} {% for record in weez.authorsList %} {% for au in record.author%} yo {{ au }} {% endfor %} {% endfor %} {% endfor %} </code></pre> <p>result : </p> <pre><code>array(1) { [0]=&gt; object(Wk\SiteBundle\Entity\Wrote)#415 (2) { ["book":"Wk\SiteBundle\Entity\Wrote":private]=&gt; object(Proxies\__CG__\Wk\SiteBundle\Entity\Books)#422 (5) { ["__isInitialized__"]=&gt; bool(true) ["bookId":"Wk\SiteBundle\Entity\Books":private]=&gt; float(500) ["title":"Wk\SiteBundle\Entity\Books":private]=&gt; string(16) "The Lean Startup" ["author":"Wk\SiteBundle\Entity\Books":private]=&gt; int(214) ["image":"Wk\SiteBundle\Entity\Books":private]=&gt; string(97) "http://bks7.books.google.fr/books?id=r9x-OXdzpPcC&amp;printsec=frontcover&amp;img=1&amp;zoom=5&amp;source=gbs_api" } ["author":"Wk\SiteBundle\Entity\Wrote":private]=&gt; NULL } } </code></pre> <p>You can see at the end : </p> <pre><code>["author":"Wk\SiteBundle\Entity\Wrote":private]=&gt; NULL </code></pre> <p>EDIT : As asked here are my authorsList stuff :</p> <pre><code>private $authorsList; public function setAuthorsList($listAuthor) { $this-&gt;authorsList = $listAuthor; } public function getAuthorsList() { return $this-&gt;authorsList; } </code></pre> <p>EDIT 2 : (Change I did with the help of a.aitboudad's answer)</p> <pre><code> $qb = $this-&gt;_em-&gt;createQueryBuilder(); $qb-&gt;select('a') -&gt;from('WeezbookSiteBundle:Authors', 'a') -&gt;where ('a.authorId IN (SELECT a2.authorId FROM WeezbookSiteBundle:Wrote w JOIN w.author a2 WHERE w.book = :book)') -&gt;setParameter('book', $book); return $qb-&gt;getQuery() -&gt;getResult(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload