Completed
Push — master ( 119440...87e4b9 )
by Peter
16:02
created

EntityManagerTrait::deleteByPk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link http://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Traits;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\EntityManager;
18
use Maslosoft\Mangan\Exceptions\ManganException;
19
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
20
use Maslosoft\Mangan\Interfaces\EntityManagerInterface;
21
use Maslosoft\Mangan\Modifier;
22
use MongoCollection;
23
24
/**
25
 * EntityManagerTrait
26
 * @see EntityManagerInterface
27
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
28
 */
29
trait EntityManagerTrait
30
{
31
32
	/**
33
	 * Entity manager
34
	 * @var EntityManagerInterface
35
	 */
36
	private $_em = null;
37
38
	/**
39
	 * Replaces the current document.
40
	 *
41
	 * **NOTE: This will overwrite entire document.**
42
	 * Any filtered out properties will be removed as well.
43
	 *
44
	 * The record is inserted as a documnent into the database collection, if exists it will be replaced.
45
	 *
46
	 * Validation will be performed before saving the record. If the validation fails,
47
	 * the record will not be saved. You can call {@link getErrors()} to retrieve the
48
	 * validation errors.
49
	 *
50
	 * @param boolean $runValidation whether to perform validation before saving the record.
51
	 * If the validation fails, the record will not be saved to database.
52
	 * @param AnnotatedInterface $model if want to insert different model than set in constructor
0 ignored issues
show
Bug introduced by
There is no parameter named $model. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
53
	 * @return boolean whether the saving succeeds
54
	 * @since v1.0
55
	 */
56
	public function replace($runValidation = true)
57
	{
58
		return $this->_getEm()->replace($runValidation);
59
	}
60
61
	/**
62
	 * Saves the current record.
63
	 *
64
	 * The record is inserted as a row into the database table if its {@link isNewRecord}
65
	 * property is true (usually the case when the record is created using the 'new'
66
	 * operator). Otherwise, it will be used to update the corresponding row in the table
67
	 * (usually the case if the record is obtained using one of those 'find' methods.)
68
	 *
69
	 * Validation will be performed before saving the record. If the validation fails,
70
	 * the record will not be saved. You can call {@link getErrors()} to retrieve the
71
	 * validation errors.
72
	 *
73
	 * If the record is saved via insertion, its {@link isNewRecord} property will be
74
	 * set false, and its {@link scenario} property will be set to be 'update'.
75
	 * And if its primary key is auto-incremental and is not set before insertion,
76
	 * the primary key will be populated with the automatically generated key value.
77
	 *
78
	 * @param boolean $runValidation whether to perform validation before saving the record.
79
	 * If the validation fails, the record will not be saved to database.
80
	 *
81
	 * @return boolean whether the saving succeeds
82
	 * @since v1.0
83
	 * @Ignored
84
	 */
85 21
	public function save($runValidation = true)
86
	{
87 21
		return $this->_getEm()->save($runValidation);
88
	}
89
90
	/**
91
	 * Updates or inserts the current document. This will try to update existing fields.
92
	 * Will keep already stored data if present in document.
93
	 *
94
	 * If document does not exist, a new one will be inserted.
95
	 *
96
	 * @param boolean $runValidation
97
	 * @return boolean
98
	 * @throws ManganException
99
	 */
100
	public function upsert($runValidation = true)
101
	{
102
		return $this->_getEm()->upsert($runValidation);
103
	}
104
105
	/**
106
	 * Inserts a row into the table based on this active record attributes.
107
	 * If the table's primary key is auto-incremental and is null before insertion,
108
	 * it will be populated with the actual value after insertion.
109
	 * Note, validation is not performed in this method. You may call {@link validate} to perform the validation.
110
	 * After the record is inserted to DB successfully, its {@link isNewRecord} property will be set false,
111
	 * and its {@link scenario} property will be set to be 'update'.
112
	 * @param AnnotatedInterface $model if want to insert different model than set in constructor
113
	 * @return boolean whether the attributes are valid and the record is inserted successfully.
114
	 * @throws ManganException if the record is not new
115
	 * @throws ManganException on fail of insert or insert of empty document
116
	 * @throws ManganException on fail of insert, when safe flag is set to true
117
	 * @throws ManganException on timeout of db operation , when safe flag is set to true
118
	 * @since v1.0
119
	 * @Ignored
120
	 */
121 3
	public function insert(AnnotatedInterface $model = null)
122
	{
123 3
		return $this->_getEm()->insert($model);
124
	}
125
126
	/**
127
	 * Updates the row represented by this active record.
128
	 * All loaded attributes will be saved to the database.
129
	 * Note, validation is not performed in this method. You may call {@link validate} to perform the validation.
130
	 * @param array $attributes list of attributes that need to be updated. Defaults to null,
131
	 * meaning all attributes that are loaded from DB will be saved.
132
	 * @return boolean whether the update is successful
133
	 * @throws ManganException if the record is new
134
	 * @throws ManganException on fail of update
135
	 * @throws ManganException on timeout of db operation , when safe flag is set to true
136
	 * @since v1.0
137
	 * @Ignored
138
	 */
139 2
	public function update(array $attributes = null)
140
	{
141 2
		return $this->_getEm()->update($attributes);
142
	}
143
144
	/**
145
	 * Updates one document with the specified criteria and attributes
146
	 *
147
	 * This is more *raw* update:
148
	 *
149
	 * * Does not raise any events or signals
150
	 * * Does not perform any validation
151
	 *
152
	 * @param array|CriteriaInterface $criteria query criteria.
153
	 * @param array $attributes list of attributes that need to be saved. Defaults to null,
154
	 * meaning all attributes that are loaded from DB will be saved.
155
	 * @param bool Whether tu force update/upsert document
156
	 * @since v1.0
157
	 */
158
	public function updateOne($criteria = null, array $attributes = null, $modify = false)
159
	{
160
		return $this->_getEm()->updateOne($criteria, $attributes, $modify);
161
	}
162
163
	/**
164
	 * Atomic, in-place update method.
165
	 *
166
	 * @since v1.3.6
167
	 * @param Modifier $modifier updating rules to apply
168
	 * @param CriteriaInterface $criteria condition to limit updating rules
169
	 * @return boolean|mixed[]
170
	 * @Ignored
171
	 */
172
	public function updateAll(Modifier $modifier, CriteriaInterface $criteria = null)
173
	{
174
		return $this->_getEm()->updateAll($modifier, $criteria);
175
	}
176
177
	/**
178
	 * Deletes the row corresponding to this Document.
179
	 * @return boolean whether the deletion is successful.
180
	 * @throws ManganException if the record is new
181
	 * @since v1.0
182
	 * @Ignored
183
	 */
184 6
	public function delete()
185
	{
186 6
		return $this->_getEm()->delete();
187
	}
188
189
	/**
190
	 * Deletes document with the specified primary key.
191
	 * See {@link find()} for detailed explanation about $condition and $params.
192
	 * @param mixed $pkValue primary key value(s). Use array for multiple primary keys. For composite key, each key value must be an array (column name=>column value).
193
	 * @param array|CriteriaInterface $criteria query criteria.
194
	 * @since v1.0
195
	 * @Ignored
196
	 */
197 1
	public function deleteByPk($pkValue, $criteria = null)
198
	{
199 1
		return $this->_getEm()->deleteByPk($pkValue, $criteria);
200
	}
201
202
	/**
203
	 * Deletes documents with the specified primary keys.
204
	 * See {@link find()} for detailed explanation about $condition and $params.
205
	 * @param mixed[] $pkValues Primary keys array
206
	 * @param array|CriteriaInterface $criteria query criteria.
207
	 * @since v1.0
208
	 * @Ignored
209
	 */
210 1
	public function deleteAllByPk($pkValues, $criteria = null)
211
	{
212 1
		return $this->_getEm()->deleteAllByPk($pkValues, $criteria);
213
	}
214
215
	/**
216
	 * Deletes documents with the specified criteria.
217
	 * See {@link find()} for detailed explanation about $condition and $params.
218
	 * @param array|CriteriaInterface $criteria query criteria.
219
	 * @since v1.0
220
	 * @Ignored
221
	 */
222
	public function deleteAll($criteria = null)
223
	{
224
		return $this->_getEm()->deleteAll($criteria);
225
	}
226
227
	/**
228
	 * Deletes one document with the specified primary keys.
229
	 * <b>Does not raise beforeDelete</b>
230
	 * See {@link find()} for detailed explanation about $condition and $params.
231
	 * @param array|CriteriaInterface $criteria query criteria.
232
	 * @since v1.0
233
	 * @Ignored
234
	 */
235
	public function deleteOne($criteria = null)
236
	{
237
		return $this->_getEm()->deleteOne($criteria);
238
	}
239
240
	/**
241
	 * Repopulates this active record with the latest data.
242
	 * @return boolean whether the row still exists in the database. If true, the latest data will be populated to this active record.
243
	 * @since v1.0
244
	 * @Ignored
245
	 */
246
	public function refresh()
247
	{
248
		return $this->_getEm()->refresh();
249
	}
250
251
	/**
252
	 *
253
	 * @return MongoCollection
254
	 * @Ignored
255
	 */
256
	public function getCollection()
257
	{
258
		return $this->_getEm()->getCollection();
259
	}
260
261 26
	private function _getEm()
262
	{
263 26
		if (null === $this->_em)
264 26
		{
265 26
			$this->_em = EntityManager::create($this);
266 26
		}
267 26
		return $this->_em;
268
	}
269
270
}
271