|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) FriendsOfSymfony <https://friendsofsymfony.github.com/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace FOS\ElasticaBundle\Doctrine\ORM; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\ORM\Query; |
|
15
|
|
|
use FOS\ElasticaBundle\Doctrine\AbstractElasticaToModelTransformer; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Maps Elastica documents with Doctrine objects |
|
19
|
|
|
* This mapper assumes an exact match between |
|
20
|
|
|
* elastica documents ids and doctrine object ids. |
|
21
|
|
|
*/ |
|
22
|
|
|
class ElasticaToModelTransformer extends AbstractElasticaToModelTransformer |
|
23
|
|
|
{ |
|
24
|
|
|
public const ENTITY_ALIAS = 'o'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Fetch objects for theses identifier values. |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $identifierValues ids values |
|
30
|
|
|
* @param bool $hydrate whether or not to hydrate the objects, false returns arrays |
|
31
|
|
|
* |
|
32
|
|
|
* @return array of objects or arrays |
|
33
|
|
|
*/ |
|
34
|
1 |
|
protected function findByIdentifiers(array $identifierValues, $hydrate) |
|
35
|
|
|
{ |
|
36
|
1 |
|
if (empty($identifierValues)) { |
|
37
|
|
|
return []; |
|
38
|
|
|
} |
|
39
|
1 |
|
$hydrationMode = $hydrate ? Query::HYDRATE_OBJECT : Query::HYDRATE_ARRAY; |
|
40
|
|
|
|
|
41
|
1 |
|
$qb = $this->getEntityQueryBuilder(); |
|
42
|
1 |
|
$qb->andWhere($qb->expr()->in(static::ENTITY_ALIAS.'.'.$this->options['identifier'], ':values')) |
|
43
|
1 |
|
->setParameter('values', $identifierValues); |
|
44
|
|
|
|
|
45
|
1 |
|
$query = $qb->getQuery(); |
|
46
|
|
|
|
|
47
|
1 |
|
foreach ($this->options['hints'] as $hint) { |
|
48
|
1 |
|
$query->setHint($hint['name'], $hint['value']); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
return $query->setHydrationMode($hydrationMode)->execute(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Retrieves a query builder to be used for querying by identifiers. |
|
56
|
|
|
* |
|
57
|
|
|
* @return \Doctrine\ORM\QueryBuilder |
|
58
|
|
|
*/ |
|
59
|
3 |
|
protected function getEntityQueryBuilder() |
|
60
|
|
|
{ |
|
61
|
3 |
|
$repository = $this->registry |
|
62
|
3 |
|
->getManagerForClass($this->objectClass) |
|
63
|
3 |
|
->getRepository($this->objectClass); |
|
64
|
|
|
|
|
65
|
3 |
|
return $repository->{$this->options['query_builder_method']}(static::ENTITY_ALIAS); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|