You can use another approach for this without writing dql queries.
It is described here:
http://doctrine-orm.readthedocs.io/en/latest/reference/working-with-associations.html#filtering-collections
Just add new method getFilteredUser($filter) to the Company entity and add
the condition

public function getFilteredUser($name)
{
        $criteria = Criteria::create()
            ->andWhere(Criteria::expr()->like('name', $name))

        return $this->getUsers()->matching($criteria);
}

More detailed tutorial about this can be found here
https://knpuniversity.com/screencast/collections/criteria-collection-filtering


Regards,
Plamen Vasilev 2

On Wed, Oct 11, 2017 at 11:35 PM, Richtermeister <[email protected]> wrote:

> Yes, this is actually quite simple. You'll either want to use the query
> builder (http://docs.doctrine-project.org/projects/doctrine-orm/en/
> latest/reference/query-builder.html) or write a DQL query.
> Either way, just like in SQL, you'll want to join Companies to Users..
>
> PseudoCode:
>
> // inside CompanyRepository
> $companies = $this->createQueryBuilder('c')
> ->join('c.users', 'u')
> ->where('u.firstName = :name')
> ->setParameter('name', 'Joe')
> ->getQuery()->getResult();
>
> Both query builder and DQL are very powerful and allow you to query just
> about anything,
> and it is common to put these queries into repository classes with a
> simple API.. `findCompaniesWithUserByName($name)` for example.
>
> Hope this helps.
>
> On Wednesday, October 11, 2017 at 6:02:36 AM UTC-7, [email protected]
> wrote:
>>
>>
>> Hi,
>>
>> I'm new to Doctrine and could not figure out how to solve this issue in a
>> good/practical way – I'm probably missing something.
>>
>> 1. Let's suppose I have a entity called "Company" and another called
>> "User".
>> 2. The "User" entity has a many to one relation with "Company", so each
>> user will be associated with a company
>> 3. In reverse, each company can have any number of users in a one to many
>> relation.
>>
>> Is there a way to query only Companies with Users? Or even, Companies
>> with Users named "Joe" or any other condition. Filtering collections
>> doesn't seem to solve any of those questions because if a Company has no
>> Users matching the criteria, it should not appear on the results.
>>
>> I can answer both questions with raw SQL queries but I don't know if it
>> is the correct path, if I should map with DQL or something else.
>>
>> Thanks a lot!
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "doctrine-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/doctrine-user.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to