@@ -7,28 +7,28 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | trait MagicSetters |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Dynamically set attributes on the entity. |
|
| 12 | - * |
|
| 13 | - * @param string $key |
|
| 14 | - * @param mixed $value |
|
| 15 | - * |
|
| 16 | - * @return void |
|
| 17 | - */ |
|
| 18 | - public function __set($key, $value) |
|
| 19 | - { |
|
| 20 | - $this->attributes[$key] = $value; |
|
| 21 | - } |
|
| 10 | + /** |
|
| 11 | + * Dynamically set attributes on the entity. |
|
| 12 | + * |
|
| 13 | + * @param string $key |
|
| 14 | + * @param mixed $value |
|
| 15 | + * |
|
| 16 | + * @return void |
|
| 17 | + */ |
|
| 18 | + public function __set($key, $value) |
|
| 19 | + { |
|
| 20 | + $this->attributes[$key] = $value; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Unset an attribute on the entity. |
|
| 25 | - * |
|
| 26 | - * @param string $key |
|
| 27 | - * |
|
| 28 | - * @return void |
|
| 29 | - */ |
|
| 30 | - public function __unset($key) |
|
| 31 | - { |
|
| 32 | - unset($this->attributes[$key]); |
|
| 33 | - } |
|
| 23 | + /** |
|
| 24 | + * Unset an attribute on the entity. |
|
| 25 | + * |
|
| 26 | + * @param string $key |
|
| 27 | + * |
|
| 28 | + * @return void |
|
| 29 | + */ |
|
| 30 | + public function __unset($key) |
|
| 31 | + { |
|
| 32 | + unset($this->attributes[$key]); |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -7,32 +7,32 @@ |
||
| 7 | 7 | |
| 8 | 8 | abstract class Command |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * The aggregated entity on which the command is executed. |
|
| 12 | - * |
|
| 13 | - * @var \Analogue\ORM\System\Aggregate |
|
| 14 | - */ |
|
| 15 | - protected $aggregate; |
|
| 10 | + /** |
|
| 11 | + * The aggregated entity on which the command is executed. |
|
| 12 | + * |
|
| 13 | + * @var \Analogue\ORM\System\Aggregate |
|
| 14 | + */ |
|
| 15 | + protected $aggregate; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Query Builder instance. |
|
| 19 | - * |
|
| 20 | - * @var \Illuminate\Database\Query\Builder |
|
| 21 | - */ |
|
| 22 | - protected $query; |
|
| 17 | + /** |
|
| 18 | + * Query Builder instance. |
|
| 19 | + * |
|
| 20 | + * @var \Illuminate\Database\Query\Builder |
|
| 21 | + */ |
|
| 22 | + protected $query; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Command constructor. |
|
| 26 | - * |
|
| 27 | - * @param Aggregate $aggregate |
|
| 28 | - * @param QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter $query |
|
| 29 | - */ |
|
| 30 | - public function __construct(Aggregate $aggregate, Builder $query) |
|
| 31 | - { |
|
| 32 | - $this->aggregate = $aggregate; |
|
| 24 | + /** |
|
| 25 | + * Command constructor. |
|
| 26 | + * |
|
| 27 | + * @param Aggregate $aggregate |
|
| 28 | + * @param QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter $query |
|
| 29 | + */ |
|
| 30 | + public function __construct(Aggregate $aggregate, Builder $query) |
|
| 31 | + { |
|
| 32 | + $this->aggregate = $aggregate; |
|
| 33 | 33 | |
| 34 | - $this->query = $query->from($aggregate->getEntityMap()->getTable()); |
|
| 35 | - } |
|
| 34 | + $this->query = $query->from($aggregate->getEntityMap()->getTable()); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - abstract public function execute(); |
|
| 37 | + abstract public function execute(); |
|
| 38 | 38 | } |
@@ -6,39 +6,39 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Delete extends Command |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Execute the Delete Statement. |
|
| 11 | - * |
|
| 12 | - * @throws MappingException |
|
| 13 | - * @throws \InvalidArgumentException |
|
| 14 | - * |
|
| 15 | - * @return false|void |
|
| 16 | - */ |
|
| 17 | - public function execute() |
|
| 18 | - { |
|
| 19 | - $aggregate = $this->aggregate; |
|
| 9 | + /** |
|
| 10 | + * Execute the Delete Statement. |
|
| 11 | + * |
|
| 12 | + * @throws MappingException |
|
| 13 | + * @throws \InvalidArgumentException |
|
| 14 | + * |
|
| 15 | + * @return false|void |
|
| 16 | + */ |
|
| 17 | + public function execute() |
|
| 18 | + { |
|
| 19 | + $aggregate = $this->aggregate; |
|
| 20 | 20 | |
| 21 | - $entity = $aggregate->getEntityObject(); |
|
| 21 | + $entity = $aggregate->getEntityObject(); |
|
| 22 | 22 | |
| 23 | - $mapper = $aggregate->getMapper(); |
|
| 23 | + $mapper = $aggregate->getMapper(); |
|
| 24 | 24 | |
| 25 | - if ($mapper->fireEvent('deleting', $entity) === false) { |
|
| 26 | - return false; |
|
| 27 | - } |
|
| 25 | + if ($mapper->fireEvent('deleting', $entity) === false) { |
|
| 26 | + return false; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - $keyName = $aggregate->getEntityMap()->getKeyName(); |
|
| 29 | + $keyName = $aggregate->getEntityMap()->getKeyName(); |
|
| 30 | 30 | |
| 31 | - $id = $this->aggregate->getEntityId(); |
|
| 31 | + $id = $this->aggregate->getEntityId(); |
|
| 32 | 32 | |
| 33 | - if (is_null($id)) { |
|
| 34 | - throw new MappingException('Executed a delete command on an entity with "null" as primary key'); |
|
| 35 | - } |
|
| 33 | + if (is_null($id)) { |
|
| 34 | + throw new MappingException('Executed a delete command on an entity with "null" as primary key'); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - $this->query->where($keyName, '=', $id)->delete(); |
|
| 37 | + $this->query->where($keyName, '=', $id)->delete(); |
|
| 38 | 38 | |
| 39 | - $mapper->fireEvent('deleted', $entity, false); |
|
| 39 | + $mapper->fireEvent('deleted', $entity, false); |
|
| 40 | 40 | |
| 41 | - // Once the Entity is successfully deleted, we'll just set the primary key to null. |
|
| 42 | - $aggregate->setEntityAttribute($keyName, null); |
|
| 43 | - } |
|
| 41 | + // Once the Entity is successfully deleted, we'll just set the primary key to null. |
|
| 42 | + $aggregate->setEntityAttribute($keyName, null); |
|
| 43 | + } |
|
| 44 | 44 | } |
@@ -14,247 +14,247 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Store extends Command |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Persist the entity in the database. |
|
| 19 | - * |
|
| 20 | - * @throws \InvalidArgumentException |
|
| 21 | - * |
|
| 22 | - * @return false|mixed |
|
| 23 | - */ |
|
| 24 | - public function execute() |
|
| 25 | - { |
|
| 26 | - $entity = $this->aggregate->getEntityObject(); |
|
| 27 | - |
|
| 28 | - $mapper = $this->aggregate->getMapper(); |
|
| 29 | - |
|
| 30 | - if ($mapper->fireEvent('storing', $entity) === false) { |
|
| 31 | - return false; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - $this->preStoreProcess(); |
|
| 35 | - |
|
| 36 | - /* |
|
| 17 | + /** |
|
| 18 | + * Persist the entity in the database. |
|
| 19 | + * |
|
| 20 | + * @throws \InvalidArgumentException |
|
| 21 | + * |
|
| 22 | + * @return false|mixed |
|
| 23 | + */ |
|
| 24 | + public function execute() |
|
| 25 | + { |
|
| 26 | + $entity = $this->aggregate->getEntityObject(); |
|
| 27 | + |
|
| 28 | + $mapper = $this->aggregate->getMapper(); |
|
| 29 | + |
|
| 30 | + if ($mapper->fireEvent('storing', $entity) === false) { |
|
| 31 | + return false; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + $this->preStoreProcess(); |
|
| 35 | + |
|
| 36 | + /* |
|
| 37 | 37 | * We will test the entity for existence |
| 38 | 38 | * and run a creation if it doesn't exists |
| 39 | 39 | */ |
| 40 | - if (!$this->aggregate->exists()) { |
|
| 41 | - if ($mapper->fireEvent('creating', $entity) === false) { |
|
| 42 | - return false; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - $this->insert(); |
|
| 46 | - |
|
| 47 | - $mapper->fireEvent('created', $entity, false); |
|
| 48 | - } elseif ($this->aggregate->isDirty()) { |
|
| 49 | - if ($mapper->fireEvent('updating', $entity) === false) { |
|
| 50 | - return false; |
|
| 51 | - } |
|
| 52 | - $this->update(); |
|
| 53 | - |
|
| 54 | - $mapper->fireEvent('updated', $entity, false); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - $this->postStoreProcess(); |
|
| 58 | - |
|
| 59 | - $mapper->fireEvent('stored', $entity, false); |
|
| 60 | - |
|
| 61 | - // Then we need to hydrate the actual entity |
|
| 62 | - $this->aggregate->hydrate(); |
|
| 63 | - |
|
| 64 | - return $entity; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Run all operations that have to occur before actually |
|
| 69 | - * storing the entity. |
|
| 70 | - * |
|
| 71 | - * @throws \InvalidArgumentException |
|
| 72 | - * |
|
| 73 | - * @return void |
|
| 74 | - */ |
|
| 75 | - protected function preStoreProcess() |
|
| 76 | - { |
|
| 77 | - // Create any related object that doesn't exist in the database. |
|
| 78 | - $localRelationships = $this->aggregate->getEntityMap()->getLocalRelationships(); |
|
| 79 | - |
|
| 80 | - $this->createRelatedEntities($localRelationships); |
|
| 81 | - |
|
| 82 | - // Now we can sync the related collections |
|
| 83 | - $this->aggregate->syncRelationships($localRelationships); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Check for existence and create non-existing related entities. |
|
| 88 | - * |
|
| 89 | - * @param array |
|
| 90 | - * |
|
| 91 | - * @throws \InvalidArgumentException |
|
| 92 | - * |
|
| 93 | - * @return void |
|
| 94 | - */ |
|
| 95 | - protected function createRelatedEntities($relations) |
|
| 96 | - { |
|
| 97 | - $entitiesToCreate = $this->aggregate->getNonExistingRelated($relations); |
|
| 98 | - |
|
| 99 | - foreach ($entitiesToCreate as $aggregate) { |
|
| 100 | - $this->createStoreCommand($aggregate)->execute(); |
|
| 101 | - } |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Create a new store command. |
|
| 106 | - * |
|
| 107 | - * @param Aggregate $aggregate |
|
| 108 | - * |
|
| 109 | - * @return Store |
|
| 110 | - */ |
|
| 111 | - protected function createStoreCommand(Aggregate $aggregate) |
|
| 112 | - { |
|
| 113 | - // We gotta retrieve the corresponding query adapter to use. |
|
| 114 | - $mapper = $aggregate->getMapper(); |
|
| 115 | - |
|
| 116 | - return new self($aggregate, $mapper->newQueryBuilder()); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Run all operations that have to occur after the entity |
|
| 121 | - * is stored. |
|
| 122 | - * |
|
| 123 | - * @throws \InvalidArgumentException |
|
| 124 | - * |
|
| 125 | - * @return void |
|
| 126 | - */ |
|
| 127 | - protected function postStoreProcess() |
|
| 128 | - { |
|
| 129 | - $aggregate = $this->aggregate; |
|
| 130 | - |
|
| 131 | - // Create any related object that doesn't exist in the database. |
|
| 132 | - $foreignRelationships = $aggregate->getEntityMap()->getForeignRelationships(); |
|
| 133 | - |
|
| 134 | - $this->createRelatedEntities($foreignRelationships); |
|
| 135 | - |
|
| 136 | - // Update any pivot tables that has been modified. |
|
| 137 | - $aggregate->updatePivotRecords(); |
|
| 138 | - |
|
| 139 | - // Update any dirty relationship. This include relationships that already exists, have |
|
| 140 | - // dirty attributes / newly created related entities / dirty related entities. |
|
| 141 | - $dirtyRelatedAggregates = $aggregate->getDirtyRelationships(); |
|
| 142 | - |
|
| 143 | - foreach ($dirtyRelatedAggregates as $related) { |
|
| 144 | - $this->createStoreCommand($related)->execute(); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - // Now we can sync the related collections |
|
| 148 | - // |
|
| 149 | - // TODO (note) : not sute this check is needed, as we can assume |
|
| 150 | - // the aggregate exists in the Post Store Process |
|
| 151 | - if ($this->aggregate->exists()) { |
|
| 152 | - $this->aggregate->syncRelationships($foreignRelationships); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - // TODO be move it to the wrapper class |
|
| 156 | - // so it's the same code for the entity builder |
|
| 157 | - $aggregate->setProxies(); |
|
| 158 | - |
|
| 159 | - // Update Entity Cache |
|
| 160 | - $aggregate->getMapper()->getEntityCache()->refresh($aggregate); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Update Related Entities which attributes have |
|
| 165 | - * been modified. |
|
| 166 | - * |
|
| 167 | - * @return void |
|
| 168 | - */ |
|
| 169 | - protected function updateDirtyRelated() |
|
| 170 | - { |
|
| 171 | - $relations = $this->entityMap->getRelationships(); |
|
| 172 | - $attributes = $this->getAttributes(); |
|
| 173 | - |
|
| 174 | - foreach ($relations as $relation) { |
|
| 175 | - if (!array_key_exists($relation, $attributes)) { |
|
| 176 | - continue; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - $value = $attributes[$relation]; |
|
| 180 | - |
|
| 181 | - if ($value == null) { |
|
| 182 | - continue; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - if ($value instanceof EntityProxy) { |
|
| 186 | - continue; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - if ($value instanceof CollectionProxy && $value->isLoaded()) { |
|
| 190 | - $value = $value->getUnderlyingCollection(); |
|
| 191 | - } |
|
| 192 | - if ($value instanceof CollectionProxy && !$value->isLoaded()) { |
|
| 193 | - foreach ($value->getAddedItems() as $entity) { |
|
| 194 | - $this->updateEntityIfDirty($entity); |
|
| 195 | - } |
|
| 196 | - continue; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - if ($value instanceof EntityCollection) { |
|
| 200 | - foreach ($value as $entity) { |
|
| 201 | - if (!$this->createEntityIfNotExists($entity)) { |
|
| 202 | - $this->updateEntityIfDirty($entity); |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - continue; |
|
| 206 | - } |
|
| 207 | - if ($value instanceof Mappable) { |
|
| 208 | - $this->updateEntityIfDirty($value); |
|
| 209 | - continue; |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Execute an insert statement on the database. |
|
| 216 | - * |
|
| 217 | - * @return void |
|
| 218 | - */ |
|
| 219 | - protected function insert() |
|
| 220 | - { |
|
| 221 | - $aggregate = $this->aggregate; |
|
| 222 | - |
|
| 223 | - $attributes = $aggregate->getRawAttributes(); |
|
| 224 | - |
|
| 225 | - $keyName = $aggregate->getEntityMap()->getKeyName(); |
|
| 226 | - |
|
| 227 | - // Check if the primary key is defined in the attributes |
|
| 228 | - if (array_key_exists($keyName, $attributes) && $attributes[$keyName] != null) { |
|
| 229 | - $this->query->insert($attributes); |
|
| 230 | - } else { |
|
| 231 | - $sequence = $aggregate->getEntityMap()->getSequence(); |
|
| 232 | - |
|
| 233 | - $id = $this->query->insertGetId($attributes, $sequence); |
|
| 234 | - |
|
| 235 | - $aggregate->setEntityAttribute($keyName, $id); |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Run an update statement on the entity. |
|
| 241 | - * |
|
| 242 | - * @throws \InvalidArgumentException |
|
| 243 | - * |
|
| 244 | - * @return void |
|
| 245 | - */ |
|
| 246 | - protected function update() |
|
| 247 | - { |
|
| 248 | - $query = $this->query; |
|
| 249 | - |
|
| 250 | - $keyName = $this->aggregate->getEntityKey(); |
|
| 251 | - |
|
| 252 | - $query = $query->where($keyName, '=', $this->aggregate->getEntityId()); |
|
| 253 | - |
|
| 254 | - $dirtyAttributes = $this->aggregate->getDirtyRawAttributes(); |
|
| 255 | - |
|
| 256 | - if (count($dirtyAttributes) > 0) { |
|
| 257 | - $query->update($dirtyAttributes); |
|
| 258 | - } |
|
| 259 | - } |
|
| 40 | + if (!$this->aggregate->exists()) { |
|
| 41 | + if ($mapper->fireEvent('creating', $entity) === false) { |
|
| 42 | + return false; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + $this->insert(); |
|
| 46 | + |
|
| 47 | + $mapper->fireEvent('created', $entity, false); |
|
| 48 | + } elseif ($this->aggregate->isDirty()) { |
|
| 49 | + if ($mapper->fireEvent('updating', $entity) === false) { |
|
| 50 | + return false; |
|
| 51 | + } |
|
| 52 | + $this->update(); |
|
| 53 | + |
|
| 54 | + $mapper->fireEvent('updated', $entity, false); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + $this->postStoreProcess(); |
|
| 58 | + |
|
| 59 | + $mapper->fireEvent('stored', $entity, false); |
|
| 60 | + |
|
| 61 | + // Then we need to hydrate the actual entity |
|
| 62 | + $this->aggregate->hydrate(); |
|
| 63 | + |
|
| 64 | + return $entity; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Run all operations that have to occur before actually |
|
| 69 | + * storing the entity. |
|
| 70 | + * |
|
| 71 | + * @throws \InvalidArgumentException |
|
| 72 | + * |
|
| 73 | + * @return void |
|
| 74 | + */ |
|
| 75 | + protected function preStoreProcess() |
|
| 76 | + { |
|
| 77 | + // Create any related object that doesn't exist in the database. |
|
| 78 | + $localRelationships = $this->aggregate->getEntityMap()->getLocalRelationships(); |
|
| 79 | + |
|
| 80 | + $this->createRelatedEntities($localRelationships); |
|
| 81 | + |
|
| 82 | + // Now we can sync the related collections |
|
| 83 | + $this->aggregate->syncRelationships($localRelationships); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Check for existence and create non-existing related entities. |
|
| 88 | + * |
|
| 89 | + * @param array |
|
| 90 | + * |
|
| 91 | + * @throws \InvalidArgumentException |
|
| 92 | + * |
|
| 93 | + * @return void |
|
| 94 | + */ |
|
| 95 | + protected function createRelatedEntities($relations) |
|
| 96 | + { |
|
| 97 | + $entitiesToCreate = $this->aggregate->getNonExistingRelated($relations); |
|
| 98 | + |
|
| 99 | + foreach ($entitiesToCreate as $aggregate) { |
|
| 100 | + $this->createStoreCommand($aggregate)->execute(); |
|
| 101 | + } |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Create a new store command. |
|
| 106 | + * |
|
| 107 | + * @param Aggregate $aggregate |
|
| 108 | + * |
|
| 109 | + * @return Store |
|
| 110 | + */ |
|
| 111 | + protected function createStoreCommand(Aggregate $aggregate) |
|
| 112 | + { |
|
| 113 | + // We gotta retrieve the corresponding query adapter to use. |
|
| 114 | + $mapper = $aggregate->getMapper(); |
|
| 115 | + |
|
| 116 | + return new self($aggregate, $mapper->newQueryBuilder()); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Run all operations that have to occur after the entity |
|
| 121 | + * is stored. |
|
| 122 | + * |
|
| 123 | + * @throws \InvalidArgumentException |
|
| 124 | + * |
|
| 125 | + * @return void |
|
| 126 | + */ |
|
| 127 | + protected function postStoreProcess() |
|
| 128 | + { |
|
| 129 | + $aggregate = $this->aggregate; |
|
| 130 | + |
|
| 131 | + // Create any related object that doesn't exist in the database. |
|
| 132 | + $foreignRelationships = $aggregate->getEntityMap()->getForeignRelationships(); |
|
| 133 | + |
|
| 134 | + $this->createRelatedEntities($foreignRelationships); |
|
| 135 | + |
|
| 136 | + // Update any pivot tables that has been modified. |
|
| 137 | + $aggregate->updatePivotRecords(); |
|
| 138 | + |
|
| 139 | + // Update any dirty relationship. This include relationships that already exists, have |
|
| 140 | + // dirty attributes / newly created related entities / dirty related entities. |
|
| 141 | + $dirtyRelatedAggregates = $aggregate->getDirtyRelationships(); |
|
| 142 | + |
|
| 143 | + foreach ($dirtyRelatedAggregates as $related) { |
|
| 144 | + $this->createStoreCommand($related)->execute(); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + // Now we can sync the related collections |
|
| 148 | + // |
|
| 149 | + // TODO (note) : not sute this check is needed, as we can assume |
|
| 150 | + // the aggregate exists in the Post Store Process |
|
| 151 | + if ($this->aggregate->exists()) { |
|
| 152 | + $this->aggregate->syncRelationships($foreignRelationships); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + // TODO be move it to the wrapper class |
|
| 156 | + // so it's the same code for the entity builder |
|
| 157 | + $aggregate->setProxies(); |
|
| 158 | + |
|
| 159 | + // Update Entity Cache |
|
| 160 | + $aggregate->getMapper()->getEntityCache()->refresh($aggregate); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Update Related Entities which attributes have |
|
| 165 | + * been modified. |
|
| 166 | + * |
|
| 167 | + * @return void |
|
| 168 | + */ |
|
| 169 | + protected function updateDirtyRelated() |
|
| 170 | + { |
|
| 171 | + $relations = $this->entityMap->getRelationships(); |
|
| 172 | + $attributes = $this->getAttributes(); |
|
| 173 | + |
|
| 174 | + foreach ($relations as $relation) { |
|
| 175 | + if (!array_key_exists($relation, $attributes)) { |
|
| 176 | + continue; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + $value = $attributes[$relation]; |
|
| 180 | + |
|
| 181 | + if ($value == null) { |
|
| 182 | + continue; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + if ($value instanceof EntityProxy) { |
|
| 186 | + continue; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + if ($value instanceof CollectionProxy && $value->isLoaded()) { |
|
| 190 | + $value = $value->getUnderlyingCollection(); |
|
| 191 | + } |
|
| 192 | + if ($value instanceof CollectionProxy && !$value->isLoaded()) { |
|
| 193 | + foreach ($value->getAddedItems() as $entity) { |
|
| 194 | + $this->updateEntityIfDirty($entity); |
|
| 195 | + } |
|
| 196 | + continue; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + if ($value instanceof EntityCollection) { |
|
| 200 | + foreach ($value as $entity) { |
|
| 201 | + if (!$this->createEntityIfNotExists($entity)) { |
|
| 202 | + $this->updateEntityIfDirty($entity); |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + continue; |
|
| 206 | + } |
|
| 207 | + if ($value instanceof Mappable) { |
|
| 208 | + $this->updateEntityIfDirty($value); |
|
| 209 | + continue; |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Execute an insert statement on the database. |
|
| 216 | + * |
|
| 217 | + * @return void |
|
| 218 | + */ |
|
| 219 | + protected function insert() |
|
| 220 | + { |
|
| 221 | + $aggregate = $this->aggregate; |
|
| 222 | + |
|
| 223 | + $attributes = $aggregate->getRawAttributes(); |
|
| 224 | + |
|
| 225 | + $keyName = $aggregate->getEntityMap()->getKeyName(); |
|
| 226 | + |
|
| 227 | + // Check if the primary key is defined in the attributes |
|
| 228 | + if (array_key_exists($keyName, $attributes) && $attributes[$keyName] != null) { |
|
| 229 | + $this->query->insert($attributes); |
|
| 230 | + } else { |
|
| 231 | + $sequence = $aggregate->getEntityMap()->getSequence(); |
|
| 232 | + |
|
| 233 | + $id = $this->query->insertGetId($attributes, $sequence); |
|
| 234 | + |
|
| 235 | + $aggregate->setEntityAttribute($keyName, $id); |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Run an update statement on the entity. |
|
| 241 | + * |
|
| 242 | + * @throws \InvalidArgumentException |
|
| 243 | + * |
|
| 244 | + * @return void |
|
| 245 | + */ |
|
| 246 | + protected function update() |
|
| 247 | + { |
|
| 248 | + $query = $this->query; |
|
| 249 | + |
|
| 250 | + $keyName = $this->aggregate->getEntityKey(); |
|
| 251 | + |
|
| 252 | + $query = $query->where($keyName, '=', $this->aggregate->getEntityId()); |
|
| 253 | + |
|
| 254 | + $dirtyAttributes = $this->aggregate->getDirtyRawAttributes(); |
|
| 255 | + |
|
| 256 | + if (count($dirtyAttributes) > 0) { |
|
| 257 | + $query->update($dirtyAttributes); |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | 260 | } |
@@ -4,54 +4,54 @@ |
||
| 4 | 4 | |
| 5 | 5 | trait MagicGetters |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Contains the entity's attributes. |
|
| 9 | - * |
|
| 10 | - * @var array |
|
| 11 | - */ |
|
| 12 | - protected $attributes = []; |
|
| 7 | + /** |
|
| 8 | + * Contains the entity's attributes. |
|
| 9 | + * |
|
| 10 | + * @var array |
|
| 11 | + */ |
|
| 12 | + protected $attributes = []; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Dynamically retrieve attributes on the entity. |
|
| 16 | - * |
|
| 17 | - * @param string $key |
|
| 18 | - * |
|
| 19 | - * @return mixed |
|
| 20 | - */ |
|
| 21 | - public function __get($key) |
|
| 22 | - { |
|
| 23 | - // When using mixed mapping, we will check |
|
| 24 | - // for a class property corresponding to |
|
| 25 | - // the attribute's key first. |
|
| 26 | - // |
|
| 27 | - // Note : this may raise issues as we may grant |
|
| 28 | - // access to unwanted properties, like class dependencies. |
|
| 29 | - // |
|
| 30 | - // -> Solution would be to access the entityMap's $attributes, but we |
|
| 31 | - // have to do this in a very efficient way. |
|
| 32 | - // |
|
| 33 | - // Manager::getEntityMap(get_class($this))->hasProperty() |
|
| 34 | - // |
|
| 35 | - // We could do the casting to array / json the same way, and it would |
|
| 14 | + /** |
|
| 15 | + * Dynamically retrieve attributes on the entity. |
|
| 16 | + * |
|
| 17 | + * @param string $key |
|
| 18 | + * |
|
| 19 | + * @return mixed |
|
| 20 | + */ |
|
| 21 | + public function __get($key) |
|
| 22 | + { |
|
| 23 | + // When using mixed mapping, we will check |
|
| 24 | + // for a class property corresponding to |
|
| 25 | + // the attribute's key first. |
|
| 26 | + // |
|
| 27 | + // Note : this may raise issues as we may grant |
|
| 28 | + // access to unwanted properties, like class dependencies. |
|
| 29 | + // |
|
| 30 | + // -> Solution would be to access the entityMap's $attributes, but we |
|
| 31 | + // have to do this in a very efficient way. |
|
| 32 | + // |
|
| 33 | + // Manager::getEntityMap(get_class($this))->hasProperty() |
|
| 34 | + // |
|
| 35 | + // We could do the casting to array / json the same way, and it would |
|
| 36 | 36 | |
| 37 | - if (property_exists($this, $key)) { |
|
| 38 | - return $this->$key; |
|
| 39 | - } |
|
| 37 | + if (property_exists($this, $key)) { |
|
| 38 | + return $this->$key; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - if (array_key_exists($key, $this->attributes)) { |
|
| 42 | - return $this->attributes[$key]; |
|
| 43 | - } |
|
| 44 | - } |
|
| 41 | + if (array_key_exists($key, $this->attributes)) { |
|
| 42 | + return $this->attributes[$key]; |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Determine if an attribute exists on the entity. |
|
| 48 | - * |
|
| 49 | - * @param string $key |
|
| 50 | - * |
|
| 51 | - * @return bool |
|
| 52 | - */ |
|
| 53 | - public function __isset($key) |
|
| 54 | - { |
|
| 55 | - return array_key_exists($key, $this->attributes) || property_exists($this, $key); |
|
| 56 | - } |
|
| 46 | + /** |
|
| 47 | + * Determine if an attribute exists on the entity. |
|
| 48 | + * |
|
| 49 | + * @param string $key |
|
| 50 | + * |
|
| 51 | + * @return bool |
|
| 52 | + */ |
|
| 53 | + public function __isset($key) |
|
| 54 | + { |
|
| 55 | + return array_key_exists($key, $this->attributes) || property_exists($this, $key); |
|
| 56 | + } |
|
| 57 | 57 | } |
@@ -13,58 +13,58 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class AnalogueServiceProvider extends ServiceProvider |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Indicates if loading of the provider is deferred. |
|
| 18 | - * |
|
| 19 | - * @var bool |
|
| 20 | - */ |
|
| 21 | - protected $defer = false; |
|
| 16 | + /** |
|
| 17 | + * Indicates if loading of the provider is deferred. |
|
| 18 | + * |
|
| 19 | + * @var bool |
|
| 20 | + */ |
|
| 21 | + protected $defer = false; |
|
| 22 | 22 | |
| 23 | - public function boot() |
|
| 24 | - { |
|
| 25 | - // |
|
| 26 | - } |
|
| 23 | + public function boot() |
|
| 24 | + { |
|
| 25 | + // |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Register the service provider. |
|
| 30 | - * |
|
| 31 | - * @return void |
|
| 32 | - */ |
|
| 33 | - public function register() |
|
| 34 | - { |
|
| 35 | - $this->app->singleton('analogue', function ($app) { |
|
| 36 | - $db = $app['db']; |
|
| 28 | + /** |
|
| 29 | + * Register the service provider. |
|
| 30 | + * |
|
| 31 | + * @return void |
|
| 32 | + */ |
|
| 33 | + public function register() |
|
| 34 | + { |
|
| 35 | + $this->app->singleton('analogue', function ($app) { |
|
| 36 | + $db = $app['db']; |
|
| 37 | 37 | |
| 38 | - $connectionProvider = new IlluminateConnectionProvider($db); |
|
| 38 | + $connectionProvider = new IlluminateConnectionProvider($db); |
|
| 39 | 39 | |
| 40 | - $illuminate = new IlluminateDriver($connectionProvider); |
|
| 40 | + $illuminate = new IlluminateDriver($connectionProvider); |
|
| 41 | 41 | |
| 42 | - $driverManager = new DriverManager(); |
|
| 42 | + $driverManager = new DriverManager(); |
|
| 43 | 43 | |
| 44 | - $driverManager->addDriver($illuminate); |
|
| 44 | + $driverManager->addDriver($illuminate); |
|
| 45 | 45 | |
| 46 | - $event = $app->make('events'); |
|
| 46 | + $event = $app->make('events'); |
|
| 47 | 47 | |
| 48 | - $manager = new Manager($driverManager, $event); |
|
| 48 | + $manager = new Manager($driverManager, $event); |
|
| 49 | 49 | |
| 50 | - $manager->registerPlugin(\Analogue\ORM\Plugins\Timestamps\TimestampsPlugin::class); |
|
| 51 | - $manager->registerPlugin(\Analogue\ORM\Plugins\SoftDeletes\SoftDeletesPlugin::class); |
|
| 50 | + $manager->registerPlugin(\Analogue\ORM\Plugins\Timestamps\TimestampsPlugin::class); |
|
| 51 | + $manager->registerPlugin(\Analogue\ORM\Plugins\SoftDeletes\SoftDeletesPlugin::class); |
|
| 52 | 52 | |
| 53 | - return $manager; |
|
| 54 | - }); |
|
| 53 | + return $manager; |
|
| 54 | + }); |
|
| 55 | 55 | |
| 56 | - $this->app->bind(Manager::class, function ($app) { |
|
| 57 | - return $app->make('analogue'); |
|
| 58 | - }); |
|
| 59 | - } |
|
| 56 | + $this->app->bind(Manager::class, function ($app) { |
|
| 57 | + return $app->make('analogue'); |
|
| 58 | + }); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Get the services provided by the provider. |
|
| 63 | - * |
|
| 64 | - * @return array |
|
| 65 | - */ |
|
| 66 | - public function provides() |
|
| 67 | - { |
|
| 68 | - return ['analogue']; |
|
| 69 | - } |
|
| 61 | + /** |
|
| 62 | + * Get the services provided by the provider. |
|
| 63 | + * |
|
| 64 | + * @return array |
|
| 65 | + */ |
|
| 66 | + public function provides() |
|
| 67 | + { |
|
| 68 | + return ['analogue']; |
|
| 69 | + } |
|
| 70 | 70 | } |
@@ -12,53 +12,53 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class TimestampsPlugin extends AnaloguePlugin |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Register the plugin. |
|
| 17 | - * |
|
| 18 | - * @throws \Exception |
|
| 19 | - * |
|
| 20 | - * @return void |
|
| 21 | - */ |
|
| 22 | - public function register() |
|
| 23 | - { |
|
| 24 | - $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) { |
|
| 25 | - $entityMap = $mapper->getEntityMap(); |
|
| 15 | + /** |
|
| 16 | + * Register the plugin. |
|
| 17 | + * |
|
| 18 | + * @throws \Exception |
|
| 19 | + * |
|
| 20 | + * @return void |
|
| 21 | + */ |
|
| 22 | + public function register() |
|
| 23 | + { |
|
| 24 | + $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) { |
|
| 25 | + $entityMap = $mapper->getEntityMap(); |
|
| 26 | 26 | |
| 27 | - if ($entityMap->usesTimestamps()) { |
|
| 28 | - $mapper->registerEvent('creating', function ($entity) use ($entityMap) { |
|
| 29 | - $factory = new Factory(); |
|
| 30 | - $wrappedEntity = $factory->make($entity); |
|
| 27 | + if ($entityMap->usesTimestamps()) { |
|
| 28 | + $mapper->registerEvent('creating', function ($entity) use ($entityMap) { |
|
| 29 | + $factory = new Factory(); |
|
| 30 | + $wrappedEntity = $factory->make($entity); |
|
| 31 | 31 | |
| 32 | - $createdAtField = $entityMap->getCreatedAtColumn(); |
|
| 33 | - $updatedAtField = $entityMap->getUpdatedAtColumn(); |
|
| 32 | + $createdAtField = $entityMap->getCreatedAtColumn(); |
|
| 33 | + $updatedAtField = $entityMap->getUpdatedAtColumn(); |
|
| 34 | 34 | |
| 35 | - $time = new Carbon(); |
|
| 35 | + $time = new Carbon(); |
|
| 36 | 36 | |
| 37 | - $wrappedEntity->setEntityAttribute($createdAtField, $time); |
|
| 38 | - $wrappedEntity->setEntityAttribute($updatedAtField, $time); |
|
| 39 | - }); |
|
| 37 | + $wrappedEntity->setEntityAttribute($createdAtField, $time); |
|
| 38 | + $wrappedEntity->setEntityAttribute($updatedAtField, $time); |
|
| 39 | + }); |
|
| 40 | 40 | |
| 41 | - $mapper->registerEvent('updating', function ($entity) use ($entityMap) { |
|
| 42 | - $factory = new Factory(); |
|
| 43 | - $wrappedEntity = $factory->make($entity); |
|
| 41 | + $mapper->registerEvent('updating', function ($entity) use ($entityMap) { |
|
| 42 | + $factory = new Factory(); |
|
| 43 | + $wrappedEntity = $factory->make($entity); |
|
| 44 | 44 | |
| 45 | - $updatedAtField = $entityMap->getUpdatedAtColumn(); |
|
| 45 | + $updatedAtField = $entityMap->getUpdatedAtColumn(); |
|
| 46 | 46 | |
| 47 | - $time = new Carbon(); |
|
| 47 | + $time = new Carbon(); |
|
| 48 | 48 | |
| 49 | - $wrappedEntity->setEntityAttribute($updatedAtField, $time); |
|
| 50 | - }); |
|
| 51 | - } |
|
| 52 | - }); |
|
| 53 | - } |
|
| 49 | + $wrappedEntity->setEntityAttribute($updatedAtField, $time); |
|
| 50 | + }); |
|
| 51 | + } |
|
| 52 | + }); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Get custom events provided by the plugin. |
|
| 57 | - * |
|
| 58 | - * @return array |
|
| 59 | - */ |
|
| 60 | - public function getCustomEvents() |
|
| 61 | - { |
|
| 62 | - return []; |
|
| 63 | - } |
|
| 55 | + /** |
|
| 56 | + * Get custom events provided by the plugin. |
|
| 57 | + * |
|
| 58 | + * @return array |
|
| 59 | + */ |
|
| 60 | + public function getCustomEvents() |
|
| 61 | + { |
|
| 62 | + return []; |
|
| 63 | + } |
|
| 64 | 64 | } |
@@ -4,17 +4,17 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface AnaloguePluginInterface |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Boot the plugin. |
|
| 9 | - * |
|
| 10 | - * @return void |
|
| 11 | - */ |
|
| 12 | - public function register(); |
|
| 7 | + /** |
|
| 8 | + * Boot the plugin. |
|
| 9 | + * |
|
| 10 | + * @return void |
|
| 11 | + */ |
|
| 12 | + public function register(); |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Get custom events provided by the plugin. |
|
| 16 | - * |
|
| 17 | - * @return array |
|
| 18 | - */ |
|
| 19 | - public function getCustomEvents(); |
|
| 14 | + /** |
|
| 15 | + * Get custom events provided by the plugin. |
|
| 16 | + * |
|
| 17 | + * @return array |
|
| 18 | + */ |
|
| 19 | + public function getCustomEvents(); |
|
| 20 | 20 | } |
@@ -6,34 +6,34 @@ |
||
| 6 | 6 | |
| 7 | 7 | abstract class AnaloguePlugin implements AnaloguePluginInterface |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Manager instance. |
|
| 11 | - * |
|
| 12 | - * @var Manager |
|
| 13 | - */ |
|
| 14 | - protected $manager; |
|
| 9 | + /** |
|
| 10 | + * Manager instance. |
|
| 11 | + * |
|
| 12 | + * @var Manager |
|
| 13 | + */ |
|
| 14 | + protected $manager; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * AnaloguePlugin constructor. |
|
| 18 | - * |
|
| 19 | - * @param Manager $manager |
|
| 20 | - */ |
|
| 21 | - public function __construct(Manager $manager) |
|
| 22 | - { |
|
| 23 | - $this->manager = $manager; |
|
| 24 | - } |
|
| 16 | + /** |
|
| 17 | + * AnaloguePlugin constructor. |
|
| 18 | + * |
|
| 19 | + * @param Manager $manager |
|
| 20 | + */ |
|
| 21 | + public function __construct(Manager $manager) |
|
| 22 | + { |
|
| 23 | + $this->manager = $manager; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Boot the plugin. |
|
| 28 | - * |
|
| 29 | - * @return void |
|
| 30 | - */ |
|
| 31 | - abstract public function register(); |
|
| 26 | + /** |
|
| 27 | + * Boot the plugin. |
|
| 28 | + * |
|
| 29 | + * @return void |
|
| 30 | + */ |
|
| 31 | + abstract public function register(); |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Get custom events provided by the plugin. |
|
| 35 | - * |
|
| 36 | - * @return array |
|
| 37 | - */ |
|
| 38 | - abstract public function getCustomEvents(); |
|
| 33 | + /** |
|
| 34 | + * Get custom events provided by the plugin. |
|
| 35 | + * |
|
| 36 | + * @return array |
|
| 37 | + */ |
|
| 38 | + abstract public function getCustomEvents(); |
|
| 39 | 39 | } |