Issue
I want to create a method in my a Doctrine2 repository class that takes a QueryBuilder and adds some extra clauses, one of which is an inner join.
How can I find out the table alias that was used to instantiate the querybuilder? Is this something discoverable or should it be a convention across the codebase (and therefore a potential source of bugs)?
My client code is:
public function getPasswordAction($id)
{
$user = $this->get('security.context')->getToken()->getUser();
$repository = $this->getDoctrine()
->getRepository('TenKPwLockerBundle:Password');
$query = $repository->createQueryBuilder('p')
->where('id = :id')
->setParameter('id', $id);
$query = $repository->userCanReadRestriction($query, $user);
...
and my repository class contains:
public function userCanReadRestriction(\Doctrine\ORM\QueryBuilder $builder, \TenK\UserBundle\Entity\User $user)
{
// where can I get 'p' from?
return $builder->innerJoin('p.shares', 's')
->where('createdBy = :creator')
->orWhere('s.toUser = :toId')
->setParameters(array('creator' => $user, 'toUser' => $user));
}
In fact, in the above code, how can I confirm that the QueryBuilder is working with the Password Entity at all?
Solution
You can retrive the select
part of your QueryBuilder by calling the getDqlPart('select')
method.
More information here.
Then, look how doctrine does to parse select part here.
You can probably do the same to know if the table associated to your repository is called and what is its alias.
Answered By - Olivier Dolbeau Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.