Q
Q
qlr2017-03-23 02:51:57
symfony
qlr, 2017-03-23 02:51:57

Write to DB values ​​from EntityType in FormBuilder?

It's probably easier to show with code:
There is an entity User, it has a ManagerId, that is, an integer.

/** @ORM\Column(name="manager_id", type="integer", nullable=true) */
    protected $managerId;

There is a form where this user is created through FormBuilder
->add('managerId', EntityType::class, array(
            'label' => 'Менеджер',
            'class' => 'WPUserBundle:User',
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('u')
                    ->setParameter('role', '%MANAGER%')
                    ->where('u.roles LIKE :role');
            },
            'choice_label' => 'id',
            'choice_value' => 'id',
            'choice_name' => 'id',
            'empty_data' => 1
            ))

Actually it is necessary that when a certain user is selected in the form, his Id is added to the ManagerId of the new user.
The last values, as you could understand, are what I'm trying, but it doesn't work. An exception is thrown:
Notice: Object of class WP\UserBundle\Entity\User could not be converted to double

as I understand it, because in the lists of enumerated values, where id should be, for some reason there is {}, that is, array.
At the same time, everything is shown normally in the form, I put choice_label = id, id is shown.
Maybe I don’t know some option yet or I couldn’t smoke the dock normally ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Q
qlr, 2017-03-23
@qlr

Thanks to BoShurik for the tip.
Using the DataTransformer, it was not possible to solve the problem, for some reason it still did not transform the data. Therefore, I went on a crutch, made a SUBMIT event on the form and this code:

->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
                $user = $event->getData();
                $form = $event->getForm();
                if (!$user) {
                    return;
                }
                $manager = $user->getManagerId(); 
                if (!$manager) {
                    return;
                }
                $user->setManagerId($manager->getId());
            })

B
BoShurik, 2017-03-23
@BoShurik

Is it by design that $managerId stores the ID and not the object?
If so, then you need a DataTransformer .
If there should be an object, then you have the wrong mapping: docs.doctrine-project.org/projects/doctrine-orm/en...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question