Symfony / Doctrine:查询似乎很好,但我仍然得到一个NULL

时间:2013-02-15 10:46:32

标签: symfony doctrine-orm

我在使用doctrine检索数据时遇到了问题。生成的查询似乎是我需要的(从我在_profiler中看到的)但是当我试图显示它时,我得到了一个很好的'NULL'。我不知道我错过了什么,但它真的很烦人......我真的需要你们:)

我的实体:

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

我的存储库:

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

来自控制器的动作:

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

结果:

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

你可以在最后看到:

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

编辑: 正如我在这里提到的那样是我的作者列表:

private $authorsList;

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

public function getAuthorsList() {
    return $this->authorsList;
}
编辑2 :(我在a.aitboudad的回答的帮助下做了改变)

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

1 个答案:

答案 0 :(得分:1)

在getWeezList方法中使用leftjoin只能使用一个查询来提供相同的结果:

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

删除此代码:

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

    $weez->setAuthorsList($authorList);          
}