Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Configurable array key #48

Merged
merged 3 commits into from

3 participants

@elHornair

Let the user choose wether to use the uuid or the path of the node as an array key. This PR includes no BC break

...sformer/ReferenceManyCollectionToArrayTransformer.php
@@ -3,6 +3,7 @@
namespace Doctrine\Bundle\PHPCRBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
+use Joiz\CmsBundle\Document\Show;
@dbu Collaborator
dbu added a note

this does not belong here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...sformer/ReferenceManyCollectionToArrayTransformer.php
@@ -38,7 +46,7 @@ public function transform($collection)
$arr = array();
foreach ($collection as $item) {
- $arr[] = $item->getPath();
+ $arr[] = $this->useUuidAsArrayKey ? $item->getNode()->getPropertyValue('jcr:uuid') : $item->getPath();
@dbu Collaborator
dbu added a note

you should use NodeInterface::getIdentifier instead of getPropertyValue(jcr:uuid)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Form/Type/PHPCRODMReferenceCollectionType.php
@@ -47,6 +47,11 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
{
parent::setDefaultOptions($resolver);
$resolver->setRequired((array('referenced_class')));
+ $resolver->setOptional(array('use_uuid_as_array_key'));
+
+ $resolver->setDefaults(array(
+ 'use_uuid_as_array_key' => false,
@dbu Collaborator
dbu added a note

i am not too happy with the name of this option. it is quite long. but i can't think of a much better name. by_uuid would be short but maybe too short. have any ideas?

@dantleech Collaborator

Maye "key" or "index_by" as enum with values "path" or "uuid" with default. (sorry no special chars on phone)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dbu
Collaborator
dbu commented

sounds doable, i commented on some issues. i'd love to hear the exact use case why we need the option - but as we are talking about references, maybe the uuid would make more sense even as a default?

btw, are you sure that for the reverse transform we do not need to distinguish the cases?

@elHornair

Thx for the feedback, did the corrections.

We need it because sometimes a CMF user wants to identify his content by path, sometimes by UUID.
Yes, I'm sure - ReferenceManyCollection can take both, UUIDs and paths, so it doesn't matter, what we pass it in the reverse transform.

Anything else that should be adapted before it can be merged?

...sformer/ReferenceManyCollectionToArrayTransformer.php
((14 lines not shown))
*/
- function __construct(DocumentManager $dm, $referencedClass)
+ function __construct(DocumentManager $dm, $referencedClass, $key)
@dantleech Collaborator

I'm not sure if we should have a default value, i.e. KEY_UUID or KEY_PATH, @dbu ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...sformer/ReferenceManyCollectionToArrayTransformer.php
@@ -9,6 +9,9 @@
class ReferenceManyCollectionToArrayTransformer implements DataTransformerInterface
{
@dantleech Collaborator

I don't think we need this blank line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dbu dbu merged commit cf41680 into from
@dbu
Collaborator
dbu commented

thanks alain! and thanks dan for the feedbacks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 4, 2013
  1. @elHornair
Commits on Apr 6, 2013
  1. @elHornair
  2. @elHornair

    made uuid the default value

    elHornair authored
This page is out of date. Refresh to see the latest.
View
21 Form/DataTransformer/ReferenceManyCollectionToArrayTransformer.php
@@ -8,6 +8,8 @@
class ReferenceManyCollectionToArrayTransformer implements DataTransformerInterface
{
+ const KEY_PATH = 'path';
+ const KEY_UUID = 'uuid';
/**
* @var \Doctrine\ODM\PHPCR\DocumentManager $dm
@@ -19,15 +21,30 @@ class ReferenceManyCollectionToArrayTransformer implements DataTransformerInterf
*/
protected $referencedClass;
+ /**
+ * @var string $key
+ */
+ protected $key;
/**
* @param \Doctrine\ODM\PHPCR\DocumentManager $dm
* @param $referencedClass
+ * @param string $key
+ * @throws \InvalidArgumentException
*/
- function __construct(DocumentManager $dm, $referencedClass)
+ function __construct(DocumentManager $dm, $referencedClass, $key = self::KEY_UUID)
{
$this->dm = $dm;
$this->referencedClass = $referencedClass;
+
+ if (!($key === self::KEY_UUID || $key === self::KEY_PATH)) {
+ throw new \InvalidArgumentException(sprintf(
+ 'Key must be either KEY_UUID or KEY_PATH. Received "%s"',
+ $key
+ ));
+ }
+
+ $this->key = $key;
}
/**
@@ -38,7 +55,7 @@ public function transform($collection)
$arr = array();
foreach ($collection as $item) {
- $arr[] = $item->getPath();
+ $arr[] = ($this->key === self::KEY_UUID) ? $item->getNode()->getIdentifier() : $item->getPath();
}
return $arr;
View
7 Form/Type/PHPCRODMReferenceCollectionType.php
@@ -47,6 +47,11 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
{
parent::setDefaultOptions($resolver);
$resolver->setRequired((array('referenced_class')));
+ $resolver->setOptional(array('key'));
+
+ $resolver->setDefaults(array(
+ 'key' => ReferenceManyCollectionToArrayTransformer::KEY_UUID,
+ ));
}
/**
@@ -54,7 +59,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
- $transformer = new ReferenceManyCollectionToArrayTransformer($this->dm, $options['referenced_class']);
+ $transformer = new ReferenceManyCollectionToArrayTransformer($this->dm, $options['referenced_class'], $options['key']);
$builder->addModelTransformer($transformer);
}
}
Something went wrong with that request. Please try again.