1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoctrineElastic\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
6
|
|
|
use DoctrineElastic\Elastic\ElasticQueryBuilder; |
7
|
|
|
use DoctrineElastic\ElasticEntityManager; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Ands |
11
|
|
|
*/ |
12
|
|
|
class ElasticEntityRepository implements ObjectRepository { |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
protected $_entityName; |
16
|
|
|
|
17
|
|
|
/** @var ElasticEntityManager */ |
18
|
|
|
protected $_em; |
19
|
|
|
|
20
|
|
|
public function __construct(ElasticEntityManager $em, $className) { |
21
|
|
|
$this->_entityName = $className; |
22
|
|
|
$this->_em = $em; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Finds an object by its primary key / identifier. |
27
|
|
|
* |
28
|
|
|
* @param mixed $id The identifier. |
29
|
|
|
* |
30
|
|
|
* @return object The object. |
31
|
|
|
*/ |
32
|
|
|
public function find($id) { |
33
|
|
|
return $this->_em->find($this->_entityName, $id, null, null); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Finds all objects in the repository. |
38
|
|
|
* |
39
|
|
|
* @return array The objects. |
40
|
|
|
*/ |
41
|
|
|
public function findAll() { |
42
|
|
|
return $this->findBy([]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Finds objects by a set of criteria. |
47
|
|
|
* |
48
|
|
|
* Optionally sorting and limiting details can be passed. An implementation may throw |
49
|
|
|
* an UnexpectedValueException if certain values of the sorting or limiting details are |
50
|
|
|
* not supported. |
51
|
|
|
* |
52
|
|
|
* @param array $criteria |
53
|
|
|
* @param array|null $orderBy |
54
|
|
|
* @param int|null $limit |
55
|
|
|
* @param int|null $offset |
56
|
|
|
* |
57
|
|
|
* @return array The objects. |
58
|
|
|
* |
59
|
|
|
* @throws \UnexpectedValueException |
60
|
|
|
*/ |
61
|
|
|
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) { |
62
|
|
|
$persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName); |
63
|
|
|
|
64
|
|
|
return $persister->loadAll($criteria, $orderBy, $limit, $offset); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Finds a single object by a set of criteria. |
69
|
|
|
* |
70
|
|
|
* @param array $criteria The criteria. |
71
|
|
|
* @param array $orderBy |
72
|
|
|
* @return object The object. |
73
|
|
|
*/ |
74
|
|
|
public function findOneBy(array $criteria, array $orderBy = null) { |
75
|
|
|
$persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName); |
76
|
|
|
|
77
|
|
|
return $persister->load($criteria, null, null, array(), null, 1, $orderBy); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the class name of the object managed by the repository. |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getClassName() { |
86
|
|
|
return $this->_entityName; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Creates a new QueryBuilder instance that is prepopulated for this entity name. |
91
|
|
|
* |
92
|
|
|
* @param string $alias |
93
|
|
|
* @param string $indexBy The index for the from. |
94
|
|
|
* |
95
|
|
|
* @return ElasticQueryBuilder |
96
|
|
|
*/ |
97
|
|
|
public function createQueryBuilder($alias, $indexBy = null) { |
98
|
|
|
return $this->_em->createQueryBuilder() |
99
|
|
|
->select($alias) |
100
|
|
|
->from($this->_entityName, $alias, $indexBy); |
101
|
|
|
} |
102
|
|
|
} |