Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DoctrineBridge] Refactor entity choice list to be ORM independant us…
…ing an EntityLoader interface.
- Loading branch information
Showing
4 changed files
with
156 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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
36 changes: 36 additions & 0 deletions
36
src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityLoaderInterface.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?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\ChoiceList; | ||
|
||
/** | ||
* Custom loader for entities in the choice list. | ||
* | ||
* @author Benjamin Eberlei <kontakt@beberlei.de> | ||
*/ | ||
interface EntityLoaderInterface | ||
{ | ||
/** | ||
* Given choice list values this method returns the appropriate entities for it. | ||
* | ||
* @param array $identifier | ||
* @param array $choiceListKeys Array of values of the select option, checkbox or radio button. | ||
* @return object[] | ||
*/ | ||
function getEntitiesByKeys(array $identifier, array $choiceListKeys); | ||
|
||
/** | ||
* Return an array of entities that are valid choices in the corresponding choice list. | ||
* | ||
* @return array | ||
*/ | ||
function getEntities(); | ||
} |
86 changes: 86 additions & 0 deletions
86
src/Symfony/Bridge/Doctrine/Form/ChoiceList/ORMQueryBuilderLoader.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?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\ChoiceList; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Symfony\Component\Form\Exception\FormException; | ||
use Symfony\Component\Form\Exception\UnexpectedTypeException; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
/** | ||
* Getting Entities through the ORM QueryBuilder | ||
*/ | ||
class ORMQueryBuilderLoader implements EntityLoaderInterface | ||
{ | ||
/** | ||
* Contains the query builder that builds the query for fetching the | ||
* entities | ||
* | ||
* This property should only be accessed through queryBuilder. | ||
* | ||
* @var Doctrine\ORM\QueryBuilder | ||
*/ | ||
private $queryBuilder; | ||
|
||
/** | ||
* Construct an ORM Query Builder Loader | ||
* | ||
* @param QueryBuilder $queryBuilder | ||
* @param EntityManager $manager | ||
* @param string $class | ||
*/ | ||
public function __construct($queryBuilder, $manager = null, $class = null) | ||
{ | ||
// If a query builder was passed, it must be a closure or QueryBuilder | ||
// instance | ||
if (!(null === $queryBuilder || $queryBuilder instanceof QueryBuilder || $queryBuilder instanceof \Closure)) { | ||
throw new UnexpectedTypeException($queryBuilder, 'Doctrine\ORM\QueryBuilder or \Closure'); | ||
} | ||
|
||
if ($queryBuilder instanceof \Closure) { | ||
$queryBuilder = $queryBuilder($manager->getRepository($class)); | ||
|
||
if (!$queryBuilder instanceof QueryBuilder) { | ||
throw new UnexpectedTypeException($queryBuilder, 'Doctrine\ORM\QueryBuilder'); | ||
} | ||
} | ||
|
||
$this->queryBuilder = $queryBuilder; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getEntities() | ||
{ | ||
return $this->queryBuilder->getQuery()->execute(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getEntitiesByKeys(array $identifier, array $choiceListKeys) | ||
{ | ||
if (count($identifier) != 1) { | ||
throw new FormException("Only entities with one identifier supported by ORM QueryBuilder."); | ||
} | ||
|
||
$qb = clone ($this->queryBuilder); | ||
$alias = $qb->getRootAlias(); | ||
$where = $qb->expr()->in($alias.'.'.current($identifier), "?1"); | ||
|
||
return $qb->andWhere($where) | ||
->getQuery() | ||
->setParameter(1, $choiceListKeys, Connection::PARAM_STR_ARRAY) | ||
->getResult(); | ||
} | ||
} |
This file contains 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