EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.04k stars 1.02k forks source link

AssociationField setQueryBuilder doesn't like using repository methods #4169

Closed tpformybh closed 3 years ago

tpformybh commented 3 years ago

Describe the bug I have a crud controller where I'm using an AssociationField and trying to define the query builder:

        yield AssociationField::new('lowLevelWarningUnitOfMeasure')
            ->setQueryBuilder(
                function(QueryBuilder $qb){
                    return $qb->andWhere('entity.isActive = true');
                 });

This works and only returns entities where isActive is true, however, rather than defining it in crud code I'm trying to use a repository method:

        yield AssociationField::new('lowLevelWarningUnitOfMeasure')
            ->setQueryBuilder(
                function(QueryBuilder $qb){
                    return $qb->getEntityManager()->getRepository(UnitOfMeasure::class)->findAllActiveQueryBuilder();
                 });

the repository method is:

    public function findAllActiveQueryBuilder() : QueryBuilder
    {
        return $this->createQueryBuilder('u')
            ->andWhere('u.isActive = :isActive')
            ->setParameter('isActive', true)
        ;
    }

This does not work, returning all active and non-active entities. The repository method is hit, and a dd placed in the repository method shows up.

Why is this happening?

tpformybh commented 3 years ago

I wasn't passing in the query builder to the method to modify it, just returning a new one

Mareva-dev commented 3 years ago

Hello, i would be very interested in four final code if possible.