1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\repositories; |
4
|
|
|
|
5
|
|
|
use Ubiquity\controllers\Controller; |
6
|
|
|
use Ubiquity\orm\DAO; |
7
|
|
|
use Ubiquity\views\View; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A repository for managing CRUD operations on a model. |
11
|
|
|
* Ubiquity\orm\repositories$AbstractRepository |
12
|
|
|
* This class is part of Ubiquity |
13
|
|
|
* |
14
|
|
|
* @author jc |
15
|
|
|
* @version 1.0.3 |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
abstract class AbstractRepository { |
19
|
|
|
|
20
|
|
|
abstract protected function getModel(): string; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Load all instances. |
24
|
|
|
* |
25
|
|
|
* @param string $condition |
26
|
|
|
* @param array|boolean $included |
27
|
|
|
* @param array $parameters |
28
|
|
|
* @param bool $useCache |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function all(string $condition = '', $included = false, array $parameters = [ ], bool $useCache = false): array { |
32
|
|
|
return DAO::getAll ( $this->getModel (), $condition, $included, $parameters, $useCache ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Load all instances with order. |
37
|
|
|
* |
38
|
|
|
* @param string $field The field to order by |
39
|
|
|
* @param string $order The order (ASC, DESC) |
40
|
|
|
* @param string $condition The condition |
41
|
|
|
* @param bool|array $included The relations to include |
42
|
|
|
* @param array $parameters The parameters for the condition |
43
|
|
|
* @param bool $useCache If true, use the cache |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function orderBy(string $field, string $order='ASC', string $condition = '', $included = false, array $parameters = [ ], bool $useCache = false): array { |
47
|
|
|
return DAO::orderBy($this->getModel(), $field, $order, $condition, $included, $parameters, $useCache); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Load one instance by id. |
52
|
|
|
* |
53
|
|
|
* @param $keyValues |
54
|
|
|
* @param bool|array $included |
55
|
|
|
* @param bool $useCache |
56
|
|
|
* @return ?object |
57
|
|
|
*/ |
58
|
|
|
public function byId($keyValues, $included = true, bool $useCache = false): ?object { |
59
|
|
|
return DAO::getById ( $this->getModel (), $keyValues, $included, $useCache ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Load one instance. |
64
|
|
|
* |
65
|
|
|
* @param string $condition |
66
|
|
|
* @param bool|array $included |
67
|
|
|
* @param array $parameters |
68
|
|
|
* @param bool $useCache |
69
|
|
|
* @return ?object |
70
|
|
|
* @throws \Ubiquity\exceptions\DAOException |
71
|
|
|
*/ |
72
|
|
|
public function one(string $condition = '', $included = true, array $parameters = [ ], bool $useCache = false): ?object { |
73
|
|
|
return DAO::getOne ( $this->getModel (), $condition, $included, $parameters, $useCache ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Insert a new instance $instance into the database. |
78
|
|
|
* |
79
|
|
|
* @param object $instance |
80
|
|
|
* @param bool $insertMany |
81
|
|
|
* @return bool |
82
|
|
|
* @throws \Exception |
83
|
|
|
*/ |
84
|
|
|
public function insert(object $instance, bool $insertMany = false): bool { |
85
|
|
|
return DAO::insert ( $instance, $insertMany ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Update an instance $instance in the database. |
90
|
|
|
* |
91
|
|
|
* @param object $instance |
92
|
|
|
* @param bool $insertMany |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function update(object $instance, bool $insertMany = false): bool { |
96
|
|
|
return DAO::update ( $instance, $insertMany ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Save (insert or update) an instance $instance in the database. |
101
|
|
|
* |
102
|
|
|
* @param object $instance |
103
|
|
|
* @param bool $insertMany |
104
|
|
|
* @return bool|int |
105
|
|
|
*/ |
106
|
|
|
public function save(object $instance, bool $insertMany = false) { |
107
|
|
|
return DAO::save ( $instance, $insertMany ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Remove an instance $instance from the database. |
112
|
|
|
* |
113
|
|
|
* @param object $instance |
114
|
|
|
* @return int|null |
115
|
|
|
*/ |
116
|
|
|
public function remove(object $instance): ?int { |
117
|
|
|
return DAO::remove ( $instance ); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns the number of instances. |
122
|
|
|
* |
123
|
|
|
* @param string $condition |
124
|
|
|
* @param array $parameters |
125
|
|
|
* @return int |
126
|
|
|
*/ |
127
|
|
|
public function count(string $condition='',?array $parameters=null):int { |
128
|
|
|
return DAO::count($this->getModel(),$condition,$parameters); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|