Completed
Push — master ( 17ce68...be2700 )
by Peter
17:48
created

EntityManagerTrait::deleteAllByPk()   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\Interfaces\CriteriaInterface;
19
use Maslosoft\Mangan\Interfaces\EntityManagerInterface;
20
use Maslosoft\Mangan\Modifier;
21
use MongoCollection;
22
use MongoException;
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 EntityManager
35
	 */
36
	private $_em = null;
37
38
	/**
39
	 * Saves the current record.
40
	 *
41
	 * The record is inserted as a row into the database table if its {@link isNewRecord}
42
	 * property is true (usually the case when the record is created using the 'new'
43
	 * operator). Otherwise, it will be used to update the corresponding row in the table
44
	 * (usually the case if the record is obtained using one of those 'find' methods.)
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
	 * If the record is saved via insertion, its {@link isNewRecord} property will be
51
	 * set false, and its {@link scenario} property will be set to be 'update'.
52
	 * And if its primary key is auto-incremental and is not set before insertion,
53
	 * the primary key will be populated with the automatically generated key value.
54
	 *
55
	 * @param boolean $runValidation whether to perform validation before saving the record.
56
	 * If the validation fails, the record will not be saved to database.
57
	 * @param array $attributes list of attributes that need to be saved. Defaults to null,
58
	 * meaning all attributes that are loaded from DB will be saved.
59
	 * @return boolean whether the saving succeeds
60
	 * @since v1.0
61
	 * @Ignored
62
	 */
63 21
	public function save($runValidation = true, $attributes = null)
64
	{
65 21
		return $this->_getEm()->save($runValidation, $attributes);
66
	}
67
68
	/**
69
	 * Inserts a row into the table based on this active record attributes.
70
	 * If the table's primary key is auto-incremental and is null before insertion,
71
	 * it will be populated with the actual value after insertion.
72
	 * Note, validation is not performed in this method. You may call {@link validate} to perform the validation.
73
	 * After the record is inserted to DB successfully, its {@link isNewRecord} property will be set false,
74
	 * and its {@link scenario} property will be set to be 'update'.
75
	 * @param AnnotatedInterface $model if want to insert different model than set in constructor
76
	 * @return boolean whether the attributes are valid and the record is inserted successfully.
77
	 * @throws MongoException if the record is not new
78
	 * @throws MongoException on fail of insert or insert of empty document
79
	 * @throws MongoException on fail of insert, when safe flag is set to true
80
	 * @throws MongoException on timeout of db operation , when safe flag is set to true
81
	 * @since v1.0
82
	 * @Ignored
83
	 */
84 3
	public function insert(AnnotatedInterface $model = null)
85
	{
86 3
		return $this->_getEm()->insert($model);
87
	}
88
89
	/**
90
	 * Updates the row represented by this active record.
91
	 * All loaded attributes will be saved to the database.
92
	 * Note, validation is not performed in this method. You may call {@link validate} to perform the validation.
93
	 * @param array $attributes list of attributes that need to be updated. Defaults to null,
94
	 * meaning all attributes that are loaded from DB will be saved.
95
	 * @return boolean whether the update is successful
96
	 * @throws MongoException if the record is new
97
	 * @throws MongoException on fail of update
98
	 * @throws MongoException on timeout of db operation , when safe flag is set to true
99
	 * @since v1.0
100
	 * @Ignored
101
	 */
102 2
	public function update(array $attributes = null)
103
	{
104 2
		return $this->_getEm()->update($attributes);
105
	}
106
107
	/**
108
	 * Updates one document with the specified criteria and attributes
109
	 *
110
	 * This is more *raw* update:
111
	 *
112
	 * * Does not raise any events or signals
113
	 * * Does not perform any validation
114
	 *
115
	 * @param array|CriteriaInterface $criteria query criteria.
116
	 * @param array $attributes list of attributes that need to be saved. Defaults to null,
117
	 * meaning all attributes that are loaded from DB will be saved.
118
	 * @since v1.0
119
	 */
120
	public function updateOne($criteria = null, $attributes = [])
121
	{
122
		return $this->_getEm()->updateOne($criteria, $attributes);
123
	}
124
125
	/**
126
	 * Atomic, in-place update method.
127
	 *
128
	 * @since v1.3.6
129
	 * @param Modifier $modifier updating rules to apply
130
	 * @param CriteriaInterface $criteria condition to limit updating rules
131
	 * @return boolean|mixed[]
132
	 * @Ignored
133
	 */
134
	public function updateAll(Modifier $modifier, CriteriaInterface $criteria = null)
135
	{
136
		return $this->_getEm()->updateAll($modifier, $criteria);
137
	}
138
139
	/**
140
	 * Deletes the row corresponding to this Document.
141
	 * @return boolean whether the deletion is successful.
142
	 * @throws MongoException if the record is new
143
	 * @since v1.0
144
	 * @Ignored
145
	 */
146 6
	public function delete()
147
	{
148 6
		return $this->_getEm()->delete();
149
	}
150
151
	/**
152
	 * Deletes document with the specified primary key.
153
	 * See {@link find()} for detailed explanation about $condition and $params.
154
	 * @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).
155
	 * @param array|CriteriaInterface $criteria query criteria.
156
	 * @since v1.0
157
	 * @Ignored
158
	 */
159 1
	public function deleteByPk($pkValue, $criteria = null)
160
	{
161 1
		return $this->_getEm()->deleteByPk($pkValue, $criteria);
162
	}
163
164
	/**
165
	 * Deletes documents with the specified primary keys.
166
	 * See {@link find()} for detailed explanation about $condition and $params.
167
	 * @param mixed[] $pkValues Primary keys array
168
	 * @param array|CriteriaInterface $criteria query criteria.
169
	 * @since v1.0
170
	 * @Ignored
171
	 */
172 1
	public function deleteAllByPk($pkValues, $criteria = null)
173
	{
174 1
		return $this->_getEm()->deleteAllByPk($pkValues, $criteria);
175
	}
176
177
	/**
178
	 * Deletes documents with the specified criteria.
179
	 * See {@link find()} for detailed explanation about $condition and $params.
180
	 * @param array|CriteriaInterface $criteria query criteria.
181
	 * @since v1.0
182
	 * @Ignored
183
	 */
184
	public function deleteAll($criteria = null)
185
	{
186
		return $this->_getEm()->deleteAll($criteria);
187
	}
188
189
	/**
190
	 * Deletes one document with the specified primary keys.
191
	 * <b>Does not raise beforeDelete</b>
192
	 * See {@link find()} for detailed explanation about $condition and $params.
193
	 * @param array|CriteriaInterface $criteria query criteria.
194
	 * @since v1.0
195
	 * @Ignored
196
	 */
197
	public function deleteOne($criteria = null)
198
	{
199
		return $this->_getEm()->deleteOne($criteria);
200
	}
201
202
	/**
203
	 * Repopulates this active record with the latest data.
204
	 * @return boolean whether the row still exists in the database. If true, the latest data will be populated to this active record.
205
	 * @since v1.0
206
	 * @Ignored
207
	 */
208
	public function refresh()
209
	{
210
		return $this->_getEm()->refresh();
211
	}
212
213
	/**
214
	 *
215
	 * @return MongoCollection
216
	 * @Ignored
217
	 */
218
	public function getCollection()
219
	{
220
		return $this->_getEm()->getCollection();
221
	}
222
223 26
	private function _getEm()
224
	{
225 26
		if (null === $this->_em)
226 26
		{
227 26
			$this->_em = new EntityManager($this);
228 26
		}
229 26
		return $this->_em;
230
	}
231
232
}
233