8000 minor #6553 EntityType: query_builder link to usage (weaverryan) · symfony/symfony-docs@53accc0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 53accc0

Browse files
committed
minor #6553 EntityType: query_builder link to usage (weaverryan)
This PR was squashed before being merged into the 2.3 branch (closes #6553). Discussion ---------- EntityType: query_builder link to usage | Q | A | ------------- | --- | Doc fix? | yes-ish | New docs? | no | Applies to | 2.3+ | Fixed tickets | n/a I noticed that when you go to the query_builder option, we didn't link back up to the example where it's shown. Then, I made a few other changes, including adding more info in code blocks, and a little less in writing. Commits ------- 31abf2d EntityType: query_builder link to usage
2 parents 449a11f + 31abf2d commit 53accc0

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

reference/forms/types/entity.rst

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,47 +54,56 @@ The ``entity`` type has just one required option: the entity which should
5454
be 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

6774
Using 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

8795
Using 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+
92101
For example, if you have a ``$group`` variable (passed into your form perhaps
93102
as a form option) and ``getUsers`` returns a collection of ``User`` entities,
94103
then 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``)
113122
or the short alias name (as shown prior).
114123

115124
em
@@ -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

168178
Overridden Options
169179
------------------

0 commit comments

Comments
 (0)
0