|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Cubiche package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) Cubiche |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Cubiche\Infrastructure\Repository\Doctrine\ODM\MongoDB; |
|
12
|
|
|
|
|
13
|
|
|
use Cubiche\Domain\Model\IdInterface; |
|
14
|
|
|
use Cubiche\Domain\Repository\Repository; |
|
15
|
|
|
use Doctrine\ODM\MongoDB\DocumentRepository as MongoDBDocumentRepository; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Document Repository Class. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
|
21
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class DocumentRepository extends Repository |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var MongoDBDocumentRepository |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $repository; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param MongoDBDocumentRepository $repository |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(MongoDBDocumentRepository $repository) |
|
34
|
|
|
{ |
|
35
|
|
|
parent::__construct($repository->getDocumentName()); |
|
36
|
|
|
|
|
37
|
|
|
$this->repository = $repository; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function get(IdInterface $id) |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->repository->find($id->toNative()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function persist($element) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->checkType($element); |
|
54
|
|
|
|
|
55
|
|
|
$this->dm()->persist($element); |
|
56
|
|
|
$this->dm()->flush($element); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
|
|
public function persistAll($elements) |
|
63
|
|
|
{ |
|
64
|
|
|
foreach ($elements as $element) { |
|
65
|
|
|
$this->checkType($element); |
|
66
|
|
|
$this->dm()->persist($element); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->dm()->flush(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
public function remove($element) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->checkType($element); |
|
78
|
|
|
|
|
79
|
|
|
$this->dm()->remove($element); |
|
80
|
|
|
$this->dm()->flush(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getIterator() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->repository->createQueryBuilder()->getQuery()->getIterator(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return \Doctrine\ODM\MongoDB\DocumentManager |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function dm() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->repository->getDocumentManager(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|