|
| 1 | +Form Integration |
| 2 | +================ |
| 3 | + |
| 4 | +There is a tight integration between Doctrine ORM and the Symfony Form component. Since Doctrine Entities |
| 5 | +are plain old php objects they nicely integrate into the Form component by default, at least for the |
| 6 | +primitive data types such as strings, integers and fields. However you can also integrate them nicely |
| 7 | +with associations. |
| 8 | + |
| 9 | +This is done by the help of ValueTransformers, which are form field extension points. There are currently |
| 10 | +three transformers that allow you to transform Doctrine ORM Collections and Entities into their identifier |
| 11 | +values that can be used with the Form component. Furthermore they translate form values back to the Doctrine |
| 12 | +representation in the most efficient way possible, issuing as few queries as possible. |
| 13 | + |
| 14 | +CollectionToChoiceTransformer |
| 15 | +----------------------------- |
| 16 | + |
| 17 | +This transformer allows you to transform a Collection of Entities into an array of ids. This transformer |
| 18 | +should be used with the ChoiceField or any compatible field that handles arrays of values. |
| 19 | + |
| 20 | + use Symfony\Component\Form\ChoiceField; |
| 21 | + use Symfony\Bundle\DoctrineBundle\Form\ValueTransformer\CollectionToChoiceTransformer; |
| 22 | + |
| 23 | + $field = new ChoiceField('products', array( |
| 24 | + 'choices' => $productChoices, |
| 25 | + 'multiple' => true, |
| 26 | + 'expanded' => true, |
| 27 | + )); |
| 28 | + $field->setValueTransformer(new CollectionToChoiceTransformer(array( |
| 29 | + 'em' => $em, |
| 30 | + 'className' => 'Product', |
| 31 | + ))); |
| 32 | + |
| 33 | + // Important: Make sure to attach the value transformer before calling addField(). |
| 34 | + $form->addField($field); |
| 35 | + |
| 36 | +The 'em' property expects the EntityManager, the 'className' property expects the Entity Class name |
| 37 | +as an argument. |
| 38 | + |
| 39 | +CollectionToStringTransformer |
| 40 | +----------------------------- |
| 41 | + |
| 42 | +This transformer allows you to transform a Collection of Entities into a string separated by a separator. |
| 43 | +This is useful for lists of tags, usernames or similiar unique fields of your Entities. |
| 44 | + |
| 45 | +EntityToIDTransformer |
| 46 | +--------------------- |
| 47 | + |
| 48 | +This transformer converts an Entity into its ID and back to allow to select many-to-one |
| 49 | +or one-to-one entities in choice fields. See this extended example on how it works. In this |
| 50 | +case a list of all users is used in a Choice field to be choosen from: |
| 51 | + |
| 52 | + use Symfony\Bundle\DoctrineBundle\Form\ValueTransformer\EntityToIDTransformer; |
| 53 | + use Symfony\Component\Form\ChoiceField; |
| 54 | + |
| 55 | + $userChoices = array(); |
| 56 | + $users = $em->getRepository('User')->findAll(); |
| 57 | + foreach ($users AS $user) { |
| 58 | + $userChoices[$user->id] = $user->name; |
| 59 | + } |
| 60 | + |
| 61 | + $userTransformer = new EntityToIDTransformer(array( |
| 62 | + 'em' => $em, |
| 63 | + 'className' => 'User', |
| 64 | + )); |
| 65 | + $engineerField = new ChoiceField('engineer', array( |
| 66 | + 'choices' => $userChoices, |
| 67 | + )); |
| 68 | + $engineerField->setValueTransformer($userTransformer); |
| 69 | + $reporterField = new ChoiceField('reporter', array( |
| 70 | + 'choices' => $userChoices, |
| 71 | + )); |
| 72 | + $reporterField->setValueTransformer($userTransformer); |
| 73 | + |
| 74 | + $form->add($engineerField); |
| 75 | + $form->add($reporterfield); |
0 commit comments