@@ -54,47 +54,56 @@ The ``entity`` type has just one required option: the entity which should
54
54
be listed inside the choice field::
55
55
56
56
$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
58
61
'property' => 'username',
62
+
63
+ // used to render a select box, check boxes or radios
64
+ // 'multiple' => true,
65
+ // 'expanded' => true,
59
66
));
60
67
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 :
66
73
67
74
Using a Custom Query for the Entities
68
75
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69
76
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
71
78
(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::
73
80
74
81
use Doctrine\ORM\EntityRepository;
75
82
// ...
76
83
77
84
$builder->add('users', 'entity', array(
78
- 'class' => 'AcmeHelloBundle :User',
85
+ 'class' => 'AppBundle :User',
79
86
'query_builder' => function (EntityRepository $er) {
80
87
return $er->createQueryBuilder('u')
81
88
->orderBy('u.username', 'ASC');
82
89
},
90
+ 'property' => 'username',
83
91
));
84
92
85
93
.. _reference-forms-entity-choices :
86
94
87
95
Using Choices
88
96
~~~~~~~~~~~~~
89
97
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
+
92
101
For example, if you have a ``$group `` variable (passed into your form perhaps
93
102
as a form option) and ``getUsers `` returns a collection of ``User `` entities,
94
103
then you can supply the ``choices `` option directly::
95
104
96
105
$builder->add('users', 'entity', array(
97
- 'class' => 'AcmeHelloBundle :User',
106
+ 'class' => 'AppBundle :User',
98
107
'choices' => $group->getUsers(),
99
108
));
100
109
@@ -108,8 +117,8 @@ class
108
117
109
118
**type **: ``string `` **required **
110
119
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 ``)
113
122
or the short alias name (as shown prior).
114
123
115
124
em
@@ -159,11 +168,12 @@ query_builder
159
168
160
169
**type **: ``Doctrine\ORM\QueryBuilder `` or a Closure
161
170
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 ``.
167
177
168
178
Overridden Options
169
179
------------------
0 commit comments