Created
April 17, 2018 07:15
-
-
Save umpirsky/d0f9d5e884fb4ae2f08fb9c7f81b2f13 to your computer and use it in GitHub Desktop.
Workaround for https://github.com/ramsey/uuid-doctrine/issues/16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<service id="umpirsky.form.type.uuid_entity_type" class="Umpirsky\Form\Type\UuidEntityType"> | |
<argument type="service" id="doctrine" /> | |
<tag name="form.type" /> | |
</service> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Umpirsky\Form\ChoiceList; | |
use Doctrine\ORM\QueryBuilder; | |
use Doctrine\DBAL\Connection; | |
use Doctrine\DBAL\Types\Type; | |
use Doctrine\DBAL\Platforms\AbstractPlatform; | |
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface; | |
use Symfony\Component\Form\Exception\LogicException; | |
class ORMUuidQueryBuilderLoader implements EntityLoaderInterface | |
{ | |
private $queryBuilder; | |
private $platform; | |
public function __construct(QueryBuilder $queryBuilder, AbstractPlatform $platform) | |
{ | |
$this->queryBuilder = $queryBuilder; | |
$this->platform = $platform; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getEntities() | |
{ | |
return $this->queryBuilder->getQuery()->execute(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getEntitiesByIds($identifier, array $values) | |
{ | |
$qb = clone $this->queryBuilder; | |
$alias = current($qb->getRootAliases()); | |
$parameter = 'ORMUuidQueryBuilderLoader_getEntitiesByIds_'.$identifier; | |
$parameter = str_replace('.', '_', $parameter); | |
$where = $qb->expr()->in($alias.'.'.$identifier, ':'.$parameter); | |
// Guess type | |
$entity = current($qb->getRootEntities()); | |
$metadata = $qb->getEntityManager()->getClassMetadata($entity); | |
if ('uuid_binary_ordered_time' !== $metadata->getTypeOfField($identifier)) { | |
throw new LogicException(sprintf( | |
'ORMUuidQueryBuilderLoader supports uuid_binary_ordered_time identifiers only, %s given.', | |
$metadata->getTypeOfField($identifier) | |
)); | |
} | |
$parameterType = Connection::PARAM_STR_ARRAY; | |
$values = array_map(function ($value) { | |
return Type::getType('uuid_binary_ordered_time')->convertToDatabaseValue($value, $this->platform); | |
}, array_values(array_filter($values, function ($v) { | |
return '' !== (string) $v; | |
}))); | |
if (!$values) { | |
return []; | |
} | |
return $qb->andWhere($where) | |
->getQuery() | |
->setParameter($parameter, $values, $parameterType) | |
->getResult() | |
; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Umpirsky\Form\Type; | |
use Umpirsky\Form\ChoiceList\ORMUuidQueryBuilderLoader; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use Symfony\Bridge\Doctrine\Form\Type\EntityType; | |
class UuidEntityType extends EntityType | |
{ | |
public function getLoader(ObjectManager $manager, $queryBuilder, $class) | |
{ | |
return new ORMUuidQueryBuilderLoader($queryBuilder, $manager->getConnection()->getDatabasePlatform()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment