2013-02-21 41 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