Permalink
Browse files

Merge pull request #41 from elHornair/reference_collection_type

Field type for references collection
  • Loading branch information...
2 parents 41f4939 + 35be23f commit 98089790d743bcad9774383c45d896073c026fa7 @dbu dbu committed Mar 1, 2013
@@ -0,0 +1,54 @@
+<?php
+
+namespace Doctrine\Bundle\PHPCRBundle\Form\DataTransformer;
+
+use Symfony\Component\Form\DataTransformerInterface;
+use Doctrine\ODM\PHPCR\ReferenceManyCollection;
+use Doctrine\ODM\PHPCR\DocumentManager;
+
+class ReferenceManyCollectionToArrayTransformer implements DataTransformerInterface
+{
+
+ /**
+ * @var \Doctrine\ODM\PHPCR\DocumentManager $dm
+ */
+ protected $dm;
+
+ /**
+ * @var string $referencedClass
+ */
+ protected $referencedClass;
+
+
+ /**
+ * @param \Doctrine\ODM\PHPCR\DocumentManager $dm
+ * @param $referencedClass
+ */
+ function __construct(DocumentManager $dm, $referencedClass)
+ {
+ $this->dm = $dm;
+ $this->referencedClass = $referencedClass;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function transform($collection)
+ {
+ $arr = array();
+
+ foreach ($collection as $item) {
+ $arr[] = $item->getPath();
+ }
+
+ return $arr;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function reverseTransform($array)
+ {
+ return new ReferenceManyCollection($this->dm, $array, $this->referencedClass);
+ }
+}
@@ -0,0 +1,57 @@
+<?php
+
+namespace Doctrine\Bundle\PHPCRBundle\Form\Type;
+
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\OptionsResolver\OptionsResolverInterface;
+use Symfony\Component\Form\FormBuilderInterface;
+use Doctrine\ODM\PHPCR\DocumentManager;
+use Doctrine\Bundle\PHPCRBundle\Form\DataTransformer\ReferenceManyCollectionToArrayTransformer;
+
+class PHPCRODMReferenceCollectionType extends AbstractType
+{
+
+ protected $dm;
+
+ /**
+ * @param DocumentManager $dm
+ */
+ function __construct(DocumentManager $dm)
+ {
+ $this->dm = $dm;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getParent()
+ {
+ return 'choice';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getName()
+ {
+ return 'phpcr_odm_reference_collection';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function setDefaultOptions(OptionsResolverInterface $resolver)
+ {
+ parent::setDefaultOptions($resolver);
+ $resolver->setRequired((array('referenced_class')));
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function buildForm(FormBuilderInterface $builder, array $options)
+ {
+ $transformer = new ReferenceManyCollectionToArrayTransformer($this->dm, $options['referenced_class']);
+ $builder->addModelTransformer($transformer);
+ }
+}
View
@@ -64,6 +64,11 @@
<tag name="form.type" alias="phpcr_odm_image"/>
</service>
+ <service id="form.type.phpcr_odm.reference_collection" class="Doctrine\Bundle\PHPCRBundle\Form\Type\PHPCRODMReferenceCollectionType">
+ <tag name="form.type" alias="phpcr_odm_reference_collection"/>
+ <argument type="service" id="doctrine_phpcr.odm.document_manager"/>
+ </service>
+
<service id="form.type.phpcr.document" class="Doctrine\Bundle\PHPCRBundle\Form\Type\DocumentType">
<tag name="form.type" alias="phpcr_document"/>
<argument type="service" id="doctrine_phpcr"/>

0 comments on commit 9808979

Please sign in to comment.