@@ -59,47 +59,56 @@ The ``entity`` type has just one required option: the entity which should
5959be listed inside the choice field::
6060
6161 $builder->add('users', 'entity', array(
62- 'class' => 'AcmeHelloBundle:User',
62+ // query choices from this entity
63+ 'class' => 'AppBundle:User',
64+
65+ // use the User.username property as the visible option string
6366 'choice_label' => 'username',
67+
68+ // used to render a select box, check boxes or radios
69+ // 'multiple' => true,
70+ // 'expanded' => true,
6471 ));
6572
66- In this case, all ``User `` objects will be loaded from the database and
67- rendered as either a `` select `` tag, a set or radio buttons or a series
68- of checkboxes (this depends on the `` multiple `` and `` expanded `` values) .
69- If the entity object does not have a `` __toString() `` method the `` choice_label ``
70- option is needed.
73+ This will build a ``select `` drop-down containing * all * of the `` User `` objects
74+ in the database. To render radio buttons or checkboxes instead, change the
75+ ` multiple `_ and `expanded `_ options .
76+
77+ .. _ ref-form-entity-query-builder :
7178
7279Using a Custom Query for the Entities
7380~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7481
75- If you need to specify a custom query to use when fetching the entities
82+ If you want to create a custom query to use when fetching the entities
7683(e.g. you only want to return some entities, or need to order them), use
77- the `` query_builder `` option. The easiest way to use the option is as follows ::
84+ the `query_builder `_ option::
7885
7986 use Doctrine\ORM\EntityRepository;
8087 // ...
8188
8289 $builder->add('users', 'entity', array(
83- 'class' => 'AcmeHelloBundle :User',
90+ 'class' => 'AppBundle :User',
8491 'query_builder' => function (EntityRepository $er) {
8592 return $er->createQueryBuilder('u')
8693 ->orderBy('u.username', 'ASC');
8794 },
95+ 'choice_label' => 'username',
8896 ));
8997
9098.. _reference-forms-entity-choices :
9199
92100Using Choices
93101~~~~~~~~~~~~~
94102
95- If you already have the exact collection of entities that you want included
96- in the choice element, you can simply pass them via the ``choices `` key.
103+ If you already have the exact collection of entities that you want to include
104+ in the choice element, just pass them via the ``choices `` key.
105+
97106For example, if you have a ``$group `` variable (passed into your form perhaps
98107as a form option) and ``getUsers `` returns a collection of ``User `` entities,
99108then you can supply the ``choices `` option directly::
100109
101110 $builder->add('users', 'entity', array(
102- 'class' => 'AcmeHelloBundle :User',
111+ 'class' => 'AppBundle :User',
103112 'choices' => $group->getUsers(),
104113 ));
105114
@@ -157,8 +166,8 @@ class
157166
158167**type **: ``string `` **required **
159168
160- The class of your entity (e.g. ``AcmeStoreBundle :Category ``). This can be
161- a fully-qualified class name (e.g. ``Acme\StoreBundle \Entity\Category ``)
169+ The class of your entity (e.g. ``AppBundle :Category ``). This can be
170+ a fully-qualified class name (e.g. ``AppBundle \Entity\Category ``)
162171or the short alias name (as shown prior).
163172
164173em
@@ -174,11 +183,12 @@ query_builder
174183
175184**type **: ``Doctrine\ORM\QueryBuilder `` or a Closure
176185
177- If specified, this is used to query the subset of options (and their
178- order) that should be used for the field. The value of this option can
179- either be a ``QueryBuilder `` object or a Closure. If using a Closure,
180- it should take a single argument, which is the ``EntityRepository `` of
181- the entity and return an instance of ``QueryBuilder ``.
186+ Allows you to create a custom query for your choices. See
187+ :ref: `ref-form-entity-query-builder ` for an example.
188+
189+ The value of this option can either be a ``QueryBuilder `` object or a Closure.
190+ When using a Closure, you will be passed the ``EntityRepository `` of the entity
191+ as the only argument and should return a ``QueryBuilder ``.
182192
183193Overridden Options
184194------------------
0 commit comments