Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3925

EntityType and many to many with extra field relation presented as dropdown (select)

$
0
0

I created a form like that

$builder->add('employees', EntityType::class, ['class'         => ActivityEmployee::class,'choice_label'  => function (ActivityEmployee $employee) {        return sprintf('%s %s', $employee->getEmployee()->getName(), $employee->getEmployee()->getLastName());    },'multiple'      => true,])

As a result it presents already existing data fine. It shows me all employees with relation to edited activity.

However as choices there should be all employess to choose (employee entity) and as selected data only employess in activityEmployee relation like right now.

I tried to add a query_builder option to provide lists of all employess, but I can only use EntityRepository which means ActivityEmployeesRepository not EmployeesRepository per se.

A can't figure out how to implement it. Basically such relation can be done by CollectionType of custom activityEmployeeType but I'd like to use multi-select for selecting employees.

I can use another approach to not mapping my employees field to entity like that

$currentEmployees = [];foreach ($activity->getEmployees() as $activityEmployee) {    $currentEmployees[] = $activityEmployee->getEmployee();}$builder->add('employees', EntityType::class, ['class'        => Employee::class,'choice_label' => function (Employee $employee) {        return sprintf('%s %s', $employee->getName(), $employee->getLastName());    },'mapped'       => false,'multiple'     => true,'data'         => $currentEmployees,]);

It works fine, but I need to deal with updating relation by myself. Which is ok, however I wonder how to achieve such thing in first approach.


Viewing all articles
Browse latest Browse all 3925

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>