简体   繁体   中英

How can I use the queryBuilder with formBuilder in Symfony 4?

I have difficulties with the use of the query builder in Symfony. Like this, my select box lists all data that is stored in my entity "Options":

$options['class'] = Options::class;
$options['choice_label'] = function ($value) {
    return $value->getName();
};

$formBuilder->add($name, $class, $options);

But I want to filter the data, so that the options show only the data that is connected to a specific id of my entity "Fields".

This is my approach with query builder:

$options['class'] = Options::class;
$options['choice_label'] = 'id';
$options['query_builder'] = function (EntityRepository $id) {
    return $this->createQueryBuilder('f')
        ->leftJoin('f.fields', 'fi')
        ->where(":id MEMBER OF f.fields")
        ->setParameter(':id', $id)
        ->getQuery()
        ->execute();
};

The error message I get is:

Argument 1 passed to App\Controller\PagesController::App\Controller{closure}() must be an instance of App\Controller\EntityRepository, instance of App\Repository\OptionsRepository given, called in /Users/work/project/vendor/symfony/doctrine-bridge/Form/Type/EntityType.php on line 32

Let's start with fact that EntityRepository is core Doctrine repository that is under Doctrine\\ORM\\EntityRepository; namespace. I also doubt that you have another EntityRepository in your project.

So, first error is in incorrect type hint. You should add use Doctrine\\ORM\\EntityRepository; before your controller. OptionsRepository will fit this definition as it extends EntityRepository , or at least, it should .

Second error is ->setParameter(':id', $id) - you cannot set repository as parameter of a query, it's useless. I don't know what is $id but as with any other callback functions, you can use it.

And third, as option is named query_builder - your callback should return QueryBuilder , so remove ->getQuery()->execute(); .

In the end the proper code should be:

use Doctrine\ORM\EntityRepository;

//....

$options['query_builder'] = function (EntityRepository $er) use ($id) {
    return $er->createQueryBuilder('f')
        ->leftJoin('f.fields', 'fi')
        ->where(":id MEMBER OF f.fields")
        ->setParameter(':id', $id);
};

Also check your entity has the repositoryClass properly set if you inject the entire repository.

Entity

/**
 * Category.
 *
 * @ORM\Table(name="category")
 * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
 */
class Category
{
...
}
    

Then

$builder->add('categories', EntityType::class, [
    'class' => Category::class,
    'query_builder' => function (CategoryRepository $categoryRepository) {
        return $categoryRepository->getQueryBuilderForProductCreation();
     },
    'choice_label' => 'name',
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM