Reputation: 8346
I am trying make my first query on symfony2 so the question will be maybe stupid.
I have my table with users and table with (friends) requests. In requests table i store
id,nick(id),odobera(another user Id- who i follow),time
And the nick property looks:
/**
* @ORM\ManyToOne(targetEntity="TB\UserBundle\Entity\User", inversedBy="Requests")
* @ORM\JoinColumn(name="nick", referencedColumnName="id")
*/
And now i want rebuild my old functional query to the new symfony one:
$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");
I started just simply:
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();
}
I dont have there the inner join for selecting profile_picture of user who follow me + my result looks weird when i dump it now: (as you can see there is no ID (nick) of the user who follow me)
"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)
Any ideas please? :P Thx!
i tried add this to my entity as you said but changed the names but i think it doesn't make sence at all and my query was loading 9s in one time and withdrawed just one user + i cant load the page now... USERS:
/**
* @ORM\OneToMany(targetEntity="TB\RequestsBundle\Entity\Requests", mappedBy="following")
*
**/
private $followers;
REQUESTS:
/**
* @ORM\ManyToOne(targetEntity="TB\UserBundle\Entity\User", inversedBy="followers")
* @ORM\JoinColumn(name="nick", referencedColumnName="id")
**/
private $following;
But ... it doesn't make sence..
Upvotes: 1
Views: 880
Reputation: 4789
You can define a Repository for user class adding the annotation to User class.
@ORM\Entity(repositoryClass="Acme\YourBundle\Entity\UserRepository")
And inside the repository you will have:
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();
}
Check this Custom Repositories
On your controller
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('YourBundle:User')->getFollowers($id);
Mapping info example
On Users
/**
* @ORM\OneToMany(targetEntity="Followers", mappedBy="following")
*
**/
private $followers;
On Followers (the table were you defined the followers)
/**
* @ORM\ManyToOne(targetEntity="Users", inversedBy="followers")
* @ORM\JoinColumn(name="company_id", referencedColumnName="id")
**/
private $following;
Upvotes: 1