Get field based on another in sonata

Viewed 17

I have a One-to-many relation between an User and a Car and I'm trying in the admin to display those cars associated to the selected User.

Here is my code:

protected function configureFormFields(FormMapper $form): void
{
    $form
        ->add('user', ModelType::class, [
            'query' => $this->getModelManager()
                ->getEntityManager(User::class)
                ->createQueryBuilder('u')
                ->select('u')
                ->from('App\Entity\User', 'u')
                ->where('u.roles LIKE :role')
                ->setParameter('role', '%' . 'ROLE_USER' . '%')
        ])
        ->add('car', ModelType::class, [
            'query' => $this->getModelManager()
                ->getEntityManager(Car::class)
                ->createQueryBuilder('u')
                ->select('u')
                ->from('App\Entity\Car', 'u')
                ->where('u.user = :user')
                ->setParameter('user', $form->get('user')),
        ]);
}

Of course the $form->get('user') doesn't work here so I was wondering if there are any ways to achieve this.

0 Answers
Related