PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, May 11, 2022

[FIXED] How to get doctrine2 table alias?

 May 11, 2022     doctrine-orm, symfony     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing