Completed
Push — master ( c7a6bb...41fefb )
by Peter
20:43
created

EntityManagerTrait::upsert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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