@NubasLol

Symfony как правильно создать форму для M:M?

Поле которое нужно заполнить

/**
     * @ORM\ManyToMany(targetEntity="Department", inversedBy="employees")
     */
    private $departments;


Делаю так:

$em = $this->getDoctrine()->getManager();

        $departmentArray = $em->getRepository('AppBundle:Department')->findAll();

        $employee = new Employee();
        $form = $this->createForm(new EmployeeType(), $employee, [
            'departmentArray' => $departmentArray,
        ]);


public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $departmentArray  = $options['departmentArray'];

            ->add('departments', ChoiceType::class,
                array(
                    'choices' => $departmentArray,
                    'choices_as_values' => true,
                    'expanded' => true,
                    'multiple' => true,
                    'label' => false,
                    'required' => true,
                    'choice_label' => function($department, $key, $value) {
                        return strtoupper($department->getName());
                    },
                ))


Результат:

Expected argument of type "string", "AppBundle\Form\EmployeeType" given

Требует строку,хотя по факту нужен объект. Как бороться?
  • Вопрос задан
  • 48 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы