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