Reputation: 11
I'm trying to get a specific field in one of my entity classes to appear on my dropdown on my form. The entities do have a one to one relation, though the field I'm trying to retrieve on to my form is not the one related to the other class.
To make things simpler, I'd also like for only the distinct values of that field be shown in the dropdown. I've tried things similar to this: How to select distinct query using symfony2 doctrine query builder? but haven't gotten it to work. I've produced errors such as "Warning: spl_object_hash() expects parameter 1 to be object, string given"
Below is what I've written so far...
In the Form's class...
$builder
->add('typeOfPosition', EntityType::class, array(
'class' => Choices::class,
'label' => 'Type of Position: ',
'query_builder' => function(EntityRepository $er){
return $er->createQueryBuilder('c')
->select('DISTINCT c.type');
},
And the entity that the form is on:
/**
* @ORM\OneToOne(targetEntity="Position", inversedBy="division")
* @ORM\JoinColumn(name="position", referencedColumnName="id")
*/
private $position;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=255)
*/
private $type;
And it's relation entity:
/**
* @var string
*
* @ORM\OneToOne(targetEntity="Choices", mappedBy="position")
*/
private $division;
Upvotes: 0
Views: 162
Reputation: 8162
Let's get things one step at a time. To get your entity dropdown you should use the 'property' option to specify which option is needed
$builder->add('typeOfPosition', EntityType::class, array(
'class' => Position::class,
'property' => 'type',
));
I understand you want to have distinct labels, but if you do a distinct, how do you know the id selected will be the good one?
1 => TYPE_1
2 => TYPE_1
3 => TYPE_3
Why do you want to do a distinct? what will be the id selected in your dropdown? Anyway this is how to do a distinct: How to select distinct query using symfony2 doctrine query builder?
Upvotes: 1