From d345d0043258165af11f8843bac0cfc29cbee97a Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Tue, 5 Oct 2010 22:02:18 +0200 Subject: [PATCH 1/2] Add docs on Doctrine ORM Form integration --- guides/doctrine/orm/form.rst | 75 +++++++++++++++++++++++++++++++++++ guides/doctrine/orm/index.rst | 1 + 2 files changed, 76 insertions(+) create mode 100644 guides/doctrine/orm/form.rst diff --git a/guides/doctrine/orm/form.rst b/guides/doctrine/orm/form.rst new file mode 100644 index 00000000000..dea5deb75bd --- /dev/null +++ b/guides/doctrine/orm/form.rst @@ -0,0 +1,75 @@ +Form Integration +================ + +There is a tight integration between Doctrine ORM and the Symfony Form component. Since Doctrine Entities +are plain old php objects they nicely integrate into the Form component by default, at least for the +primitive data types such as strings, integers and fields. However you can also integrate them nicely +with associations. + +This is done by the help of ValueTransformers, which are form field extension points. There are currently +three transformers that allow you to transform Doctrine ORM Collections and Entities into their identifier +values that can be used with the Form component. Furthermore they translate form values back to the Doctrine +representation in the most efficient way possible, issuing as few queries as possible. + +CollectionToChoiceTransformer +----------------------------- + +This transformer allows you to transform a Collection of Entities into an array of ids. This transformer +should be used with the ChoiceField or any compatible field that handles arrays of values. + + use Symfony\Component\Form\ChoiceField; + use Symfony\Bundle\DoctrineBundle\Form\ValueTransformer\CollectionToChoiceTransformer; + + $field = new ChoiceField('products', array( + 'choices' => $productChoices, + 'multiple' => true, + 'expanded' => true, + )); + $field->setValueTransformer(new CollectionToChoiceTransformer(array( + 'em' => $em, + 'className' => 'Product', + ))); + + // Important: Make sure to attach the value transformer before calling addField(). + $form->addField($field); + +The 'em' property expects the EntityManager, the 'className' property expects the Entity Class name +as an argument. + +CollectionToStringTransformer +----------------------------- + +This transformer allows you to transform a Collection of Entities into a string separated by a separator. +This is useful for lists of tags, usernames or similiar unique fields of your Entities. + +EntityToIDTransformer +--------------------- + +This transformer converts an Entity into its ID and back to allow to select many-to-one +or one-to-one entities in choice fields. See this extended example on how it works. In this +case a list of all users is used in a Choice field to be choosen from: + + use Symfony\Bundle\DoctrineBundle\Form\ValueTransformer\EntityToIDTransformer; + use Symfony\Component\Form\ChoiceField; + + $userChoices = array(); + $users = $em->getRepository('User')->findAll(); + foreach ($users AS $user) { + $userChoices[$user->id] = $user->name; + } + + $userTransformer = new EntityToIDTransformer(array( + 'em' => $em, + 'className' => 'User', + )); + $engineerField = new ChoiceField('engineer', array( + 'choices' => $userChoices, + )); + $engineerField->setValueTransformer($userTransformer); + $reporterField = new ChoiceField('reporter', array( + 'choices' => $userChoices, + )); + $reporterField->setValueTransformer($userTransformer); + + $form->add($engineerField); + $form->add($reporterfield); \ No newline at end of file diff --git a/guides/doctrine/orm/index.rst b/guides/doctrine/orm/index.rst index 3cc24717d76..7627761bbef 100644 --- a/guides/doctrine/orm/index.rst +++ b/guides/doctrine/orm/index.rst @@ -7,3 +7,4 @@ Object Relational Mapper Overview Configuration Console Commands + Form
\ No newline at end of file From f40da346a0f7da9ae9236f298f3101ac1efa56d7 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sat, 15 Jan 2011 17:03:14 +0100 Subject: [PATCH 2/2] Added docs on event listeners and subscribers DIC configuration for Doctrine --- guides/doctrine/mongodb-odm/configuration.rst | 8 +++ guides/doctrine/orm/configuration.rst | 62 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/guides/doctrine/mongodb-odm/configuration.rst b/guides/doctrine/mongodb-odm/configuration.rst index 3341b2c24eb..dd706703f5e 100644 --- a/guides/doctrine/mongodb-odm/configuration.rst +++ b/guides/doctrine/mongodb-odm/configuration.rst @@ -294,5 +294,13 @@ Later you can retrieve the persisted document by its id:: } } +Registering Event Listeners and Subscribers +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Registering events works like described in the :ref:`ORM Bundle documentation `. +The MongoDB event tags are called "doctrine.odm.mongodb.default_event_listener" and +"doctrine.odm.mongodb.default_event_subscriber" respectively where "default" is the name of the +MongoDB document manager. + .. _MongoDB: http://www.mongodb.org/ .. _documentation: http://www.doctrine-project.org/projects/mongodb_odm/1.0/docs/en diff --git a/guides/doctrine/orm/configuration.rst b/guides/doctrine/orm/configuration.rst index 8db9b645810..334942dce54 100644 --- a/guides/doctrine/orm/configuration.rst +++ b/guides/doctrine/orm/configuration.rst @@ -147,3 +147,65 @@ retrieve it from the Symfony Dependency Injection Container:: The service "doctrine.orm.entity_manager" is an alias for the default entity manager defined in the "default_entity_manager" configuration option. + +.. _doctrine-event-config: + +Registering Event Listeners and Subscribers +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Doctrine ships with an event system that allows to hook into many different +events happening during the lifecycle of entities or at other occasions. + +To register services to act as event listeners or subscribers (listeners from here) +you have to tag them with the appropriate names. Depending on your use-case you can hook +a listener into every DBAL Connection and ORM Entity Manager or just into one +specific DBAL connection and all the EntityManagers that use this connection. + +.. configuration-block:: + + .. code-block:: yaml + + doctrine.dbal: + default_connection: default + connections: + default: + driver: pdo_sqlite + memory: true + + services: + my.listener: + class: MyEventListener + tags: + - { name: doctrine.common.event_listener } + my.listener2: + class: MyEventListener2 + tags: + - { name: doctrine.dbal.default_event_listener } + my.subscriber: + class: MyEventSubscriber + tags: + - { name: doctrine.dbal.default_event_subscriber } + + .. code-block:: xml + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy