1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HMLB\DDDBundle\Doctrine\ODM\MongoDB; |
4
|
|
|
|
5
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
6
|
|
|
use Doctrine\ODM\MongoDB\DocumentRepository; |
7
|
|
|
use MongoId; |
8
|
|
|
use HMLB\DDD\Entity\AggregateRoot; |
9
|
|
|
use HMLB\DDD\Entity\Repository; |
10
|
|
|
use HMLB\DDD\Entity\Identity; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Aggregate root repository implementation for ORM. |
14
|
|
|
* |
15
|
|
|
* @author Hugues Maignol <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractMongoRepository implements Repository |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var DocumentManager |
21
|
|
|
*/ |
22
|
|
|
protected $dm; |
23
|
|
|
/** |
24
|
|
|
* @var DocumentRepository |
25
|
|
|
*/ |
26
|
|
|
protected $documentRepository; |
27
|
|
|
|
28
|
|
|
public function __construct(DocumentManager $dm = null) |
29
|
|
|
{ |
30
|
|
|
$this->dm = $dm; |
31
|
|
|
$this->documentRepository = $dm->getRepository($this->getClassName()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return AggregateRoot[] |
36
|
|
|
*/ |
37
|
|
|
public function getAll() |
38
|
|
|
{ |
39
|
|
|
return $this->documentRepository->findAll(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Identity $identity |
44
|
|
|
* |
45
|
|
|
* @return AggregateRoot |
46
|
|
|
*/ |
47
|
|
|
public function get(Identity $identity) |
48
|
|
|
{ |
49
|
|
|
return $this->documentRepository->find($identity); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param AggregateRoot $document |
54
|
|
|
*/ |
55
|
|
|
public function add(AggregateRoot $document) |
56
|
|
|
{ |
57
|
|
|
$this->dm->persist($document); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Finds Entities by a set of criteria. |
62
|
|
|
* |
63
|
|
|
* @param array $criteria Query criteria |
64
|
|
|
* @param array $sort Sort array for Cursor::sort() |
65
|
|
|
* @param int|null $limit Limit for Cursor::limit() |
66
|
|
|
* @param int|null $skip Skip for Cursor::skip() |
67
|
|
|
* |
68
|
|
|
* @return AggregateRoot[] |
69
|
|
|
*/ |
70
|
|
|
protected function getBy(array $criteria, array $sort = null, $limit = null, $skip = null) |
71
|
|
|
{ |
72
|
|
|
return $this->documentRepository->findBy($criteria, $sort, $limit, $skip); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Finds a single AggregateRoot by a set of criteria. |
77
|
|
|
* |
78
|
|
|
* @param array $criteria |
79
|
|
|
* |
80
|
|
|
* @return AggregateRoot |
81
|
|
|
*/ |
82
|
|
|
protected function getOneBy(array $criteria) |
83
|
|
|
{ |
84
|
|
|
return $this->documentRepository->findOneBy($criteria); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param Identity $id |
89
|
|
|
* |
90
|
|
|
* @return MongoId |
91
|
|
|
*/ |
92
|
|
|
protected function createMongoIdFromIdentity(Identity $id) |
93
|
|
|
{ |
94
|
|
|
return new MongoId((string) $id); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|