Permalink
Browse files

Merge pull request #8 from stof/forms

Forms
  • Loading branch information...
2 parents 66ebdb3 + 240547f commit 61bae227b49e7a1410ea2c032206f4834fed1830 @beberlei beberlei committed Dec 22, 2011
@@ -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));
- }
-}
@@ -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);
-}
@@ -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.
Oops, something went wrong.

0 comments on commit 61bae22

Please sign in to comment.