0

教義でデータを取得する際に問題があります。生成されたクエリは私が必要としているもののようです (_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);
}

小枝 :

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

編集:ここで尋ねられたように、私のauthorsListのものがあります:

private $authorsList;

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

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

EDIT 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();
4

1 に答える 1

1

getWeezList メソッドで leftjoin を使用すると、1 つのクエリだけを使用して同じ結果を得ることができます。

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);          
}
于 2013-02-17T14:21:38.693 に答える