K
K
Kirill Gorelov2019-09-27 09:58:40
symfony
Kirill Gorelov, 2019-09-27 09:58:40

Symfony ChoicesType?

Hello.
Guys, how can I load my categories in choicestype?

$builder->add('category_id', ChoiceType::class, [
        'choices' =>$this->getCategory(),
        'by_reference' => false,
        'placeholder' => 'Выбрать категорию',
        'required' => false,
        'label' => 'Категория',
    ]);

public function getCategory(){
 $w = new \Doctrine\ORM\EntityRepository;
        return $w->get('doctrine')->getRepository(\Backend\Modules\Asaf\Domain\Categorys\Category::class)->getAllCategoryActive(true);
        
}

Error:
Type error: Too few arguments to function Doctrine\ORM\EntityRepository::__construct(), 0 passed in /opt/lampp/htdocs/imoscow/src/Backend/Modules/Asaf/Domain/Products/ProductType.php on line 107 and exactly 2
expected

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2019-09-27
@Kirill-Gorelov

Given that the field is called 'category_id', I dare to assume that you have it associated with Many-to-One or Many-to-Many. In this case you need to use not ChoiceType but EntityType .
And an example of how to select only part of the categories:
https://symfony.com/doc/current/reference/forms/ty...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question