Maslosoft Mangan API
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 293 294 295 296 297 298 299
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan\Traits;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\EntityManager;
use Maslosoft\Mangan\Exceptions\ManganException;
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
use Maslosoft\Mangan\Interfaces\EntityManagerInterface;
use Maslosoft\Mangan\Modifier;
use MongoCollection;
/**
* This trait contains same methods as `EntityManagerInterface`, and it forwards
* them to concrete Entity Manager class. Entity manager used by this trait
* can be defined via EntityManager annotation.
*
* This is usefull to create Active Document
* pattern classes.
*
* @see EntityManagerInterface
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
trait EntityManagerTrait
{
/**
* Entity manager
* @var EntityManagerInterface|EntityManager
*/
private $_em = null;
/**
* Replaces the current document.
*
* **NOTE: This will overwrite entire document.**
*
* Any filtered out properties will be removed as well.
*
* The record is inserted as a documnent into the database collection, if exists it will be replaced.
*
* Validation will be performed before saving the record. If the validation fails,
* the record will not be saved. You can call `getErrors()` to retrieve the
* validation errors.
*
* @param boolean $runValidation whether to perform validation before saving the record.
* If the validation fails, the record will not be saved to database.
*
* @return boolean whether the saving succeeds
* @since v1.0
*/
public function replace($runValidation = true)
{
return $this->_getEm()->replace($runValidation);
}
/**
* Saves the current document.
*
* The document is inserted into collection if it is not already saved. The
* check whether to update or insert document is dony by primary key.
*
* Validation will be performed before saving the record. If the validation fails,
* the record will not be saved. You can call `getErrors()` to retrieve the
* validation errors.
*
* If the record is saved its scenario will be set to be 'update'.
* And if its primary key is of type of `MongoId`, it will be set after save.
*
* @param boolean $runValidation whether to perform validation before saving the record.
* If the validation fails, the record will not be saved to database.
*
* @return boolean whether the saving succeeds
* @since v1.0
* @Ignored
*/
public function save($runValidation = true)
{
return $this->_getEm()->save($runValidation);
}
/**
* Updates or inserts the current document. This will try to update existing fields.
* Will keep already stored data if present in document.
*
* If document does not exist, a new one will be inserted.
*
* @param boolean $runValidation
* @return boolean
* @throws ManganException
*/
public function upsert($runValidation = true)
{
return $this->_getEm()->upsert($runValidation);
}
/**
* Inserts a document into the collection based on this active document attributes.
*
* Note, validation is not performed in this method. You may call `validate()` to perform the validation.
*
* After the record is inserted to DB successfully, its scenario will be set to be 'update'.
*
* @param AnnotatedInterface $model if want to insert different model than set in constructor
* @return boolean whether the attributes are valid and the record is inserted successfully.
* @throws ManganException if the record is not new
* @throws ManganException on fail of insert or insert of empty document
* @throws ManganException on fail of insert, when safe flag is set to true
* @throws ManganException on timeout of db operation , when safe flag is set to true
* @since v1.0
* @Ignored
*/
public function insert(AnnotatedInterface $model = null)
{
return $this->_getEm()->insert($model);
}
/**
* Updates the document represented by this active document.
* All loaded attributes will be saved to the database.
* Note, validation is not performed in this method. You may call `validate()` to perform the validation.
*
* @param array $attributes list of attributes that need to be updated. Defaults to null,
* meaning all attributes that are loaded from DB will be saved.
* @return boolean whether the update is successful
* @throws ManganException if the record is new
* @throws ManganException on fail of update
* @throws ManganException on timeout of db operation , when safe flag is set to true
* @since v1.0
* @Ignored
*/
public function update(array $attributes = null)
{
return $this->_getEm()->update($attributes);
}
/**
* Updates one document with the specified criteria and attributes
*
* This is more *raw* update:
*
* * Does not raise any events or signals
* * Does not perform any validation
*
* @param array|CriteriaInterface $criteria query criteria.
* @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.
* @param bool Whether tu force update/upsert document
* @since v1.0
*/
public function updateOne($criteria = null, array $attributes = null, $modify = false)
{
return $this->_getEm()->updateOne($criteria, $attributes, $modify);
}
/**
* Atomic, in-place update method.
*
* @since v1.3.6
* @param Modifier $modifier updating rules to apply
* @param CriteriaInterface $criteria condition to limit updating rules
* @return boolean|mixed[]
* @Ignored
*/
public function updateAll(Modifier $modifier, CriteriaInterface $criteria = null)
{
return $this->_getEm()->updateAll($modifier, $criteria);
}
/**
* Deletes the databse document corresponding to this `Document`.
* @return boolean whether the deletion is successful.
* @throws ManganException if the record is new
* @since v1.0
* @Ignored
*/
public function delete()
{
return $this->_getEm()->delete();
}
/**
* Deletes document with the specified primary key.
*
* Additional `$criteria` can be used to filter out which document should be deleted.
*
* See `find()` for detailed explanation about `$criteria`.
*
* @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).
* @param array|CriteriaInterface $criteria query criteria.
* @since v1.0
* @Ignored
*/
public function deleteByPk($pkValue, $criteria = null)
{
return $this->_getEm()->deleteByPk($pkValue, $criteria);
}
/**
* Deletes documents with the specified primary keys.
*
* Additional `$criteria` can be used to filter out which documents should be deleted.
*
* See `find()` for detailed explanation about `$criteria`.
*
* @param mixed[] $pkValues Primary keys array
* @param array|CriteriaInterface $criteria query criteria.
* @since v1.0
* @Ignored
*/
public function deleteAllByPk($pkValues, $criteria = null)
{
return $this->_getEm()->deleteAllByPk($pkValues, $criteria);
}
/**
* Deletes documents with the specified criteria.
*
* Optional `$criteria` can be used to filter out which documents should be deleted.
*
* See `find()` for detailed explanation about $criteria.
*
* @param array|CriteriaInterface $criteria query criteria.
* @since v1.0
* @Ignored
*/
public function deleteAll($criteria = null)
{
return $this->_getEm()->deleteAll($criteria);
}
/**
* Deletes one document with the specified primary keys.
*
* Optional `$criteria` can be used to filter out which document should be deleted.
*
* **Does not raise `beforeDelete` event**
*
* See `find()` for detailed explanation about $criteria
* @param array|CriteriaInterface $criteria query criteria.
* @since v1.0
* @Ignored
*/
public function deleteOne($criteria = null)
{
return $this->_getEm()->deleteOne($criteria);
}
/**
* Repopulates this active document with the latest data.
*
* @return boolean whether the row still exists in the database. If true, the latest data will be populated to this active record.
* @since v1.0
* @Ignored
*/
public function refresh()
{
return $this->_getEm()->refresh();
}
/**
* Get working mongo collection instance.
*
* Should not be called manually in most cases.
*
* @return MongoCollection
* @Ignored
*/
public function getCollection()
{
return $this->_getEm()->getCollection();
}
/**
* Get entity manager instance
* @return EntityManagerInterface|EntityManager
*/
private function _getEm()
{
if (null === $this->_em)
{
$this->_em = EntityManager::create($this);
}
return $this->_em;
}
}
API documentation generated by ApiGen