0

I have so far 2 queries which are similar:

the first one I use when user is ROLE_REDAC

$qb = $this->createQueryBuilder('a'); $qb->LeftJoin('a.ArticlePhrases','ap') ->addSelect('ap') ->where( $qb->expr()->andX( $qb->expr()->eq('ap.order', '?1'), $qb->expr()->eq('a.author', '?2') ) ) ->setParameters(//... ); 

The second one when user is ROLE_ADMIN

$qb = $this->createQueryBuilder('a'); $qb->LeftJoin('a.ArticlePhrases','ap') ->addSelect('ap') ->where( $qb->expr()->andX( $qb->expr()->eq('ap.order', '?1') ) ) ->setParameters(... )); 

There are not much difference. is it possible to call from the controller the same repository fonction and that this fonction adapt whether the actual user is ROLE_REDAC or ROLE_ADM? if yes how can I do that?

2
  • Pass a parameter (user role) to your custom repository function and then, depending on user role, add or not dql code. As easy as that! Commented Feb 19, 2015 at 13:54
  • can u please provide an exemple DonCallisto, that would be great Commented Feb 19, 2015 at 13:57

1 Answer 1

2

I haven't tested it, but should work

class MyCustomRepository extends EntityRepository { public function myCustomFunction($user_role) { $qb = $this->createQueryBuilder('a'); if ($user_role == 'ROLE_REDAC') { $where_dql_expr = $qb->expr()->andX( $qb->expr()->eq('ap.order', '?1'), $qb->expr()->eq('a.author', '?2') ); $qb->setParameters(//....); } else if ($user_role == 'ROLE_ADMIN') { $where_dql_expr = $qb->expr()->andX( $qb->expr()->eq('ap.order', '?1') ); $qb->setParameters(//....); } $qb->LeftJoin('a.ArticlePhrases','ap') ->addSelect('ap') ->where($where_dql_expr) ->//and so on } } 

of course into your controller you have to call it passing the right parameter

$role = //retrieve role $custom_repo = $this->getDoctrine() ->getManager() ->getRepository('YourBundleName:MyCustomEntity'); $custom_repo->myCustomFunction($role); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.