2013-02-21 36 views
1

我想讓我的第一個查詢symfony2,所以這個問題可能會很愚蠢。symfony2第一條準則查詢

我有我的表與用戶和表(朋友)請求。在申請表我存儲

ID,暱稱(ID),odobera(其他用戶ID-我跟着誰),時間

而且缺口性質的樣子:

/** 
* @ORM\ManyToOne(targetEntity="TB\UserBundle\Entity\User", inversedBy="Requests") 
* @ORM\JoinColumn(name="nick", referencedColumnName="id") 
*/ 

,現在我想重建我到新的symfony的一個老功能查詢:

$select_all=mysql_query("SELECT re.*,tb.rank,tb.profile_picture_ultra_small,tb.country,tb.typ_hraca,tb.rank 
          FROM requests as re 
          INNER JOIN tb_users as tb 
          ON re.nick=tb.nick 
          WHERE re.odobera='$nick' AND re.nick<>'$nick' ORDER BY re.id DESC LIMIT 50"); 

我開始只是簡單:

public function getMyFollowers($my_id) 
{ 
    $qb = $this->createQueryBuilder('mf') 
       ->select('mf') 
       ->where('mf.odobera = :my_id') 
       ->addOrderBy('mf.time','DESC') 
       ->setParameter('my_id', $my_id) 
       ->setMaxResults(50); 

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

我沒有那裏的內部連接,用於選擇跟隨我的用戶profile_picture +我的結果看起來很奇怪,現在我傾倒它:(如你所見,沒有用戶的ID(nick)跟着我)

"Proxies\__CG__\TB\UserBundle\Entity\User" ["odobera"]=> int(25) ["time"]=> int(1359128544) ["viewed"]=> bool(true) } [6]=> object(stdClass)#624 (6) { ["__CLASS__"]=> string(33) "TB\RequestsBundle\Entity\Requests" ["id"]=> int(57443) ["nick"]=> string(40) "Proxies\__CG__\TB\UserBundle\Entity\User" ["odobera"]=> int(25) ["time"]=> int(1359124714) ["viewed"]=> bool(true) } [7]=> object(stdClass)#625 (6) { ["__CLASS__"]=> string(33) "TB\RequestsBundle\Entity\Requests" ["id"]=> int(57013) ["nick"]=> string(40) "Proxies\__CG__\TB\UserBundle\Entity\User" ["odobera"]=> int(25) ["time"]=> int(1359047459) ["viewed"]=> bool(true) } [8]=> object(stdClass)#626 (6) { ["__CLASS__"]=> string(33) "TB\RequestsBundle\Entity\Requests" ["id"]=> int(56766) ["nick"]=> string(40) 

有什麼想法嗎? :P Thx!

我想這添加到我的實體,如你所說,但改變了名稱,但我認爲它根本不會讓SENCE和我的查詢語句加載787-9在一個時間只及回收一個用戶+我現在不能加載頁面... 用戶:

/** 
* @ORM\OneToMany(targetEntity="TB\RequestsBundle\Entity\Requests", mappedBy="following") 
* 
**/ 
private $followers; 

請求:

/** 
* @ORM\ManyToOne(targetEntity="TB\UserBundle\Entity\User", inversedBy="followers") 
* @ORM\JoinColumn(name="nick", referencedColumnName="id") 
**/ 

私人$以下;

但是......它沒有SENCE ..

回答

1

您可以定義用戶類添加註釋User類的存儲庫。

@ORM\Entity(repositoryClass="Acme\YourBundle\Entity\UserRepository") 

而且在倉庫裏,你將有:

public function getFollowers($id) 
{ 
    $qb = $this->getEntityManager()->createQueryBuilder(); 
    $qb 
     ->select('user') 
     ->from('YourBundle:User', 'user') 
     ->leftJoin('user.requests', 'friends') 
     ->where('friends.odobera = :id') 
     ->setParameter('id', $id) 
     ->orderBy('friends.time', 'DESC') 
     ->setMaxResults(50); 

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

入住這Custom Repositories

在您的控制器

$em = $this->getDoctrine()->getManager(); 
$entity = $em->getRepository('YourBundle:User')->getFollowers($id); 

映射信息例如

的用戶

/** 
* @ORM\OneToMany(targetEntity="Followers", mappedBy="following") 
* 
**/ 
private $followers; 

在關注者(該表中所定義的追隨者)

/** 
* @ORM\ManyToOne(targetEntity="Users", inversedBy="followers") 
* @ORM\JoinColumn(name="company_id", referencedColumnName="id") 
**/ 
private $following; 
+0

是的,我有它通過捆綁:)嗯產生我的倉庫......這如何查詢我應該使用? $ em = $ this-> getDoctrine() - > getEntityManager(); $ myFollowers = $ em-> getRepository('RequestsBundle:Requests') - > getMyFollowers('25'); – EnchanterIO 2013-02-21 20:57:44

+0

就是這樣;)我加入了答案。 – Axxiss 2013-02-21 21:05:00

+0

好的thx人,但爲什麼getRepository用戶?而不是朋友?我會去嘗試一下。 – EnchanterIO 2013-02-21 21:08:08