Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7794157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:42:14+00:00 2026-06-01T22:42:14+00:00

I have my two classes User and Role , and I need to make

  • 0

I have my two classes User and Role, and I need to make a QueryBuilder which returns a query for the users who have the ROLE_PROVIDER role. I need this for an entity form field in Symfony 2. In the Form Class definition I have the following snippet for the mentioned field:

$builder->add('provider', 'entity', array(
    'class'    => 'ElCuadreAccountBundle:User',
    'property' => 'username',
    'query_builder' => function(UserRepository $ur) {
                         return $ur->getUsersByRoleQB('ROLE_PROVIDER');
                       },
    'required' => true,
));

And then in my Custom UserRepository I have the following function, which should return a QueryBuilder object:

public function getUsersByRoleQB($role) {
    $qb = $this->createQueryBuilder('u');
    return $qb->join('u.roles','r')
              ->where($qb->expr()->in('r.role',$qb->expr()->literal($role)))
              ->orderBy('u.username', 'ASC');
}

Of course this doesn’t work, but i pasted it to illustrate my needs.

I’ve been looking around and it seems Doctrine2 does not support natively filtering by an association. In this page they say so, and suggest using DQL for this kind of filtering. My problem is that I have not found how to make a QueryBuilder object from a DQL sentence. If you could also provide me with the right DQL query, I would be very grateful…

Thanks for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:42:15+00:00Added an answer on June 1, 2026 at 10:42 pm

    where should actually do what you want. You just have the syntax wrong for ‘in’:

    This

    ->where($qb->expr()->in('r.role',$qb->expr()->literal($role)))
    

    Should be

    ->where($qb->expr()->in('r.role',$role))
    

    I know it may seem a bit strange but since prepared statements do not directly support arrays, parameters to IN clauses always have to be escaped individually (which doctrine does for you). Hence the syntax is a bit different then say for an eq expression where literal is required.

    You do raise a good question because I have needed to filter by association. I think D2.2 will allow this out of the box. I have not really tried it but I suspect that

    $dql = 'a,b FROM whatever...'; // Don't start with SELECT
    $qb->select($dql); 
    return $qb;
    

    will actually work without specifying any other parts as long as your leave the actual ‘SELECT’ string out of the $dql. Untested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, user and role, defined as: public class User : Entity
I have two classes which second inherit by first. then I have this: First[]
I have two classes: User : /** @Entity @Table(name=users) */ class User { /**
I got two classes User and Role which are mapped to each other using
I'm using LINQ to SQL and C#. I have two LINQ classes: User and
I have two classes namely classA and classB which both inherit from class1 .
I have two classes, Foo and Bar, that have constructors like this: class Foo
I have two classes that each need an instance of each other to function.
I have two tables: Users and Roles. A user may have more roles, so
I have two classes : User, Patent and Help (belongs_to :user and belongs_to :patent)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.