@@ -54,47 +54,56 @@ The ``entity`` type has just one required option: the entity which should
5454be listed inside the choice field::
5555
5656 $builder->add('users', 'entity', array(
57- 'class' => 'AcmeHelloBundle:User',
57+ // query choices from this entity
58+ 'class' => 'AppBundle:User',
59+
60+ // use the User.username property as the visible option string
5861 'property' => 'username',
62+
63+ // used to render a select box, check boxes or radios
64+ // 'multiple' => true,
65+ // 'expanded' => true,
5966 ));
6067
61- In this case, all ``User `` objects will be loaded from the database and
62- rendered as either a `` select `` tag, a set or radio buttons or a series
63- of checkboxes (this depends on the `` multiple `` and `` expanded `` values) .
64- If the entity object does not have a `` __toString() `` method the `` property ``
65- option is needed.
68+ This will build a ``select `` drop-down containing * all * of the `` User `` objects
69+ in the database. To render radio buttons or checkboxes instead, change the
70+ ` multiple `_ and `expanded `_ options .
71+
72+ .. _ ref-form-entity-query-builder :
6673
6774Using a Custom Query for the Entities
6875~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6976
70- If you need to specify a custom query to use when fetching the entities
77+ If you want to create a custom query to use when fetching the entities
7178(e.g. you only want to return some entities, or need to order them), use
72- the `` query_builder `` option. The easiest way to use the option is as follows ::
79+ the `query_builder `_ option::
7380
7481 use Doctrine\ORM\EntityRepository;
7582 // ...
7683
7784 $builder->add('users', 'entity', array(
78- 'class' => 'AcmeHelloBundle :User',
85+ 'class' => 'AppBundle :User',
7986 'query_builder' => function (EntityRepository $er) {
8087 return $er->createQueryBuilder('u')
8188 ->orderBy('u.username', 'ASC');
8289 },
90+ 'property' => 'username',
8391 ));
8492
8593.. _reference-forms-entity-choices :
8694
8795Using Choices
8896~~~~~~~~~~~~~
8997
90- If you already have the exact collection of entities that you want included
91- in the choice element, you can simply pass them via the ``choices `` key.
98+ If you already have the exact collection of entities that you want to include
99+ in the choice element, just pass them via the ``choices `` key.
100+
92101For example, if you have a ``$group `` variable (passed into your form perhaps
93102as a form option) and ``getUsers `` returns a collection of ``User `` entities,
94103then you can supply the ``choices `` option directly::
95104
96105 $builder->add('users', 'entity', array(
97- 'class' => 'AcmeHelloBundle :User',
106+ 'class' => 'AppBundle :User',
98107 'choices' => $group->getUsers(),
99108 ));
100109
@@ -108,8 +117,8 @@ class
108117
109118**type **: ``string `` **required **
110119
111- The class of your entity (e.g. ``AcmeStoreBundle :Category ``). This can be
112- a fully-qualified class name (e.g. ``Acme\StoreBundle \Entity\Category ``)
120+ The class of your entity (e.g. ``AppBundle :Category ``). This can be
121+ a fully-qualified class name (e.g. ``AppBundle \Entity\Category ``)
113122or the short alias name (as shown prior).
114123
115124em
@@ -159,11 +168,12 @@ query_builder
159168
160169**type **: ``Doctrine\ORM\QueryBuilder `` or a Closure
161170
162- If specified, this is used to query the subset of options (and their
163- order) that should be used for the field. The value of this option can
164- either be a ``QueryBuilder `` object or a Closure. If using a Closure,
165- it should take a single argument, which is the ``EntityRepository `` of
166- the entity and return an instance of ``QueryBuilder ``.
171+ Allows you to create a custom query for your choices. See
172+ :ref: `ref-form-entity-query-builder ` for an example.
173+
174+ The value of this option can either be a ``QueryBuilder `` object or a Closure.
175+ When using a Closure, you will be passed the ``EntityRepository `` of the entity
176+ as the only argument and should return a ``QueryBuilder ``.
167177
168178Overridden Options
169179------------------
0 commit comments