Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Forms #8

Merged
merged 2 commits into from

2 participants

@stof
Collaborator

This updates the form integration to use the new bridge classes.

There is a BC break here: the option to change the manager is now em instead of dm as you did not make it configurable in your refactoring of the bridge.

@beberlei beberlei merged commit 61bae22 into from
@beberlei
Owner

exactly the right amount of additions and deletions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
40 Form/ChoiceList/CouchDBDocumentChoiceList.php
@@ -1,40 +0,0 @@
-<?php
-
-/*
- * Doctrine CouchDB Bundle
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to kontakt@beberlei.de so I can send you a copy immediately.
- */
-
-namespace Doctrine\Bundle\CouchDBBundle\Form\ChoiceList;
-
-use Symfony\Component\Form\Exception\FormException;
-
-class CouchDBDocumentChoiceList extends PersistentObjectChoiceList
-{
- protected function doGetPersistentObject($key)
- {
- // CouchDB has no composite keys.
- if ($this->persistentObjects) {
- return isset($this->persistentObjects[$key]) ? $this->persistentObjects[$key] : null;
- }
-
- return $this->objectManager->find($this->class, $key);
- }
-
- public function getIdentifierValues($object)
- {
- if (!$this->objectManager->getUnitOfWork()->isInIdentityMap($object)) {
- throw new FormException('Documents passed to the choice field must be managed in the Document Manager.');
- }
-
- // has to be array of
- return array($this->objectManager->getUnitOfWork()->getDocumentIdentifier($object));
- }
-}
View
243 Form/ChoiceList/PersistentObjectChoiceList.php
@@ -1,243 +0,0 @@
-<?php
-
-/*
- * Doctrine CouchDB Bundle
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to kontakt@beberlei.de so I can send you a copy immediately.
- */
-
-namespace Doctrine\Bundle\CouchDBBundle\Form\ChoiceList;
-
-use Symfony\Component\Form\Util\PropertyPath;
-use Symfony\Component\Form\Exception\FormException;
-use Symfony\Component\Form\Exception\UnexpectedTypeException;
-use Symfony\Component\Form\Extension\Core\ChoiceList\ArrayChoiceList;
-use Doctrine\Common\Persistence\ObjectManager;
-
-/**
- * Abstract choice list to be used with PersistentObject storages.
- *
- * @author Benjamin Eberlei <kontakt@beberlei.de>
- */
-abstract class PersistentObjectChoiceList extends ArrayChoiceList
-{
- /**
- * @var \Doctrine\Common\Persistence\ObjectManager
- */
- protected $objectManager;
-
- /**
- * Class name of the persistent object
- *
- * @var string
- */
- protected $class;
-
- /**
- * The persistent objects from which the user can choose
- *
- * This array is either indexed by ID (if the ID is a single field)
- * or by key in the choices array (if the ID consists of multiple fields)
- *
- * This property is initialized by initializeChoices(). It should only
- * be accessed through getPersistentObject() and getPersistentObjects().
- *
- * @var Collection
- */
- protected $persistentObjects = array();
-
-
- /**
- * The fields of which the identifier of the underlying class consists
- *
- * This property should only be accessed through identifier.
- *
- * @var array
- */
- protected $identifier = array();
-
- /**
- * @var PropertyPath
- */
- private $propertyPath;
-
- /**
- * @param ObjectManager $objectManager
- * @param string $class
- * @param string|null $property
- * @param array $choices
- */
- public function __construct(ObjectManager $objectManager, $class, $property = null, $choices = array())
- {
- $this->objectManager = $objectManager;
- $this->class = $class;
- $this->identifier = $objectManager->getClassMetadata($class)->getIdentifier();
-
- // The property option defines, which property (path) is used for
- // displaying entities as strings
- if ($property) {
- $this->propertyPath = new PropertyPath($property);
- }
-
- parent::__construct($choices);
- }
-
- /**
- * Initializes the choices and returns them
- *
- * If the entities were passed in the "choices" option, this method
- * does not have any significant overhead. Otherwise, if a query builder
- * was passed in the "query_builder" option, this builder is now used
- * to construct a query which is executed. In the last case, all entities
- * for the underlying class are fetched from the repository.
- *
- * @return array An array of choices
- */
- protected function load()
- {
- parent::load();
-
- $objects = $this->getChoiceObjects();
-
- $this->choices = array();
- $this->persistentObjects = array();
-
- $this->loadPersistentObjects($objects);
-
- return $this->choices;
- }
-
- protected function getChoiceObjects()
- {
- if ($this->choices) {
- $objects = $this->choices;
- } else {
- $objects = $this->objectManager->getRepository($this->class)->findAll();
- }
- return $objects;
- }
-
- /**
- * Convert persistent objects into choices with support for groups
- *
- * The choices are generated from the objects. If the objects have a
- * composite identifier, the choices are indexed using ascending integers.
- * Otherwise the identifiers are used as indices.
- *
- * If the option "property" was passed, the property path in that option
- * is used as option values. Otherwise this method tries to convert
- * objects to strings using __toString().
- *
- */
- private function loadPersistentObjects($objects, $group = null)
- {
- $isComposite = (count($this->identifier) > 1);
- foreach ($objects as $key => $object) {
- if (is_array($object)) {
- // Entities are in named groups
- $this->loadPersistentObjects($object, $key);
- continue;
- }
-
- if ($this->propertyPath) {
- // If the property option was given, use it
- $value = $this->propertyPath->getValue($object);
- } else {
- // Otherwise expect a __toString() method in the entity
- if (!method_exists($object, '__toString')) {
- throw new FormException('Persistent objects (Entities, Documents) passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option).');
- }
-
- $value = (string)$object;
- }
-
- if ($isComposite) {
- // When the identifier consists of multiple field, use
- // naturally ordered keys to refer to the choices
- $id = $key;
- } else {
- // When the identifier is a single field, index choices by
- // entity ID for performance reasons
- $id = current($this->getIdentifierValues($object));
- }
-
- if (null === $group) {
- // Flat list of choices
- $this->choices[$id] = $value;
- } else {
- // Nested choices
- $this->choices[$group][$id] = $value;
- }
-
- $this->persistentObjects[$id] = $object;
- }
- }
-
- public function getIdentifier()
- {
- return $this->identifier;
- }
-
- /**
- * Returns the according entities for the choices
- *
- * If the choices were not initialized, they are initialized now. This
- * is an expensive operation, except if the entities were passed in the
- * "choices" option.
- *
- * @return array An array of entities
- */
- public function getPersistentObjects()
- {
- if (!$this->loaded) {
- $this->load();
- }
-
- return $this->persistentObjects;
- }
-
- /**
- * Returns the entity for the given key
- *
- * If the underlying entities have composite identifiers, the choices
- * are initialized. The key is expected to be the index in the choices
- * array in this case.
- *
- * If they have single identifiers, they are either fetched from the
- * internal entity cache (if filled) or loaded from the database.
- *
- * @param string $key The choice key (for entities with composite
- * identifiers) or entity ID (for entities with single
- * identifiers)
- * @return object The matching entity
- */
- public function getPersistentObject($key)
- {
- if (!$this->loaded) {
- $this->load();
- }
-
- return $this->doGetPersistentObject($key);
- }
-
- abstract protected function doGetPersistentObject($key);
-
- /**
- * Returns the values of the identifier fields of an object
- *
- * Doctrine must know about this entity, that is, the object must already
- * be persisted or added to the identity map before. Otherwise an
- * exception is thrown.
- *
- * @param object $object The object for which to get the identifier
- * @throws FormException If the object does not exist in Doctrine's
- * identity map
- */
- abstract public function getIdentifierValues($object);
-}
View
3  Form/CouchDBTypeGuesser.php
@@ -20,12 +20,11 @@
namespace Doctrine\Bundle\CouchDBBundle\Form;
+use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Form\FormTypeGuesserInterface;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Component\Form\Guess\ValueGuess;
-use Doctrine\ORM\Mapping\MappingException;
-use Symfony\Bridge\Doctrine\ManagerRegistry;
/**
* Guesser for Form component using Doctrine CouchDB registry and metadata.
View
104 Form/DataTransformer/PersistentObjectToArrayTransformer.php
@@ -1,104 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Doctrine\Bundle\CouchDBBundle\Form\DataTransformer;
-
-use Doctrine\Bundle\CouchDBBundle\Form\ChoiceList\PersistentObjectChoiceList;
-use Symfony\Component\Form\Exception\UnexpectedTypeException;
-use Symfony\Component\Form\Exception\TransformationFailedException;
-use Symfony\Component\Form\DataTransformerInterface;
-use Doctrine\Common\Collections\Collection;
-use Doctrine\Common\Collections\ArrayCollection;
-
-class PersistentObjectToArrayTransformer implements DataTransformerInterface
-{
- private $choiceList;
-
- public function __construct(PersistentObjectChoiceList $choiceList)
- {
- $this->choiceList = $choiceList;
- }
-
- /**
- * Transforms persistent objects into choice keys
- *
- * @param Collection|object $collection A collection of persistents objects, a single PO or NULL
- * @return mixed An array of choice keys, a single key or NULL
- */
- public function transform($collection)
- {
- if (null === $collection) {
- return array();
- }
-
- if (!($collection instanceof Collection)) {
- throw new UnexpectedTypeException($collection, 'Doctrine\Common\Collections\Collection');
- }
-
- $array = array();
-
- if (count($this->choiceList->getIdentifier()) > 1) {
- // load all choices
- $availableEntities = $this->choiceList->getPersistentObjects();
-
- foreach ($collection as $entity) {
- // identify choices by their collection key
- $key = array_search($entity, $availableEntities);
- $array[] = $key;
- }
- } else {
- foreach ($collection as $entity) {
- $array[] = current($this->choiceList->getIdentifierValues($entity));
- }
- }
-
- return $array;
- }
-
- /**
- * Transforms choice keys into persistent objects
- *
- * @param mixed $keys An array of keys, a single key or NULL
- * @return Collection|object A collection of persistent objects, a single PO or NULL
- */
- public function reverseTransform($keys)
- {
- $collection = new ArrayCollection();
-
- if ('' === $keys || null === $keys) {
- return $collection;
- }
-
- if (!is_array($keys)) {
- throw new UnexpectedTypeException($keys, 'array');
- }
-
- $notFound = array();
-
- // optimize this into a SELECT WHERE IN query
- foreach ($keys as $key) {
- if ($object = $this->choiceList->getPersistentObject($key)) {
- $collection->add($object);
- } else {
- $notFound[] = $key;
- }
- }
-
- if (count($notFound) > 0) {
- throw new TransformationFailedException(
- sprintf('The persistent objects (entities or documents) with keys "%s" could not be found',
- implode('", "', $notFound))
- );
- }
-
- return $collection;
- }
-}
View
78 Form/DataTransformer/PersistentObjectToIdTransformer.php
@@ -1,78 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\Bridge\Doctrine\Form\DataTransformer;
-
-use Doctrine\Bundle\CouchDBBundle\Form\ChoiceList\PersistentObjectChoiceList;
-use Symfony\Component\Form\DataTransformerInterface;
-use Symfony\Component\Form\Exception\UnexpectedTypeException;
-use Symfony\Component\Form\Exception\TransformationFailedException;
-
-class PersistentObjectToIdTransformer implements DataTransformerInterface
-{
- private $choiceList;
-
- public function __construct(PersistentObjectChoiceList $choiceList)
- {
- $this->choiceList = $choiceList;
- }
-
- /**
- * Transforms entities into choice keys
- *
- * @param Collection|object $object A collection of persistent objects, a single PO or NULL
- * @return mixed An array of choice keys, a single key or NULL
- */
- public function transform($object)
- {
- if (null === $object || '' === $object) {
- return '';
- }
-
- if (!is_object($object)) {
- throw new UnexpectedTypeException($object, 'object');
- }
-
- if (count($this->choiceList->getIdentifier()) > 1) {
- // load all choices
- $availableObjects = $this->choiceList->getPersistentObjects();
-
- return array_search($object, $availableObjects);
- }
-
- return current($this->choiceList->getIdentifierValues($object));
- }
-
- /**
- * Transforms choice keys into peristent objects
- *
- * @param mixed $key An array of keys, a single key or NULL
- * @return Collection|object A collection of persistent objects, a single PO or NULL
- */
- public function reverseTransform($key)
- {
- if ('' === $key || null === $key) {
- return null;
- }
-
- if (count($this->choiceList->getIdentifier()) > 1 && !is_numeric($key)) {
- throw new UnexpectedTypeException($key, 'numeric');
- }
-
- if (!($object = $this->choiceList->getPersistentObject($key))) {
- throw new TransformationFailedException(
- sprintf('The persistent object (document, entity) with key "%s" could not be found', $key
- ));
- }
-
- return $object;
- }
-}
View
5 Form/DoctrineCouchDBExtension.php
@@ -29,4 +29,9 @@ protected function loadTypes()
new Type\DocumentType($this->registry),
);
}
+
+ protected function loadTypeGuesser()
+ {
+ return new CouchDBTypeGuesser($this->registry);
+ }
}
View
62 Form/EventListener/MergeCollectionListener.php
@@ -1,62 +0,0 @@
-<?php
-
-/*
- * Doctrine CouchDB Bundle
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to kontakt@beberlei.de so I can send you a copy immediately.
- */
-
-namespace Doctrine\Bundle\CouchDBBundle\Form\EventListener;
-
-use Symfony\Component\Form\FormEvents;
-use Symfony\Component\Form\Event\FilterDataEvent;
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
-
-/**
- * Merge changes from the request to a Doctrine\Common\Collections\Collection instance.
- *
- * This works with ORM, MongoDB and CouchDB instances of the collection interface.
- *
- * @see Doctrine\Common\Collections\Collection
- * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
- */
-class MergeCollectionListener implements EventSubscriberInterface
-{
- static public function getSubscribedEvents()
- {
- return array(FormEvents::BIND_NORM_DATA => 'onBindNormData');
- }
-
- public function onBindNormData(FilterDataEvent $event)
- {
- $collection = $event->getForm()->getData();
- $data = $event->getData();
-
- if (!$collection) {
- $collection = $data;
- } else if (count($data) === 0) {
- $collection->clear();
- } else {
- // merge $data into $collection
- foreach ($collection as $entity) {
- if (!$data->contains($entity)) {
- $collection->removeElement($entity);
- } else {
- $data->removeElement($entity);
- }
- }
-
- foreach ($data as $entity) {
- $collection->add($entity);
- }
- }
-
- $event->setData($collection);
- }
-}
View
32 Form/Type/DocumentType.php
@@ -14,34 +14,26 @@
namespace Doctrine\Bundle\CouchDBBundle\Form\Type;
-use Symfony\Component\Form\FormBuilder;
-use Symfony\Component\Form\FormFactoryInterface;
-use Symfony\Component\Form\AbstractType;
-use Doctrine\Bundle\CouchDBBundle\Form\ChoiceList\CouchDBDocumentChoiceList;
+use Doctrine\Common\Persistence\ObjectManager;
+use Symfony\Bridge\Doctrine\Form\Type\DoctrineType;
+use Symfony\Component\Form\Exception\FormException;
-class DocumentType extends PersistentObjectType
+class DocumentType extends DoctrineType
{
- protected function getObjectManagerKey()
- {
- return 'dm';
- }
-
/**
- * @return PersistentObjectChoiceList
+ * Return the loader object.
+ *
+ * @param ObjectManager $manager
+ * @param array $options
+ * @return EntityLoaderInterface
*/
- protected function createDefaultChoiceList($options)
+ protected function getLoader(ObjectManager $manager, array $options)
{
- $documentManager = is_object($options['dm']) ?: $this->registry->getManager($options['dm']);
- return new CouchDBDocumentChoiceList(
- $documentManager,
- $options['class'],
- $options['property'],
- $options['choices']
- );
+ throw new FormException('The query builder option is not supported by CouchDB.');
}
public function getName()
{
- return 'doctrine_couchdb_document';
+ return 'couchdb_document';
}
}
View
78 Form/Type/PersistentObjectType.php
@@ -1,78 +0,0 @@
-<?php
-
-/*
- * Doctrine CouchDB Bundle
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to kontakt@beberlei.de so I can send you a copy immediately.
- */
-
-namespace Doctrine\Bundle\CouchDBBundle\Form\Type;
-
-use Symfony\Component\Form\FormBuilder;
-use Symfony\Component\Form\FormFactoryInterface;
-use Symfony\Component\Form\AbstractType;
-use Doctrine\Common\Persistence\ManagerRegistry;
-use Doctrine\Bundle\CouchDBBundle\ChoiceList\EntityChoiceList;
-use Doctrine\Bundle\CouchDBBundle\Form\EventListener\MergeCollectionListener;
-use Doctrine\Bundle\CouchDBBundle\Form\DataTransformer\PersistentObjectToArrayTransformer;
-use Doctrine\Bundle\CouchDBBundle\Form\DataTransformer\PersistentObjectToIdTransformer;
-
-abstract class PersistentObjectType extends AbstractType
-{
- protected $registry;
-
- public function __construct(ManagerRegistry $registry)
- {
- $this->registry = $registry;
- }
-
- public function buildForm(FormBuilder $builder, array $options)
- {
- if ($options['multiple']) {
- $builder
- ->addEventSubscriber(new MergeCollectionListener())
- ->prependClientTransformer(new PersistentObjectToArrayTransformer($options['choice_list']))
- ;
- } else {
- $builder->prependClientTransformer(new PersistentObjectToIdTransformer($options['choice_list']));
- }
- }
-
- abstract protected function getObjectManagerKey();
-
- /**
- * @return PersistentObjectChoiceList
- */
- abstract protected function createDefaultChoiceList($options);
-
- public function getDefaultOptions(array $options)
- {
- $objectManagerKey = $this->getObjectManagerKey();
- $defaultOptions = array(
- $objectManagerKey => null,
- 'class' => null,
- 'property' => null,
- 'query_builder' => null,
- 'choices' => array(),
- );
-
- $options = array_replace($defaultOptions, $options);
-
- if (!isset($options['choice_list'])) {
- $defaultOptions['choice_list'] = $this->createDefaultChoiceList($options);
- }
-
- return $defaultOptions;
- }
-
- public function getParent(array $options)
- {
- return 'choice';
- }
-}
Something went wrong with that request. Please try again.