-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepositoryInterface.php
292 lines (270 loc) · 12.2 KB
/
RepositoryInterface.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
namespace pc\entity;
use yii\base\Exception;
use yii\db\ActiveQueryInterface;
interface RepositoryInterface
{
/**
* Populates an active record object using a row of data from the database/storage.
*
* This is an internal method meant to be called to create active record objects after
* fetching data from the database. It is mainly used by [[ActiveQuery]] to populate
* the query results into active records.
*
* When calling this method manually you should call [[afterFind()]] on the created
* record to trigger the [[EVENT_AFTER_FIND|afterFind Event]].
*
* @param Entity $record the record to be populated. In most cases this will be an instance
* created by [[instantiate()]] beforehand.
* @param array $row attribute values (name => value)
*/
public static function populateRecord($record, $row);
/**
* Creates an active record instance.
*
* This method is called together with [[populateRecord()]] by [[ActiveQuery]].
* It is not meant to be used for creating new records directly.
*
* You may override this method if the instance being created
* depends on the row data to be populated into the record.
* For example, by creating a record based on the value of a column,
* you may implement the so-called single-table inheritance mapping.
* @param array $row row data to be populated into the record.
* @return static the newly created active record
*/
public function instantiate($row);
/**
* Returns the primary key **name(s)** for this repository.
*
* Note that an array should be returned even when the record only has a single primary key.
*
* For the primary key **value** see [[getPrimaryKey()]] instead.
*
* @return string[] the primary key name(s) for this repository.
*/
public function primaryKey();
/**
* Returns a value indicating whether the given set of attributes represents the primary key for this model
* @param array $keys the set of attributes to check
* @return boolean whether the given set of attributes represents the primary key for this model
*/
public function isPrimaryKey($keys);
/**
* Returns the connection used by this repository.
* @return mixed the database connection used by this repository.
*/
public function getDb();
/**
* Creates an [[ActiveQueryInterface]] instance for query purpose.
*
* @return ActiveQueryInterface the newly created [[ActiveQueryInterface]] instance.
*/
public function find();
/**
* Returns a single active record model instance by a primary key or an array of column values.
*
* @param mixed $condition primary key value or a set of column values
* @return static|null ActiveRecord instance matching the condition, or null if nothing matches.
*/
public function findOne($condition);
/**
* Returns a list of active record models that match the specified primary key value(s) or a set of column values.
*
* @param mixed $condition primary key value or a set of column values
* @return array an array of ActiveRecord instance, or an empty array if nothing matches.
*/
public function findAll($condition);
/**
* Updates records using the provided attribute values and conditions.
* For example, to change the status to be 1 for all customers whose status is 2:
*
* ```php
* Customer::updateAll(['status' => 1], ['status' => '2']);
* ```
*
* @param array $attributes attribute values (name-value pairs) to be saved for the record.
* Unlike [[update()]] these are not going to be validated.
* @param array $condition the condition that matches the records that should get updated.
* Please refer to [[QueryInterface::where()]] on how to specify this parameter.
* An empty condition will match all records.
* @return integer the number of rows updated
*/
public function updateAll($attributes, $condition = null);
/**
* Deletes records using the provided conditions.
* WARNING: If you do not specify any condition, this method will delete ALL rows in the table.
*
* For example, to delete all customers whose status is 3:
*
* ```php
* Customer::deleteAll([status = 3]);
* ```
*
* @param array $condition the condition that matches the records that should get deleted.
* Please refer to [[QueryInterface::where()]] on how to specify this parameter.
* An empty condition will match all records.
* @return integer the number of rows deleted
*/
public function deleteAll($condition = null);
/**
* Updates the whole table using the provided counter changes and conditions.
* For example, to increment all customers' age by 1,
*
* ```php
* Customer::updateAllCounters(['age' => 1]);
* ```
*
* @param array $counters the counters to be updated (attribute name => increment value).
* Use negative values if you want to decrement the counters.
* @param string|array $condition the conditions that will be put in the WHERE part of the UPDATE SQL.
* Please refer to [[Query::where()]] on how to specify this parameter.
* @param array $params the parameters (name => value) to be bound to the query.
* Do not name the parameters as `:bp0`, `:bp1`, etc., because they are used internally by this method.
* @return integer the number of rows updated
*/
public function updateAllCounters($counters, $condition = '', $params = []);
/**
* Saves the current record.
*
* This method will call [[insert()]] when [[getIsNewRecord()|isNewRecord]] is true, or [[update()]]
* when [[getIsNewRecord()|isNewRecord]] is false.
*
* For example, to save a customer record:
*
* ```php
* $customer = new Customer; // or $customer = Customer::findOne($id);
* $customer->name = $name;
* $customer->email = $email;
* $customer->save();
* ```
*
* @param Entity $entity
* @param array $attributeNames list of attribute names that need to be saved. Defaults to null,
* meaning all attributes that are loaded from DB will be saved.
* @return boolean whether the saving succeeded (i.e. no validation errors occurred).
*/
public function save($entity, $attributeNames = null);
/**
* Inserts the record into the database using the attribute values of this record.
*
* Usage example:
*
* ```php
* $customer = new Customer;
* $customer->name = $name;
* $customer->email = $email;
* $customer->insert();
* ```
*
* @param Entity $entity
* @param array $attributes list of attributes that need to be saved. Defaults to null,
* meaning all attributes that are loaded from DB will be saved.
* @return boolean whether the attributes are valid and the record is inserted successfully.
*/
public function insert($entity, $attributes = null);
/**
* Saves the changes to this active record into the database.
*
* Usage example:
*
* ```php
* $customer = Customer::findOne($id);
* $customer->name = $name;
* $customer->email = $email;
* $customer->update();
* ```
*
* @param Entity $entity
* @param array $attributeNames list of attributes that need to be saved. Defaults to null,
* meaning all attributes that are loaded from DB will be saved.
* @return integer|boolean the number of rows affected, or false if validation fails
* or updating process is stopped for other reasons.
* Note that it is possible that the number of rows affected is 0, even though the
* update execution is successful.
*/
public function update($entity, $attributeNames = null);
/**
* Updates the specified attributes.
*
* This method is a shortcut to [[update()]] when data validation is not needed
* and only a small set attributes need to be updated.
*
* You may specify the attributes to be updated as name list or name-value pairs.
* If the latter, the corresponding attribute values will be modified accordingly.
* The method will then save the specified attributes into database.
*
* Note that this method will **not** perform data validation and will **not** trigger events.
*
* @param Entity $entity
* @param array $attributes the attributes (names or name-value pairs) to be updated
* @return integer the number of rows affected.
*/
public function updateAttributes($entity, $attributes);
/**
* Updates one or several counter columns for the current AR object.
* Note that this method differs from [[updateAllCounters()]] in that it only
* saves counters for the current AR object.
*
* An example usage is as follows:
*
* ```php
* $post = Post::findOne($id);
* $post->updateCounters(['view_count' => 1]);
* ```
*
* @param Entity $entity
* @param array $counters the counters to be updated (attribute name => increment value)
* Use negative values if you want to decrement the counters.
* @return boolean whether the saving is successful
* @see updateAllCounters()
*/
public function updateCounters($entity, $counters);
/**
* Returns the old primary key value(s).
* This refers to the primary key value that is populated into the record
* after executing a find method (e.g. find(), findOne()).
* The value remains unchanged even if the primary key attribute is manually assigned with a different value.
* @param Entity $entity
* @param boolean $asArray whether to return the primary key value as an array. If true,
* the return value will be an array with column name as key and column value as value.
* If this is false (default), a scalar value will be returned for non-composite primary key.
* @property mixed The old primary key value. An array (column name => column value) is
* returned if the primary key is composite. A string is returned otherwise (null will be
* returned if the key value is null).
* @return mixed the old primary key value. An array (column name => column value) is returned if the primary key
* is composite or `$asArray` is true. A string is returned otherwise (null will be returned if
* the key value is null).
* @throws Exception if the AR model does not have a primary key
*/
public function getOldPrimaryKey($entity, $asArray = false);
/**
* Returns the primary key value(s).
* @param Entity $entity
* @param boolean $asArray whether to return the primary key value as an array. If true,
* the return value will be an array with column names as keys and column values as values.
* Note that for composite primary keys, an array will always be returned regardless of this parameter value.
* @property mixed The primary key value. An array (column name => column value) is returned if
* the primary key is composite. A string is returned otherwise (null will be returned if
* the key value is null).
* @return mixed the primary key value. An array (column name => column value) is returned if the primary key
* is composite or `$asArray` is true. A string is returned otherwise (null will be returned if
* the key value is null).
*/
public function getPrimaryKey($entity, $asArray = false);
/**
* Deletes the record from the database.
*
* @param Entity $entity
* @return integer|boolean the number of rows deleted, or false if the deletion is unsuccessful for some reason.
* Note that it is possible that the number of rows deleted is 0, even though the deletion execution is successful.
*/
public function delete($entity);
/**
* Returns a value indicating whether the given active record is the same as the current one.
* Two [[getIsNewRecord()|new]] records are considered to be not equal.
* @param Entity $source record to compare from
* @param Entity $target record to compare to
* @return boolean whether the two active records refer to the same row in the same database table.
*/
public function equals($source, $target);
}