SETUP:
public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('relatedEntity', 'entity', array( 'class' => 'ProjectName\RelatedEntityBundle\Entity\RelatedEntity', 'query_builder' => function (EntityRepository $er) { $queryBuilder = $er->createQueryBuilder('relatedEntity'); $queryBuilder->resetDQLPart('select'); $queryBuilder->resetDQLPart('from'); $queryBuilder->select('relatedEntity') ->distinct(true) ->from('ProjectNameRelatedEntityBundle:RelatedEntity', 'relatedEntity'); return $queryBuilder; }, .... .... }
Template:
(relateEntity has a __toString()
function defined to show its name).
{{ form_label(form.relatedEntity) }} {{ form_widget(form.relatedEntity) }} {{ form_errors(form.relatedEntity) }}
QUESTIONS:
entity
type to get only some fields of the related
entity and show them in the template?
TESTS:
I cant seem to build the form with the choice type by passing just an array to show a selectBox with the id and name of my related entity in the template. I always get the same error, asking me to insert an array of entity objects in that choiceS option.
Lets look at some examples at the formType, buildForm function of the main entity:
->add('relatedEntity', null, array('label'=> 'relatedEntity'))
->add('relatedEntity', 'entity', array( 'class' => 'ProjectName\RelatedEntityBundle\Entity\RelatedEntity', 'query_builder' => function (EntityRepository $er) { $queryBuilder = $er->createQueryBuilder('relatedEntity'); $queryBuilder->resetDQLPart('select'); $queryBuilder->resetDQLPart('from'); $queryBuilder->select('relatedEntity') ->from('ProjectNameRelatedEntityBundle:RelatedEntity', 'relatedEntity'); return $queryBuilder; }, 'property' => 'descripcion' ))
$arrayValues = array('1'=>'name1', '2'=>'name2', '3'=>'name3');
->add('relatedEntity', 'choice', array( 'choices' => $arrayValues, 'multiple' => false, 'label'=> 'relatedEntity' ))
$arrayValues = array('1'=>'name1', '2'=>'name2', '3'=>'name3');
->add('relatedEntity', 'entity', array( 'class' => 'ProjectName\RelatedEntityBundle\Entity\RelatedEntity', 'choices' => $arrayValues , 'multiple' => false, 'label'=> 'relatedEntity' ))
I have also tested trying to hack the choices input requeriment by building an array of objets of my related entity, but it asks me to persists those entities before being sent to the choice type.
The problem is your form element which requires its content to be an entity, which is an instance of class ProjectName\RelatedEntityBundle\Entity\RelatedEntity
, but you pass an array as choices:
$arrayValues = array(
'1'=>'name1',
'2'=>'name2',
'3'=>'name3'
);
On the other hand, when you use a choice-element and add the array, your form element will return a string, whereas your entity requires relatedEntity
to be an instance of the above mentioned class.
Either way, you have to ensure the data you add or retrieve from the element matches your requirements.
What you can do, is make it a choice
-element and remove the class-restriction (as you have tried). Then, to ensure it will return an entity-instance rather than a string you can use Form Events. You could use FormEvents::SUBMIT
or FormEvents::PRE_SUBMIT
to check which entity name was selected and perform a query to fetch the corresponding entity, e.g. something like:
$objectRepository->findEntityBy(array('name' => $name));