2013-02-15 55 views
0

我與教條檢索數據有問題。生成的查詢似乎是我所需要的(從我在_profiler /中看到的),但是當我試圖顯示它時,我得到一個很好的'NULL'。我不知道我錯過了什麼,但它真的很煩人...我真的需要你們:)Symfony/Doctrine:查詢似乎是好的,但我仍然得到一個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; 
} 

編輯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

嗨,你可以把這個方法的代碼「setAuthorsList」 – 2013-02-17 10:15:16

+0

當然,我編輯我的帖子;) – BaNz 2013-02-17 12:39:44

回答

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);   
} 
+0

嘿,我試過你的解決方案,但它不能解決我的問題,我試過這種事情,因爲我不確定對象本身是否更新或不,但它似乎是因爲如果我註釋行ŝetAuthorList(..),var的轉儲爲空而不是「半空」。 – BaNz 2013-02-17 21:51:49

+0

我編輯了我的答案,參見上文。 – 2013-02-17 23:09:03

+0

嘿,感謝您的幫助,我無法改變我的查詢方式,因爲它只是不適合,但我嘗試了另一種請求我的數據,它的工作方式!所以你的解決方案並不是我目前所做的,但是我改變了做事的方式,所以謝謝!無論如何,我會接受你的回答。 – BaNz 2013-02-19 21:07:04

相關問題