1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Doctrine MongoDBBundle |
5
|
|
|
* |
6
|
|
|
* The code was originally distributed inside the Symfony framework. |
7
|
|
|
* |
8
|
|
|
* (c) Fabien Potencier <[email protected]> |
9
|
|
|
* (c) Doctrine Project |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Saxulum\DoctrineMongodbOdmManagerRegistry\Form\ChoiceList; |
16
|
|
|
|
17
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
18
|
|
|
use Doctrine\ODM\MongoDB\Query\Builder; |
19
|
|
|
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface; |
20
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Getting Entities through the MongoDB QueryBuilder |
24
|
|
|
*/ |
25
|
|
|
class MongoDBQueryBuilderLoader implements EntityLoaderInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Contains the query builder that builds the query for fetching the |
29
|
|
|
* entities |
30
|
|
|
* |
31
|
|
|
* This property should only be accessed through queryBuilder. |
32
|
|
|
* |
33
|
|
|
* @var Builder |
34
|
|
|
*/ |
35
|
|
|
private $queryBuilder; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Construct an ORM Query Builder Loader |
39
|
|
|
* |
40
|
|
|
* @param Builder|\Closure $queryBuilder |
41
|
|
|
* @param ObjectManager $manager |
42
|
|
|
* @param string $class |
43
|
|
|
*/ |
44
|
|
|
public function __construct($queryBuilder, ObjectManager $manager = null, $class = null) |
45
|
|
|
{ |
46
|
|
|
// If a query builder was passed, it must be a closure or QueryBuilder |
47
|
|
|
// instance |
48
|
|
|
if (!($queryBuilder instanceof Builder || $queryBuilder instanceof \Closure)) { |
49
|
|
|
throw new UnexpectedTypeException($queryBuilder, 'Doctrine\ODM\MongoDB\Query\Builder or \Closure'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($queryBuilder instanceof \Closure) { |
53
|
|
|
if (!$manager instanceof ObjectManager) { |
54
|
|
|
throw new UnexpectedTypeException($manager, 'Doctrine\Common\Persistence\ObjectManager'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$queryBuilder = $queryBuilder($manager->getRepository($class)); |
58
|
|
|
|
59
|
|
|
if (!$queryBuilder instanceof Builder) { |
60
|
|
|
throw new UnexpectedTypeException($queryBuilder, 'Doctrine\ODM\MongoDB\Query\Builder'); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->queryBuilder = $queryBuilder; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritDoc} |
69
|
|
|
*/ |
70
|
|
|
public function getEntities() |
71
|
|
|
{ |
72
|
|
|
return array_values($this->queryBuilder->getQuery()->execute()->toArray()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritDoc} |
77
|
|
|
*/ |
78
|
|
|
public function getEntitiesByIds($identifier, array $values) |
79
|
|
|
{ |
80
|
|
|
$qb = clone ($this->queryBuilder); |
81
|
|
|
|
82
|
|
|
return array_values($qb |
83
|
|
|
->field($identifier)->in($values) |
84
|
|
|
->getQuery() |
85
|
|
|
->execute() |
86
|
|
|
->toArray() |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|