@@ -6,36 +6,36 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Restore extends Command |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @throws \InvalidArgumentException |
|
| 11 | - * |
|
| 12 | - * @return false|mixed |
|
| 13 | - */ |
|
| 14 | - public function execute() |
|
| 15 | - { |
|
| 16 | - $aggregate = $this->aggregate; |
|
| 17 | - $entity = $aggregate->getEntityObject(); |
|
| 18 | - $mapper = $aggregate->getMapper(); |
|
| 19 | - $entityMap = $mapper->getEntityMap(); |
|
| 20 | - |
|
| 21 | - if ($mapper->fireEvent('restoring', $entity) === false) { |
|
| 22 | - return false; |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - $keyName = $entityMap->getKeyName(); |
|
| 9 | + /** |
|
| 10 | + * @throws \InvalidArgumentException |
|
| 11 | + * |
|
| 12 | + * @return false|mixed |
|
| 13 | + */ |
|
| 14 | + public function execute() |
|
| 15 | + { |
|
| 16 | + $aggregate = $this->aggregate; |
|
| 17 | + $entity = $aggregate->getEntityObject(); |
|
| 18 | + $mapper = $aggregate->getMapper(); |
|
| 19 | + $entityMap = $mapper->getEntityMap(); |
|
| 20 | + |
|
| 21 | + if ($mapper->fireEvent('restoring', $entity) === false) { |
|
| 22 | + return false; |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + $keyName = $entityMap->getKeyName(); |
|
| 26 | 26 | |
| 27 | - $query = $this->query->where($keyName, '=', $aggregate->getEntityAttribute($keyName)); |
|
| 27 | + $query = $this->query->where($keyName, '=', $aggregate->getEntityAttribute($keyName)); |
|
| 28 | 28 | |
| 29 | - $deletedAtColumn = $entityMap->getQualifiedDeletedAtColumn(); |
|
| 29 | + $deletedAtColumn = $entityMap->getQualifiedDeletedAtColumn(); |
|
| 30 | 30 | |
| 31 | - $query->update([$deletedAtColumn => null]); |
|
| 31 | + $query->update([$deletedAtColumn => null]); |
|
| 32 | 32 | |
| 33 | - $aggregate->setEntityAttribute($deletedAtColumn, null); |
|
| 33 | + $aggregate->setEntityAttribute($deletedAtColumn, null); |
|
| 34 | 34 | |
| 35 | - $mapper->fireEvent('restored', $entity, false); |
|
| 35 | + $mapper->fireEvent('restored', $entity, false); |
|
| 36 | 36 | |
| 37 | - $mapper->getEntityCache()->refresh($aggregate); |
|
| 37 | + $mapper->getEntityCache()->refresh($aggregate); |
|
| 38 | 38 | |
| 39 | - return $entity; |
|
| 40 | - } |
|
| 39 | + return $entity; |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -13,80 +13,80 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class SoftDeletesPlugin extends AnaloguePlugin |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Register the plugin |
|
| 18 | - * |
|
| 19 | - * @throws \Exception |
|
| 20 | - * @return void |
|
| 21 | - */ |
|
| 22 | - public function register() |
|
| 23 | - { |
|
| 24 | - $host = $this; |
|
| 25 | - |
|
| 26 | - // Hook any mapper init and check the mapping include soft deletes. |
|
| 27 | - $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) use ($host) { |
|
| 28 | - $entityMap = $mapper->getEntityMap(); |
|
| 29 | - |
|
| 30 | - if ($entityMap->usesSoftDeletes()) { |
|
| 31 | - $host->registerSoftDelete($mapper); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - }); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * By hooking to the mapper initialization event, we can extend it |
|
| 39 | - * with the softDelete capacity. |
|
| 40 | - * |
|
| 41 | - * @param \Analogue\ORM\System\Mapper $mapper |
|
| 42 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 43 | - * @return bool|void |
|
| 44 | - */ |
|
| 45 | - protected function registerSoftDelete(Mapper $mapper) |
|
| 46 | - { |
|
| 47 | - $entityMap = $mapper->getEntityMap(); |
|
| 48 | - |
|
| 49 | - // Add Scopes |
|
| 50 | - $mapper->addGlobalScope(new SoftDeletingScope); |
|
| 51 | - |
|
| 52 | - $host = $this; |
|
| 53 | - |
|
| 54 | - // Register 'deleting' events |
|
| 55 | - $mapper->registerEvent('deleting', function ($entity) use ($entityMap, $host) { |
|
| 16 | + /** |
|
| 17 | + * Register the plugin |
|
| 18 | + * |
|
| 19 | + * @throws \Exception |
|
| 20 | + * @return void |
|
| 21 | + */ |
|
| 22 | + public function register() |
|
| 23 | + { |
|
| 24 | + $host = $this; |
|
| 25 | + |
|
| 26 | + // Hook any mapper init and check the mapping include soft deletes. |
|
| 27 | + $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) use ($host) { |
|
| 28 | + $entityMap = $mapper->getEntityMap(); |
|
| 29 | + |
|
| 30 | + if ($entityMap->usesSoftDeletes()) { |
|
| 31 | + $host->registerSoftDelete($mapper); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + }); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * By hooking to the mapper initialization event, we can extend it |
|
| 39 | + * with the softDelete capacity. |
|
| 40 | + * |
|
| 41 | + * @param \Analogue\ORM\System\Mapper $mapper |
|
| 42 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 43 | + * @return bool|void |
|
| 44 | + */ |
|
| 45 | + protected function registerSoftDelete(Mapper $mapper) |
|
| 46 | + { |
|
| 47 | + $entityMap = $mapper->getEntityMap(); |
|
| 48 | + |
|
| 49 | + // Add Scopes |
|
| 50 | + $mapper->addGlobalScope(new SoftDeletingScope); |
|
| 51 | + |
|
| 52 | + $host = $this; |
|
| 53 | + |
|
| 54 | + // Register 'deleting' events |
|
| 55 | + $mapper->registerEvent('deleting', function ($entity) use ($entityMap, $host) { |
|
| 56 | 56 | |
| 57 | - // Convert Entity into an EntityWrapper |
|
| 58 | - $factory = new Factory; |
|
| 57 | + // Convert Entity into an EntityWrapper |
|
| 58 | + $factory = new Factory; |
|
| 59 | 59 | |
| 60 | - $wrappedEntity = $factory->make($entity); |
|
| 60 | + $wrappedEntity = $factory->make($entity); |
|
| 61 | 61 | |
| 62 | - $deletedAtField = $entityMap->getQualifiedDeletedAtColumn(); |
|
| 62 | + $deletedAtField = $entityMap->getQualifiedDeletedAtColumn(); |
|
| 63 | 63 | |
| 64 | - if (!is_null($wrappedEntity->getEntityAttribute($deletedAtField))) { |
|
| 65 | - return true; |
|
| 66 | - } else { |
|
| 67 | - $time = new Carbon; |
|
| 64 | + if (!is_null($wrappedEntity->getEntityAttribute($deletedAtField))) { |
|
| 65 | + return true; |
|
| 66 | + } else { |
|
| 67 | + $time = new Carbon; |
|
| 68 | 68 | |
| 69 | - $wrappedEntity->setEntityAttribute($deletedAtField, $time); |
|
| 69 | + $wrappedEntity->setEntityAttribute($deletedAtField, $time); |
|
| 70 | 70 | |
| 71 | - $plainObject = $wrappedEntity->getObject(); |
|
| 72 | - $host->manager->mapper(get_class($plainObject))->store($plainObject); |
|
| 71 | + $plainObject = $wrappedEntity->getObject(); |
|
| 72 | + $host->manager->mapper(get_class($plainObject))->store($plainObject); |
|
| 73 | 73 | |
| 74 | - return false; |
|
| 75 | - } |
|
| 74 | + return false; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - }); |
|
| 77 | + }); |
|
| 78 | 78 | |
| 79 | - // Register RestoreCommand |
|
| 80 | - $mapper->addCustomCommand('Analogue\ORM\Plugins\SoftDeletes\Restore'); |
|
| 81 | - } |
|
| 79 | + // Register RestoreCommand |
|
| 80 | + $mapper->addCustomCommand('Analogue\ORM\Plugins\SoftDeletes\Restore'); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Get custom events provided by the plugin |
|
| 85 | - * |
|
| 86 | - * @return string[] |
|
| 87 | - */ |
|
| 88 | - public function getCustomEvents() |
|
| 89 | - { |
|
| 90 | - return ['restoring', 'restored']; |
|
| 91 | - } |
|
| 83 | + /** |
|
| 84 | + * Get custom events provided by the plugin |
|
| 85 | + * |
|
| 86 | + * @return string[] |
|
| 87 | + */ |
|
| 88 | + public function getCustomEvents() |
|
| 89 | + { |
|
| 90 | + return ['restoring', 'restored']; |
|
| 91 | + } |
|
| 92 | 92 | } |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | $host = $this; |
| 25 | 25 | |
| 26 | 26 | // Hook any mapper init and check the mapping include soft deletes. |
| 27 | - $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) use ($host) { |
|
| 27 | + $this->manager->registerGlobalEvent('initialized', function(Mapper $mapper) use ($host) { |
|
| 28 | 28 | $entityMap = $mapper->getEntityMap(); |
| 29 | 29 | |
| 30 | 30 | if ($entityMap->usesSoftDeletes()) { |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | $host = $this; |
| 53 | 53 | |
| 54 | 54 | // Register 'deleting' events |
| 55 | - $mapper->registerEvent('deleting', function ($entity) use ($entityMap, $host) { |
|
| 55 | + $mapper->registerEvent('deleting', function($entity) use ($entityMap, $host) { |
|
| 56 | 56 | |
| 57 | 57 | // Convert Entity into an EntityWrapper |
| 58 | 58 | $factory = new Factory; |
@@ -12,55 +12,55 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class TimestampsPlugin extends AnaloguePlugin |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Register the plugin |
|
| 17 | - * |
|
| 18 | - * @throws \Exception |
|
| 19 | - * @return void |
|
| 20 | - */ |
|
| 21 | - public function register() |
|
| 22 | - { |
|
| 23 | - $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) { |
|
| 24 | - $entityMap = $mapper->getEntityMap(); |
|
| 15 | + /** |
|
| 16 | + * Register the plugin |
|
| 17 | + * |
|
| 18 | + * @throws \Exception |
|
| 19 | + * @return void |
|
| 20 | + */ |
|
| 21 | + public function register() |
|
| 22 | + { |
|
| 23 | + $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) { |
|
| 24 | + $entityMap = $mapper->getEntityMap(); |
|
| 25 | 25 | |
| 26 | - if ($entityMap->usesTimestamps()) { |
|
| 27 | - $mapper->registerEvent('creating', function ($entity) use ($entityMap) { |
|
| 26 | + if ($entityMap->usesTimestamps()) { |
|
| 27 | + $mapper->registerEvent('creating', function ($entity) use ($entityMap) { |
|
| 28 | 28 | |
| 29 | - $factory = new Factory; |
|
| 30 | - $wrappedEntity = $factory->make($entity); |
|
| 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); |
|
| 37 | + $wrappedEntity->setEntityAttribute($createdAtField, $time); |
|
| 38 | + $wrappedEntity->setEntityAttribute($updatedAtField, $time); |
|
| 39 | 39 | |
| 40 | - }); |
|
| 40 | + }); |
|
| 41 | 41 | |
| 42 | - $mapper->registerEvent('updating', function ($entity) use ($entityMap) { |
|
| 42 | + $mapper->registerEvent('updating', function ($entity) use ($entityMap) { |
|
| 43 | 43 | |
| 44 | - $factory = new Factory; |
|
| 45 | - $wrappedEntity = $factory->make($entity); |
|
| 44 | + $factory = new Factory; |
|
| 45 | + $wrappedEntity = $factory->make($entity); |
|
| 46 | 46 | |
| 47 | - $updatedAtField = $entityMap->getUpdatedAtColumn(); |
|
| 47 | + $updatedAtField = $entityMap->getUpdatedAtColumn(); |
|
| 48 | 48 | |
| 49 | - $time = new Carbon; |
|
| 49 | + $time = new Carbon; |
|
| 50 | 50 | |
| 51 | - $wrappedEntity->setEntityAttribute($updatedAtField, $time); |
|
| 52 | - }); |
|
| 53 | - } |
|
| 54 | - }); |
|
| 55 | - } |
|
| 51 | + $wrappedEntity->setEntityAttribute($updatedAtField, $time); |
|
| 52 | + }); |
|
| 53 | + } |
|
| 54 | + }); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Get custom events provided by the plugin |
|
| 59 | - * |
|
| 60 | - * @return array |
|
| 61 | - */ |
|
| 62 | - public function getCustomEvents() |
|
| 63 | - { |
|
| 64 | - return []; |
|
| 65 | - } |
|
| 57 | + /** |
|
| 58 | + * Get custom events provided by the plugin |
|
| 59 | + * |
|
| 60 | + * @return array |
|
| 61 | + */ |
|
| 62 | + public function getCustomEvents() |
|
| 63 | + { |
|
| 64 | + return []; |
|
| 65 | + } |
|
| 66 | 66 | } |
@@ -20,11 +20,11 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | public function register() |
| 22 | 22 | { |
| 23 | - $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) { |
|
| 23 | + $this->manager->registerGlobalEvent('initialized', function(Mapper $mapper) { |
|
| 24 | 24 | $entityMap = $mapper->getEntityMap(); |
| 25 | 25 | |
| 26 | 26 | if ($entityMap->usesTimestamps()) { |
| 27 | - $mapper->registerEvent('creating', function ($entity) use ($entityMap) { |
|
| 27 | + $mapper->registerEvent('creating', function($entity) use ($entityMap) { |
|
| 28 | 28 | |
| 29 | 29 | $factory = new Factory; |
| 30 | 30 | $wrappedEntity = $factory->make($entity); |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | |
| 40 | 40 | }); |
| 41 | 41 | |
| 42 | - $mapper->registerEvent('updating', function ($entity) use ($entityMap) { |
|
| 42 | + $mapper->registerEvent('updating', function($entity) use ($entityMap) { |
|
| 43 | 43 | |
| 44 | 44 | $factory = new Factory; |
| 45 | 45 | $wrappedEntity = $factory->make($entity); |
@@ -6,33 +6,33 @@ |
||
| 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 | - * @param Manager $manager |
|
| 19 | - */ |
|
| 20 | - public function __construct(Manager $manager) |
|
| 21 | - { |
|
| 22 | - $this->manager = $manager; |
|
| 23 | - } |
|
| 16 | + /** |
|
| 17 | + * AnaloguePlugin constructor. |
|
| 18 | + * @param Manager $manager |
|
| 19 | + */ |
|
| 20 | + public function __construct(Manager $manager) |
|
| 21 | + { |
|
| 22 | + $this->manager = $manager; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Boot the plugin |
|
| 27 | - * |
|
| 28 | - * @return void |
|
| 29 | - */ |
|
| 30 | - abstract public function register(); |
|
| 25 | + /** |
|
| 26 | + * Boot the plugin |
|
| 27 | + * |
|
| 28 | + * @return void |
|
| 29 | + */ |
|
| 30 | + abstract public function register(); |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Get custom events provided by the plugin |
|
| 34 | - * |
|
| 35 | - * @return array |
|
| 36 | - */ |
|
| 37 | - abstract public function getCustomEvents(); |
|
| 32 | + /** |
|
| 33 | + * Get custom events provided by the plugin |
|
| 34 | + * |
|
| 35 | + * @return array |
|
| 36 | + */ |
|
| 37 | + abstract public function getCustomEvents(); |
|
| 38 | 38 | } |
@@ -15,129 +15,129 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Repository |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * The mapper object for the corresponding entity |
|
| 20 | - * |
|
| 21 | - * @var \Analogue\ORM\System\Mapper |
|
| 22 | - */ |
|
| 23 | - protected $mapper; |
|
| 18 | + /** |
|
| 19 | + * The mapper object for the corresponding entity |
|
| 20 | + * |
|
| 21 | + * @var \Analogue\ORM\System\Mapper |
|
| 22 | + */ |
|
| 23 | + protected $mapper; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * To build a repository, either provide : |
|
| 27 | - * |
|
| 28 | - * - Mappable object's class name as a string |
|
| 29 | - * - Mappable object instance |
|
| 30 | - * - Instance of mapper |
|
| 31 | - * |
|
| 32 | - * @param Mapper $mapper |
|
| 33 | - * @param EntityMap|null $entityMap (optional) |
|
| 34 | - * @throws \InvalidArgumentException |
|
| 35 | - * @throws MappingException |
|
| 36 | - */ |
|
| 37 | - public function __construct($mapper, EntityMap $entityMap = null) |
|
| 38 | - { |
|
| 39 | - if ($mapper instanceof Mappable || is_string($mapper)) { |
|
| 40 | - $this->mapper = Manager::getMapper($mapper, $entityMap); |
|
| 41 | - } elseif ($mapper instanceof Mapper) { |
|
| 42 | - $this->mapper = $mapper; |
|
| 43 | - } else { |
|
| 44 | - new InvalidArgumentException('Repository class constructor need a valid Mapper or Mappable object.'); |
|
| 45 | - } |
|
| 46 | - } |
|
| 25 | + /** |
|
| 26 | + * To build a repository, either provide : |
|
| 27 | + * |
|
| 28 | + * - Mappable object's class name as a string |
|
| 29 | + * - Mappable object instance |
|
| 30 | + * - Instance of mapper |
|
| 31 | + * |
|
| 32 | + * @param Mapper $mapper |
|
| 33 | + * @param EntityMap|null $entityMap (optional) |
|
| 34 | + * @throws \InvalidArgumentException |
|
| 35 | + * @throws MappingException |
|
| 36 | + */ |
|
| 37 | + public function __construct($mapper, EntityMap $entityMap = null) |
|
| 38 | + { |
|
| 39 | + if ($mapper instanceof Mappable || is_string($mapper)) { |
|
| 40 | + $this->mapper = Manager::getMapper($mapper, $entityMap); |
|
| 41 | + } elseif ($mapper instanceof Mapper) { |
|
| 42 | + $this->mapper = $mapper; |
|
| 43 | + } else { |
|
| 44 | + new InvalidArgumentException('Repository class constructor need a valid Mapper or Mappable object.'); |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Return all Entities from database |
|
| 50 | - * |
|
| 51 | - * @return \Analogue\ORM\EntityCollection |
|
| 52 | - */ |
|
| 53 | - public function all() |
|
| 54 | - { |
|
| 55 | - return $this->mapper->get(); |
|
| 56 | - } |
|
| 48 | + /** |
|
| 49 | + * Return all Entities from database |
|
| 50 | + * |
|
| 51 | + * @return \Analogue\ORM\EntityCollection |
|
| 52 | + */ |
|
| 53 | + public function all() |
|
| 54 | + { |
|
| 55 | + return $this->mapper->get(); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Fetch a record from the database |
|
| 60 | - * @param integer $id |
|
| 61 | - * @return \Analogue\ORM\Mappable |
|
| 62 | - */ |
|
| 63 | - public function find($id) |
|
| 64 | - { |
|
| 65 | - return $this->mapper->find($id); |
|
| 66 | - } |
|
| 58 | + /** |
|
| 59 | + * Fetch a record from the database |
|
| 60 | + * @param integer $id |
|
| 61 | + * @return \Analogue\ORM\Mappable |
|
| 62 | + */ |
|
| 63 | + public function find($id) |
|
| 64 | + { |
|
| 65 | + return $this->mapper->find($id); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Get the first entity matching the given attributes. |
|
| 70 | - * |
|
| 71 | - * @param array $attributes |
|
| 72 | - * @return \Analogue\ORM\Mappable|null |
|
| 73 | - */ |
|
| 74 | - public function firstMatching(array $attributes) |
|
| 75 | - { |
|
| 76 | - return $this->mapper->where($attributes)->first(); |
|
| 77 | - } |
|
| 68 | + /** |
|
| 69 | + * Get the first entity matching the given attributes. |
|
| 70 | + * |
|
| 71 | + * @param array $attributes |
|
| 72 | + * @return \Analogue\ORM\Mappable|null |
|
| 73 | + */ |
|
| 74 | + public function firstMatching(array $attributes) |
|
| 75 | + { |
|
| 76 | + return $this->mapper->where($attributes)->first(); |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Return all the entities matching the given attributes |
|
| 81 | - * |
|
| 82 | - * @param array $attributes |
|
| 83 | - * @return \Analogue\ORM\EntityCollection |
|
| 84 | - */ |
|
| 85 | - public function allMatching(array $attributes) |
|
| 86 | - { |
|
| 87 | - return $this->mapper->where($attributes)->get(); |
|
| 88 | - } |
|
| 79 | + /** |
|
| 80 | + * Return all the entities matching the given attributes |
|
| 81 | + * |
|
| 82 | + * @param array $attributes |
|
| 83 | + * @return \Analogue\ORM\EntityCollection |
|
| 84 | + */ |
|
| 85 | + public function allMatching(array $attributes) |
|
| 86 | + { |
|
| 87 | + return $this->mapper->where($attributes)->get(); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Return a paginator instance on the EntityCollection |
|
| 92 | - * |
|
| 93 | - * @param int|null $perPage number of item per page (fallback on default setup in entity map) |
|
| 94 | - * @return \Illuminate\Pagination\LengthAwarePaginator |
|
| 95 | - */ |
|
| 96 | - public function paginate($perPage = null) |
|
| 97 | - { |
|
| 98 | - return $this->mapper->paginate($perPage); |
|
| 99 | - } |
|
| 90 | + /** |
|
| 91 | + * Return a paginator instance on the EntityCollection |
|
| 92 | + * |
|
| 93 | + * @param int|null $perPage number of item per page (fallback on default setup in entity map) |
|
| 94 | + * @return \Illuminate\Pagination\LengthAwarePaginator |
|
| 95 | + */ |
|
| 96 | + public function paginate($perPage = null) |
|
| 97 | + { |
|
| 98 | + return $this->mapper->paginate($perPage); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * Delete an entity or an entity collection from the database |
|
| 103 | - * |
|
| 104 | - * @param Mappable|EntityCollection $entity |
|
| 105 | - * @throws MappingException |
|
| 106 | - * @throws \InvalidArgumentException |
|
| 107 | - * @return \Illuminate\Support\Collection|null |
|
| 108 | - */ |
|
| 109 | - public function delete($entity) |
|
| 110 | - { |
|
| 111 | - return $this->mapper->delete($entity); |
|
| 112 | - } |
|
| 101 | + /** |
|
| 102 | + * Delete an entity or an entity collection from the database |
|
| 103 | + * |
|
| 104 | + * @param Mappable|EntityCollection $entity |
|
| 105 | + * @throws MappingException |
|
| 106 | + * @throws \InvalidArgumentException |
|
| 107 | + * @return \Illuminate\Support\Collection|null |
|
| 108 | + */ |
|
| 109 | + public function delete($entity) |
|
| 110 | + { |
|
| 111 | + return $this->mapper->delete($entity); |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Persist an entity or an entity collection in the database. |
|
| 116 | - * |
|
| 117 | - * @param Mappable|EntityCollection|array $entity |
|
| 118 | - * @throws MappingException |
|
| 119 | - * @throws \InvalidArgumentException |
|
| 120 | - * @return Mappable|EntityCollection|array |
|
| 121 | - */ |
|
| 122 | - public function store($entity) |
|
| 123 | - { |
|
| 124 | - return $this->mapper->store($entity); |
|
| 125 | - } |
|
| 114 | + /** |
|
| 115 | + * Persist an entity or an entity collection in the database. |
|
| 116 | + * |
|
| 117 | + * @param Mappable|EntityCollection|array $entity |
|
| 118 | + * @throws MappingException |
|
| 119 | + * @throws \InvalidArgumentException |
|
| 120 | + * @return Mappable|EntityCollection|array |
|
| 121 | + */ |
|
| 122 | + public function store($entity) |
|
| 123 | + { |
|
| 124 | + return $this->mapper->store($entity); |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * Make custom mapper custom commands available in repository |
|
| 129 | - * |
|
| 130 | - * @param string $method |
|
| 131 | - * @param array $parameters |
|
| 132 | - * @throws Exception |
|
| 133 | - * @return mixed |
|
| 134 | - */ |
|
| 135 | - public function __call($method, $parameters) |
|
| 136 | - { |
|
| 137 | - if ($this->mapper->hasCustomCommand($method)) { |
|
| 138 | - call_user_func_array([$this->mapper, $method], $parameters); |
|
| 139 | - } else { |
|
| 140 | - throw new Exception("No method $method on " . get_class($this)); |
|
| 141 | - } |
|
| 142 | - } |
|
| 127 | + /** |
|
| 128 | + * Make custom mapper custom commands available in repository |
|
| 129 | + * |
|
| 130 | + * @param string $method |
|
| 131 | + * @param array $parameters |
|
| 132 | + * @throws Exception |
|
| 133 | + * @return mixed |
|
| 134 | + */ |
|
| 135 | + public function __call($method, $parameters) |
|
| 136 | + { |
|
| 137 | + if ($this->mapper->hasCustomCommand($method)) { |
|
| 138 | + call_user_func_array([$this->mapper, $method], $parameters); |
|
| 139 | + } else { |
|
| 140 | + throw new Exception("No method $method on " . get_class($this)); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | 143 | } |
@@ -137,7 +137,7 @@ |
||
| 137 | 137 | if ($this->mapper->hasCustomCommand($method)) { |
| 138 | 138 | call_user_func_array([$this->mapper, $method], $parameters); |
| 139 | 139 | } else { |
| 140 | - throw new Exception("No method $method on " . get_class($this)); |
|
| 140 | + throw new Exception("No method $method on ".get_class($this)); |
|
| 141 | 141 | } |
| 142 | 142 | } |
| 143 | 143 | } |
@@ -8,63 +8,63 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | trait MappableTrait |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * The Entity's Attributes |
|
| 13 | - * @var array |
|
| 14 | - */ |
|
| 15 | - protected $attributes = []; |
|
| 11 | + /** |
|
| 12 | + * The Entity's Attributes |
|
| 13 | + * @var array |
|
| 14 | + */ |
|
| 15 | + protected $attributes = []; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Method used by the mapper to set the object |
|
| 19 | - * attribute raw values (hydration) |
|
| 20 | - * |
|
| 21 | - * @param array $attributes |
|
| 22 | - * |
|
| 23 | - * @return void |
|
| 24 | - */ |
|
| 25 | - public function setEntityAttributes(array $attributes) |
|
| 26 | - { |
|
| 27 | - $this->attributes = $attributes; |
|
| 28 | - } |
|
| 17 | + /** |
|
| 18 | + * Method used by the mapper to set the object |
|
| 19 | + * attribute raw values (hydration) |
|
| 20 | + * |
|
| 21 | + * @param array $attributes |
|
| 22 | + * |
|
| 23 | + * @return void |
|
| 24 | + */ |
|
| 25 | + public function setEntityAttributes(array $attributes) |
|
| 26 | + { |
|
| 27 | + $this->attributes = $attributes; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Method used by the mapper to get the |
|
| 32 | - * raw object's values. |
|
| 33 | - * |
|
| 34 | - * @return array |
|
| 35 | - */ |
|
| 36 | - public function getEntityAttributes() |
|
| 37 | - { |
|
| 38 | - return $this->attributes; |
|
| 39 | - } |
|
| 30 | + /** |
|
| 31 | + * Method used by the mapper to get the |
|
| 32 | + * raw object's values. |
|
| 33 | + * |
|
| 34 | + * @return array |
|
| 35 | + */ |
|
| 36 | + public function getEntityAttributes() |
|
| 37 | + { |
|
| 38 | + return $this->attributes; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Method used by the mapper to set raw |
|
| 43 | - * key-value pair |
|
| 44 | - * |
|
| 45 | - * @param string $key |
|
| 46 | - * @param string $value |
|
| 47 | - * |
|
| 48 | - * @return void |
|
| 49 | - */ |
|
| 50 | - public function setEntityAttribute($key, $value) |
|
| 51 | - { |
|
| 52 | - $this->attributes[$key] = $value; |
|
| 53 | - } |
|
| 41 | + /** |
|
| 42 | + * Method used by the mapper to set raw |
|
| 43 | + * key-value pair |
|
| 44 | + * |
|
| 45 | + * @param string $key |
|
| 46 | + * @param string $value |
|
| 47 | + * |
|
| 48 | + * @return void |
|
| 49 | + */ |
|
| 50 | + public function setEntityAttribute($key, $value) |
|
| 51 | + { |
|
| 52 | + $this->attributes[$key] = $value; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Method used by the mapper to get single |
|
| 57 | - * key-value pair |
|
| 58 | - * |
|
| 59 | - * @param string $key |
|
| 60 | - * @return mixed|null |
|
| 61 | - */ |
|
| 62 | - public function getEntityAttribute($key) |
|
| 63 | - { |
|
| 64 | - if (array_key_exists($key, $this->attributes)) { |
|
| 65 | - return $this->attributes[$key]; |
|
| 66 | - } else { |
|
| 67 | - return null; |
|
| 68 | - } |
|
| 69 | - } |
|
| 55 | + /** |
|
| 56 | + * Method used by the mapper to get single |
|
| 57 | + * key-value pair |
|
| 58 | + * |
|
| 59 | + * @param string $key |
|
| 60 | + * @return mixed|null |
|
| 61 | + */ |
|
| 62 | + public function getEntityAttribute($key) |
|
| 63 | + { |
|
| 64 | + if (array_key_exists($key, $this->attributes)) { |
|
| 65 | + return $this->attributes[$key]; |
|
| 66 | + } else { |
|
| 67 | + return null; |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | 70 | } |
@@ -6,35 +6,35 @@ |
||
| 6 | 6 | |
| 7 | 7 | class EntityNotFoundException extends RuntimeException |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Name of the affected Entity Map. |
|
| 11 | - * |
|
| 12 | - * @var string |
|
| 13 | - */ |
|
| 14 | - protected $entity; |
|
| 9 | + /** |
|
| 10 | + * Name of the affected Entity Map. |
|
| 11 | + * |
|
| 12 | + * @var string |
|
| 13 | + */ |
|
| 14 | + protected $entity; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Set the affected Entity Map. |
|
| 18 | - * |
|
| 19 | - * @param string $entity |
|
| 20 | - * @return $this |
|
| 21 | - */ |
|
| 22 | - public function setEntity($entity) |
|
| 23 | - { |
|
| 24 | - $this->entity = $entity; |
|
| 16 | + /** |
|
| 17 | + * Set the affected Entity Map. |
|
| 18 | + * |
|
| 19 | + * @param string $entity |
|
| 20 | + * @return $this |
|
| 21 | + */ |
|
| 22 | + public function setEntity($entity) |
|
| 23 | + { |
|
| 24 | + $this->entity = $entity; |
|
| 25 | 25 | |
| 26 | - $this->message = "No query results for entity [{$entity}]."; |
|
| 26 | + $this->message = "No query results for entity [{$entity}]."; |
|
| 27 | 27 | |
| 28 | - return $this; |
|
| 29 | - } |
|
| 28 | + return $this; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Get the affected Entity. |
|
| 33 | - * |
|
| 34 | - * @return string |
|
| 35 | - */ |
|
| 36 | - public function getEntity() |
|
| 37 | - { |
|
| 38 | - return $this->entity; |
|
| 39 | - } |
|
| 31 | + /** |
|
| 32 | + * Get the affected Entity. |
|
| 33 | + * |
|
| 34 | + * @return string |
|
| 35 | + */ |
|
| 36 | + public function getEntity() |
|
| 37 | + { |
|
| 38 | + return $this->entity; |
|
| 39 | + } |
|
| 40 | 40 | } |
@@ -393,7 +393,7 @@ discard block |
||
| 393 | 393 | if (!is_null($this->sequence)) { |
| 394 | 394 | return $this->sequence; |
| 395 | 395 | } else { |
| 396 | - return $this->getTable() . '_id_seq'; |
|
| 396 | + return $this->getTable().'_id_seq'; |
|
| 397 | 397 | } |
| 398 | 398 | } |
| 399 | 399 | |
@@ -563,7 +563,7 @@ discard block |
||
| 563 | 563 | */ |
| 564 | 564 | public function getQualifiedKeyName() |
| 565 | 565 | { |
| 566 | - return $this->getTable() . '.' . $this->getKeyName(); |
|
| 566 | + return $this->getTable().'.'.$this->getKeyName(); |
|
| 567 | 567 | } |
| 568 | 568 | |
| 569 | 569 | /** |
@@ -644,7 +644,7 @@ discard block |
||
| 644 | 644 | */ |
| 645 | 645 | public function getForeignKey() |
| 646 | 646 | { |
| 647 | - return snake_case(class_basename($this->getClass())) . '_id'; |
|
| 647 | + return snake_case(class_basename($this->getClass())).'_id'; |
|
| 648 | 648 | } |
| 649 | 649 | |
| 650 | 650 | /** |
@@ -667,7 +667,7 @@ discard block |
||
| 667 | 667 | |
| 668 | 668 | $localKey = $localKey ?: $this->getKeyName(); |
| 669 | 669 | |
| 670 | - return new HasOne($relatedMapper, $entity, $relatedMap->getTable() . '.' . $foreignKey, $localKey); |
|
| 670 | + return new HasOne($relatedMapper, $entity, $relatedMap->getTable().'.'.$foreignKey, $localKey); |
|
| 671 | 671 | } |
| 672 | 672 | |
| 673 | 673 | /** |
@@ -692,7 +692,7 @@ discard block |
||
| 692 | 692 | |
| 693 | 693 | $table = $relatedMapper->getEntityMap()->getTable(); |
| 694 | 694 | |
| 695 | - return new MorphOne($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey); |
|
| 695 | + return new MorphOne($relatedMapper, $entity, $table.'.'.$type, $table.'.'.$id, $localKey); |
|
| 696 | 696 | } |
| 697 | 697 | |
| 698 | 698 | /** |
@@ -721,7 +721,7 @@ discard block |
||
| 721 | 721 | // foreign key name by using the name of the relationship function, which |
| 722 | 722 | // when combined with an "_id" should conventionally match the columns. |
| 723 | 723 | if (is_null($foreignKey)) { |
| 724 | - $foreignKey = snake_case($relation) . '_id'; |
|
| 724 | + $foreignKey = snake_case($relation).'_id'; |
|
| 725 | 725 | } |
| 726 | 726 | |
| 727 | 727 | $relatedMapper = $this->manager->mapper($related); |
@@ -798,7 +798,7 @@ discard block |
||
| 798 | 798 | |
| 799 | 799 | $relatedMapper = $this->manager->mapper($related); |
| 800 | 800 | |
| 801 | - $table = $relatedMapper->getEntityMap()->getTable() . '.' . $foreignKey; |
|
| 801 | + $table = $relatedMapper->getEntityMap()->getTable().'.'.$foreignKey; |
|
| 802 | 802 | |
| 803 | 803 | $localKey = $localKey ?: $this->getKeyName(); |
| 804 | 804 | |
@@ -856,7 +856,7 @@ discard block |
||
| 856 | 856 | |
| 857 | 857 | $localKey = $localKey ?: $this->getKeyName(); |
| 858 | 858 | |
| 859 | - return new MorphMany($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey); |
|
| 859 | + return new MorphMany($relatedMapper, $entity, $table.'.'.$type, $table.'.'.$id, $localKey); |
|
| 860 | 860 | } |
| 861 | 861 | |
| 862 | 862 | /** |
@@ -921,7 +921,7 @@ discard block |
||
| 921 | 921 | // First, we will need to determine the foreign key and "other key" for the |
| 922 | 922 | // relationship. Once we have determined the keys we will make the query |
| 923 | 923 | // instances, as well as the relationship instances we need for these. |
| 924 | - $foreignKey = $foreignKey ?: $name . '_id'; |
|
| 924 | + $foreignKey = $foreignKey ?: $name.'_id'; |
|
| 925 | 925 | |
| 926 | 926 | $relatedMapper = $this->manager->mapper($related); |
| 927 | 927 | |
@@ -951,7 +951,7 @@ discard block |
||
| 951 | 951 | // For the inverse of the polymorphic many-to-many relations, we will change |
| 952 | 952 | // the way we determine the foreign and other keys, as it is the opposite |
| 953 | 953 | // of the morph-to-many method since we're figuring out these inverses. |
| 954 | - $otherKey = $otherKey ?: $name . '_id'; |
|
| 954 | + $otherKey = $otherKey ?: $name.'_id'; |
|
| 955 | 955 | |
| 956 | 956 | return $this->morphToMany($entity, $related, $name, $table, $foreignKey, $otherKey, true); |
| 957 | 957 | } |
@@ -965,7 +965,7 @@ discard block |
||
| 965 | 965 | { |
| 966 | 966 | $self = __FUNCTION__; |
| 967 | 967 | |
| 968 | - $caller = array_first(debug_backtrace(false), function ($key, $trace) use ($self) { |
|
| 968 | + $caller = array_first(debug_backtrace(false), function($key, $trace) use ($self) { |
|
| 969 | 969 | $caller = $trace['function']; |
| 970 | 970 | |
| 971 | 971 | return (!in_array($caller, EntityMap::$manyMethods) && $caller != $self); |
@@ -1009,9 +1009,9 @@ discard block |
||
| 1009 | 1009 | */ |
| 1010 | 1010 | protected function getMorphs($name, $type, $id) |
| 1011 | 1011 | { |
| 1012 | - $type = $type ?: $name . '_type'; |
|
| 1012 | + $type = $type ?: $name.'_type'; |
|
| 1013 | 1013 | |
| 1014 | - $id = $id ?: $name . '_id'; |
|
| 1014 | + $id = $id ?: $name.'_id'; |
|
| 1015 | 1015 | |
| 1016 | 1016 | return [$type, $id]; |
| 1017 | 1017 | } |
@@ -1176,7 +1176,7 @@ discard block |
||
| 1176 | 1176 | public function __call($method, $parameters) |
| 1177 | 1177 | { |
| 1178 | 1178 | if (!array_key_exists($method, $this->dynamicRelationships)) { |
| 1179 | - throw new Exception(get_class($this) . " has no method $method"); |
|
| 1179 | + throw new Exception(get_class($this)." has no method $method"); |
|
| 1180 | 1180 | } |
| 1181 | 1181 | |
| 1182 | 1182 | // Add $this to parameters so the closure can call relationship method on the map. |
@@ -23,1167 +23,1167 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class EntityMap |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * The mapping driver to use with this entity |
|
| 28 | - */ |
|
| 29 | - protected $driver = 'illuminate'; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * The Database Connection name for the model. |
|
| 33 | - * |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - protected $connection; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * The table associated with the entity. |
|
| 40 | - * |
|
| 41 | - * @var string|null |
|
| 42 | - */ |
|
| 43 | - protected $table = null; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * The primary key for the model. |
|
| 47 | - * |
|
| 48 | - * @var string |
|
| 49 | - */ |
|
| 50 | - protected $primaryKey = 'id'; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Array containing a list of class attributes. Mandatory if the |
|
| 54 | - * mapped entity is a Plain PHP Object. |
|
| 55 | - * |
|
| 56 | - * @var array |
|
| 57 | - */ |
|
| 58 | - protected $attributes = []; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The Custom Domain Class to use with this mapping |
|
| 62 | - * |
|
| 63 | - * @var string|null |
|
| 64 | - */ |
|
| 65 | - protected $class = null; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Attributes that should be treated as Value Objects |
|
| 69 | - * |
|
| 70 | - * @var array |
|
| 71 | - */ |
|
| 72 | - protected $embeddables = []; |
|
| 26 | + /** |
|
| 27 | + * The mapping driver to use with this entity |
|
| 28 | + */ |
|
| 29 | + protected $driver = 'illuminate'; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * The Database Connection name for the model. |
|
| 33 | + * |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + protected $connection; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * The table associated with the entity. |
|
| 40 | + * |
|
| 41 | + * @var string|null |
|
| 42 | + */ |
|
| 43 | + protected $table = null; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * The primary key for the model. |
|
| 47 | + * |
|
| 48 | + * @var string |
|
| 49 | + */ |
|
| 50 | + protected $primaryKey = 'id'; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Array containing a list of class attributes. Mandatory if the |
|
| 54 | + * mapped entity is a Plain PHP Object. |
|
| 55 | + * |
|
| 56 | + * @var array |
|
| 57 | + */ |
|
| 58 | + protected $attributes = []; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The Custom Domain Class to use with this mapping |
|
| 62 | + * |
|
| 63 | + * @var string|null |
|
| 64 | + */ |
|
| 65 | + protected $class = null; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Attributes that should be treated as Value Objects |
|
| 69 | + * |
|
| 70 | + * @var array |
|
| 71 | + */ |
|
| 72 | + protected $embeddables = []; |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Determine the relationships method used on the entity. |
|
| 76 | - * If not set, mapper will autodetect them |
|
| 77 | - * |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - private $relationships = []; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Relationships that should be treated as collection. |
|
| 84 | - * |
|
| 85 | - * @var array |
|
| 86 | - */ |
|
| 87 | - private $manyRelations = []; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Relationships that should be treated as single entity. |
|
| 91 | - * |
|
| 92 | - * @var array |
|
| 93 | - */ |
|
| 94 | - private $singleRelations = []; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Relationships for which the key is stored in the Entity itself |
|
| 98 | - * |
|
| 99 | - * @var array |
|
| 100 | - */ |
|
| 101 | - private $localRelations = []; |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Relationships for which the key is stored in the Related Entity |
|
| 105 | - * |
|
| 106 | - * @var array |
|
| 107 | - */ |
|
| 108 | - private $foreignRelations = []; |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Relationships which use a pivot record. |
|
| 112 | - * |
|
| 113 | - * @var array |
|
| 114 | - */ |
|
| 115 | - private $pivotRelations = []; |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Dynamic relationships |
|
| 119 | - * |
|
| 120 | - * @var array |
|
| 121 | - */ |
|
| 122 | - private $dynamicRelationships = []; |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * The number of models to return for pagination. |
|
| 126 | - * |
|
| 127 | - * @var int |
|
| 128 | - */ |
|
| 129 | - protected $perPage = 15; |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * The relations to eager load on every query. |
|
| 133 | - * |
|
| 134 | - * @var array |
|
| 135 | - */ |
|
| 136 | - protected $with = []; |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * The class name to be used in polymorphic relations. |
|
| 140 | - * |
|
| 141 | - * @var string |
|
| 142 | - */ |
|
| 143 | - protected $morphClass; |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Sequence name, to be used with postgreSql |
|
| 147 | - * defaults to %table_name%_id_seq |
|
| 148 | - * |
|
| 149 | - * @var string|null |
|
| 150 | - */ |
|
| 151 | - protected $sequence = null; |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Indicates if the entity should be timestamped. |
|
| 155 | - * |
|
| 156 | - * @var bool |
|
| 157 | - */ |
|
| 158 | - public $timestamps = false; |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * The name of the "created at" column. |
|
| 162 | - * |
|
| 163 | - * @var string |
|
| 164 | - */ |
|
| 165 | - protected $createdAtColumn = 'created_at'; |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * The name of the "updated at" column. |
|
| 169 | - * |
|
| 170 | - * @var string |
|
| 171 | - */ |
|
| 172 | - protected $updatedAtColumn = 'updated_at'; |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Indicates if the entity uses softdeletes |
|
| 176 | - * |
|
| 177 | - * @var boolean |
|
| 178 | - */ |
|
| 179 | - public $softDeletes = false; |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * The name of the "deleted at" column. |
|
| 183 | - * |
|
| 184 | - * @var string |
|
| 185 | - */ |
|
| 186 | - protected $deletedAtColumn = 'deleted_at'; |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * The many to many relationship methods. |
|
| 190 | - * |
|
| 191 | - * @var array |
|
| 192 | - */ |
|
| 193 | - protected static $manyMethods = ['belongsToMany', 'morphToMany', 'morphedByMany']; |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * The 'Many' relationships classes, which related Entity attribute should be |
|
| 197 | - * an array/entityCollection |
|
| 198 | - * |
|
| 199 | - * @var array |
|
| 200 | - */ |
|
| 201 | - protected static $manyClasses = ['BelongsToMany', 'HasMany', 'HasManyThrough', 'MorphMany', 'MorphToMany']; |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * The 'Single' relationships classes, which related Entity attribute should be |
|
| 205 | - * another Entity. |
|
| 206 | - * |
|
| 207 | - * @var array |
|
| 208 | - */ |
|
| 209 | - protected static $singleClasses = ['BelongsTo', 'HasOne', 'MorphOne', 'MorphTo']; |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Relationships with a pivot record |
|
| 213 | - * |
|
| 214 | - * @var array |
|
| 215 | - */ |
|
| 216 | - protected static $pivotClasses = ['BelongsToMany', 'MorphToMany']; |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Relationships on which key is stored in the Entity itself |
|
| 220 | - * |
|
| 221 | - * @var array |
|
| 222 | - */ |
|
| 223 | - protected static $localClasses = ['BelongsTo', 'MorphTo']; |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Relationships on which key is stored in the related Entity record or in a pivot record |
|
| 227 | - * |
|
| 228 | - * @var array |
|
| 229 | - */ |
|
| 230 | - protected static $foreignClasses = [ |
|
| 231 | - 'BelongsToMany', |
|
| 232 | - 'HasMany', |
|
| 233 | - 'HasManyThrough', |
|
| 234 | - 'MorphMany', |
|
| 235 | - 'MorphToMany', |
|
| 236 | - 'HasOne', |
|
| 237 | - 'MorphOne', |
|
| 238 | - ]; |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * The date format to use with the current database connection |
|
| 242 | - * |
|
| 243 | - * @var string |
|
| 244 | - */ |
|
| 245 | - protected $dateFormat; |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * The Analogue's manager instance. |
|
| 249 | - * |
|
| 250 | - * @var \Analogue\ORM\System\Manager |
|
| 251 | - */ |
|
| 252 | - private $manager; |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Set the Manager that will be used for relationship's mapper instantiations. |
|
| 256 | - * |
|
| 257 | - * @param Manager $manager |
|
| 258 | - */ |
|
| 259 | - public function setManager(Manager $manager) |
|
| 260 | - { |
|
| 261 | - $this->manager = $manager; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Return Domain class attributes, useful when mapping to a Plain PHP Object |
|
| 266 | - * |
|
| 267 | - * @return array |
|
| 268 | - */ |
|
| 269 | - public function getAttributes() |
|
| 270 | - { |
|
| 271 | - return $this->attributes; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Set the domain class attributes |
|
| 276 | - * |
|
| 277 | - * @param array $attributeNames |
|
| 278 | - */ |
|
| 279 | - public function setAttributes(array $attributeNames) |
|
| 280 | - { |
|
| 281 | - $this->attributes = $attributeNames; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Get all the attribute names for the class, including relationships, embeddables and primary key. |
|
| 286 | - * |
|
| 287 | - * @return array |
|
| 288 | - */ |
|
| 289 | - public function getCompiledAttributes() |
|
| 290 | - { |
|
| 291 | - $key = $this->getKeyName(); |
|
| 292 | - |
|
| 293 | - $embeddables = array_keys($this->getEmbeddables()); |
|
| 294 | - |
|
| 295 | - $relationships = $this->getRelationships(); |
|
| 296 | - |
|
| 297 | - $attributes = $this->getAttributes(); |
|
| 298 | - |
|
| 299 | - return array_merge([$key], $embeddables, $relationships, $attributes); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Set the date format to use with the current database connection |
|
| 304 | - * |
|
| 305 | - * @param string $format |
|
| 306 | - */ |
|
| 307 | - public function setDateFormat($format) |
|
| 308 | - { |
|
| 309 | - $this->dateFormat = $format; |
|
| 310 | - } |
|
| 74 | + /** |
|
| 75 | + * Determine the relationships method used on the entity. |
|
| 76 | + * If not set, mapper will autodetect them |
|
| 77 | + * |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + private $relationships = []; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Relationships that should be treated as collection. |
|
| 84 | + * |
|
| 85 | + * @var array |
|
| 86 | + */ |
|
| 87 | + private $manyRelations = []; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Relationships that should be treated as single entity. |
|
| 91 | + * |
|
| 92 | + * @var array |
|
| 93 | + */ |
|
| 94 | + private $singleRelations = []; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Relationships for which the key is stored in the Entity itself |
|
| 98 | + * |
|
| 99 | + * @var array |
|
| 100 | + */ |
|
| 101 | + private $localRelations = []; |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Relationships for which the key is stored in the Related Entity |
|
| 105 | + * |
|
| 106 | + * @var array |
|
| 107 | + */ |
|
| 108 | + private $foreignRelations = []; |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Relationships which use a pivot record. |
|
| 112 | + * |
|
| 113 | + * @var array |
|
| 114 | + */ |
|
| 115 | + private $pivotRelations = []; |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Dynamic relationships |
|
| 119 | + * |
|
| 120 | + * @var array |
|
| 121 | + */ |
|
| 122 | + private $dynamicRelationships = []; |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * The number of models to return for pagination. |
|
| 126 | + * |
|
| 127 | + * @var int |
|
| 128 | + */ |
|
| 129 | + protected $perPage = 15; |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * The relations to eager load on every query. |
|
| 133 | + * |
|
| 134 | + * @var array |
|
| 135 | + */ |
|
| 136 | + protected $with = []; |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * The class name to be used in polymorphic relations. |
|
| 140 | + * |
|
| 141 | + * @var string |
|
| 142 | + */ |
|
| 143 | + protected $morphClass; |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Sequence name, to be used with postgreSql |
|
| 147 | + * defaults to %table_name%_id_seq |
|
| 148 | + * |
|
| 149 | + * @var string|null |
|
| 150 | + */ |
|
| 151 | + protected $sequence = null; |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Indicates if the entity should be timestamped. |
|
| 155 | + * |
|
| 156 | + * @var bool |
|
| 157 | + */ |
|
| 158 | + public $timestamps = false; |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * The name of the "created at" column. |
|
| 162 | + * |
|
| 163 | + * @var string |
|
| 164 | + */ |
|
| 165 | + protected $createdAtColumn = 'created_at'; |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * The name of the "updated at" column. |
|
| 169 | + * |
|
| 170 | + * @var string |
|
| 171 | + */ |
|
| 172 | + protected $updatedAtColumn = 'updated_at'; |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Indicates if the entity uses softdeletes |
|
| 176 | + * |
|
| 177 | + * @var boolean |
|
| 178 | + */ |
|
| 179 | + public $softDeletes = false; |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * The name of the "deleted at" column. |
|
| 183 | + * |
|
| 184 | + * @var string |
|
| 185 | + */ |
|
| 186 | + protected $deletedAtColumn = 'deleted_at'; |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * The many to many relationship methods. |
|
| 190 | + * |
|
| 191 | + * @var array |
|
| 192 | + */ |
|
| 193 | + protected static $manyMethods = ['belongsToMany', 'morphToMany', 'morphedByMany']; |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * The 'Many' relationships classes, which related Entity attribute should be |
|
| 197 | + * an array/entityCollection |
|
| 198 | + * |
|
| 199 | + * @var array |
|
| 200 | + */ |
|
| 201 | + protected static $manyClasses = ['BelongsToMany', 'HasMany', 'HasManyThrough', 'MorphMany', 'MorphToMany']; |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * The 'Single' relationships classes, which related Entity attribute should be |
|
| 205 | + * another Entity. |
|
| 206 | + * |
|
| 207 | + * @var array |
|
| 208 | + */ |
|
| 209 | + protected static $singleClasses = ['BelongsTo', 'HasOne', 'MorphOne', 'MorphTo']; |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Relationships with a pivot record |
|
| 213 | + * |
|
| 214 | + * @var array |
|
| 215 | + */ |
|
| 216 | + protected static $pivotClasses = ['BelongsToMany', 'MorphToMany']; |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Relationships on which key is stored in the Entity itself |
|
| 220 | + * |
|
| 221 | + * @var array |
|
| 222 | + */ |
|
| 223 | + protected static $localClasses = ['BelongsTo', 'MorphTo']; |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Relationships on which key is stored in the related Entity record or in a pivot record |
|
| 227 | + * |
|
| 228 | + * @var array |
|
| 229 | + */ |
|
| 230 | + protected static $foreignClasses = [ |
|
| 231 | + 'BelongsToMany', |
|
| 232 | + 'HasMany', |
|
| 233 | + 'HasManyThrough', |
|
| 234 | + 'MorphMany', |
|
| 235 | + 'MorphToMany', |
|
| 236 | + 'HasOne', |
|
| 237 | + 'MorphOne', |
|
| 238 | + ]; |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * The date format to use with the current database connection |
|
| 242 | + * |
|
| 243 | + * @var string |
|
| 244 | + */ |
|
| 245 | + protected $dateFormat; |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * The Analogue's manager instance. |
|
| 249 | + * |
|
| 250 | + * @var \Analogue\ORM\System\Manager |
|
| 251 | + */ |
|
| 252 | + private $manager; |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Set the Manager that will be used for relationship's mapper instantiations. |
|
| 256 | + * |
|
| 257 | + * @param Manager $manager |
|
| 258 | + */ |
|
| 259 | + public function setManager(Manager $manager) |
|
| 260 | + { |
|
| 261 | + $this->manager = $manager; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Return Domain class attributes, useful when mapping to a Plain PHP Object |
|
| 266 | + * |
|
| 267 | + * @return array |
|
| 268 | + */ |
|
| 269 | + public function getAttributes() |
|
| 270 | + { |
|
| 271 | + return $this->attributes; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Set the domain class attributes |
|
| 276 | + * |
|
| 277 | + * @param array $attributeNames |
|
| 278 | + */ |
|
| 279 | + public function setAttributes(array $attributeNames) |
|
| 280 | + { |
|
| 281 | + $this->attributes = $attributeNames; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Get all the attribute names for the class, including relationships, embeddables and primary key. |
|
| 286 | + * |
|
| 287 | + * @return array |
|
| 288 | + */ |
|
| 289 | + public function getCompiledAttributes() |
|
| 290 | + { |
|
| 291 | + $key = $this->getKeyName(); |
|
| 292 | + |
|
| 293 | + $embeddables = array_keys($this->getEmbeddables()); |
|
| 294 | + |
|
| 295 | + $relationships = $this->getRelationships(); |
|
| 296 | + |
|
| 297 | + $attributes = $this->getAttributes(); |
|
| 298 | + |
|
| 299 | + return array_merge([$key], $embeddables, $relationships, $attributes); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Set the date format to use with the current database connection |
|
| 304 | + * |
|
| 305 | + * @param string $format |
|
| 306 | + */ |
|
| 307 | + public function setDateFormat($format) |
|
| 308 | + { |
|
| 309 | + $this->dateFormat = $format; |
|
| 310 | + } |
|
| 311 | 311 | |
| 312 | - /** |
|
| 313 | - * Get the date format to use with the current database connection |
|
| 314 | - * |
|
| 315 | - * @return string |
|
| 316 | - */ |
|
| 317 | - public function getDateFormat() |
|
| 318 | - { |
|
| 319 | - return $this->dateFormat; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Set the Driver for this mapping |
|
| 324 | - * |
|
| 325 | - * @param string $driver |
|
| 326 | - */ |
|
| 327 | - public function setDriver($driver) |
|
| 328 | - { |
|
| 329 | - $this->driver = $driver; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * Get the Driver for this mapping. |
|
| 334 | - * |
|
| 335 | - * @return string |
|
| 336 | - */ |
|
| 337 | - public function getDriver() |
|
| 338 | - { |
|
| 339 | - return $this->driver; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Set the db connection to use on the table |
|
| 344 | - * |
|
| 345 | - * @param $connection |
|
| 346 | - */ |
|
| 347 | - public function setConnection($connection) |
|
| 348 | - { |
|
| 349 | - $this->connection = $connection; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Get the Database connection the Entity is stored on. |
|
| 354 | - * |
|
| 355 | - * @return string |
|
| 356 | - */ |
|
| 357 | - public function getConnection() |
|
| 358 | - { |
|
| 359 | - return $this->connection; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Get the table associated with the entity. |
|
| 364 | - * |
|
| 365 | - * @return string |
|
| 366 | - */ |
|
| 367 | - public function getTable() |
|
| 368 | - { |
|
| 369 | - if (!is_null($this->table)) { |
|
| 370 | - return $this->table; |
|
| 371 | - } |
|
| 312 | + /** |
|
| 313 | + * Get the date format to use with the current database connection |
|
| 314 | + * |
|
| 315 | + * @return string |
|
| 316 | + */ |
|
| 317 | + public function getDateFormat() |
|
| 318 | + { |
|
| 319 | + return $this->dateFormat; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Set the Driver for this mapping |
|
| 324 | + * |
|
| 325 | + * @param string $driver |
|
| 326 | + */ |
|
| 327 | + public function setDriver($driver) |
|
| 328 | + { |
|
| 329 | + $this->driver = $driver; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * Get the Driver for this mapping. |
|
| 334 | + * |
|
| 335 | + * @return string |
|
| 336 | + */ |
|
| 337 | + public function getDriver() |
|
| 338 | + { |
|
| 339 | + return $this->driver; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Set the db connection to use on the table |
|
| 344 | + * |
|
| 345 | + * @param $connection |
|
| 346 | + */ |
|
| 347 | + public function setConnection($connection) |
|
| 348 | + { |
|
| 349 | + $this->connection = $connection; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Get the Database connection the Entity is stored on. |
|
| 354 | + * |
|
| 355 | + * @return string |
|
| 356 | + */ |
|
| 357 | + public function getConnection() |
|
| 358 | + { |
|
| 359 | + return $this->connection; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Get the table associated with the entity. |
|
| 364 | + * |
|
| 365 | + * @return string |
|
| 366 | + */ |
|
| 367 | + public function getTable() |
|
| 368 | + { |
|
| 369 | + if (!is_null($this->table)) { |
|
| 370 | + return $this->table; |
|
| 371 | + } |
|
| 372 | 372 | |
| 373 | - return str_replace('\\', '', snake_case(str_plural(class_basename($this->getClass())))); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * Set the database table name |
|
| 378 | - * |
|
| 379 | - * @param string $table |
|
| 380 | - */ |
|
| 381 | - public function setTable($table) |
|
| 382 | - { |
|
| 383 | - $this->table = $table; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * Get the pgSql sequence name |
|
| 388 | - * |
|
| 389 | - * @return string |
|
| 390 | - */ |
|
| 391 | - public function getSequence() |
|
| 392 | - { |
|
| 393 | - if (!is_null($this->sequence)) { |
|
| 394 | - return $this->sequence; |
|
| 395 | - } else { |
|
| 396 | - return $this->getTable() . '_id_seq'; |
|
| 397 | - } |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Get the custom entity class |
|
| 402 | - * |
|
| 403 | - * @return string namespaced class name |
|
| 404 | - */ |
|
| 405 | - public function getClass() |
|
| 406 | - { |
|
| 407 | - return isset($this->class) ? $this->class : null; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Set the custom entity class |
|
| 412 | - * |
|
| 413 | - * @param string $class namespaced class name |
|
| 414 | - */ |
|
| 415 | - public function setClass($class) |
|
| 416 | - { |
|
| 417 | - // Throw exception if class not exists |
|
| 418 | - |
|
| 419 | - $this->class = $class; |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * Get the embedded Value Objects |
|
| 424 | - * |
|
| 425 | - * @return array |
|
| 426 | - */ |
|
| 427 | - public function getEmbeddables() |
|
| 428 | - { |
|
| 429 | - return $this->embeddables; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * Set the embedded Value Objects |
|
| 434 | - * |
|
| 435 | - * @param array $embeddables |
|
| 436 | - */ |
|
| 437 | - public function setEmbeddables(array $embeddables) |
|
| 438 | - { |
|
| 439 | - $this->embeddables = $embeddables; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * Get the relationships to map on a custom domain |
|
| 444 | - * class. |
|
| 445 | - * |
|
| 446 | - * @return array |
|
| 447 | - */ |
|
| 448 | - public function getRelationships() |
|
| 449 | - { |
|
| 450 | - return $this->relationships; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * Relationships of the Entity type |
|
| 455 | - * |
|
| 456 | - * @return array |
|
| 457 | - */ |
|
| 458 | - public function getSingleRelationships() |
|
| 459 | - { |
|
| 460 | - return $this->singleRelations; |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - /** |
|
| 464 | - * Relationships of type Collection |
|
| 465 | - * |
|
| 466 | - * @return array |
|
| 467 | - */ |
|
| 468 | - public function getManyRelationships() |
|
| 469 | - { |
|
| 470 | - return $this->manyRelations; |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - /** |
|
| 474 | - * Relationships with foreign key in the mapped entity record. |
|
| 475 | - * |
|
| 476 | - * @return array |
|
| 477 | - */ |
|
| 478 | - public function getLocalRelationships() |
|
| 479 | - { |
|
| 480 | - return $this->localRelations; |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * Relationships with foreign key in the related Entity record |
|
| 485 | - * |
|
| 486 | - * @return array |
|
| 487 | - */ |
|
| 488 | - public function getForeignRelationships() |
|
| 489 | - { |
|
| 490 | - return $this->foreignRelations; |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - /** |
|
| 494 | - * Relationships which keys are stored in a pivot record |
|
| 495 | - * |
|
| 496 | - * @return array |
|
| 497 | - */ |
|
| 498 | - public function getPivotRelationships() |
|
| 499 | - { |
|
| 500 | - return $this->pivotRelations; |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - /** |
|
| 504 | - * Add a Dynamic Relationship method at runtime. This has to be done |
|
| 505 | - * by hooking the 'initializing' event, before entityMap is initialized. |
|
| 506 | - * |
|
| 507 | - * @param string $name Relation name |
|
| 508 | - * @param \Closure $relationship |
|
| 509 | - * |
|
| 510 | - * @return void |
|
| 511 | - */ |
|
| 512 | - public function addRelationshipMethod($name, \Closure $relationship) |
|
| 513 | - { |
|
| 514 | - $this->dynamicRelationships[$name] = $relationship; |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - /** |
|
| 518 | - * Get the dynamic relationship method names. |
|
| 519 | - * |
|
| 520 | - * @return array |
|
| 521 | - */ |
|
| 522 | - public function getDynamicRelationships() |
|
| 523 | - { |
|
| 524 | - return array_keys($this->dynamicRelationships); |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * Get the relationships that have to be eager loaded |
|
| 529 | - * on each request. |
|
| 530 | - * |
|
| 531 | - * @return array |
|
| 532 | - */ |
|
| 533 | - public function getEagerloadedRelationships() |
|
| 534 | - { |
|
| 535 | - return $this->with; |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - /** |
|
| 539 | - * Get the primary key for the entity. |
|
| 540 | - * |
|
| 541 | - * @return string |
|
| 542 | - */ |
|
| 543 | - public function getKeyName() |
|
| 544 | - { |
|
| 545 | - return $this->primaryKey; |
|
| 546 | - } |
|
| 373 | + return str_replace('\\', '', snake_case(str_plural(class_basename($this->getClass())))); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * Set the database table name |
|
| 378 | + * |
|
| 379 | + * @param string $table |
|
| 380 | + */ |
|
| 381 | + public function setTable($table) |
|
| 382 | + { |
|
| 383 | + $this->table = $table; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Get the pgSql sequence name |
|
| 388 | + * |
|
| 389 | + * @return string |
|
| 390 | + */ |
|
| 391 | + public function getSequence() |
|
| 392 | + { |
|
| 393 | + if (!is_null($this->sequence)) { |
|
| 394 | + return $this->sequence; |
|
| 395 | + } else { |
|
| 396 | + return $this->getTable() . '_id_seq'; |
|
| 397 | + } |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Get the custom entity class |
|
| 402 | + * |
|
| 403 | + * @return string namespaced class name |
|
| 404 | + */ |
|
| 405 | + public function getClass() |
|
| 406 | + { |
|
| 407 | + return isset($this->class) ? $this->class : null; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Set the custom entity class |
|
| 412 | + * |
|
| 413 | + * @param string $class namespaced class name |
|
| 414 | + */ |
|
| 415 | + public function setClass($class) |
|
| 416 | + { |
|
| 417 | + // Throw exception if class not exists |
|
| 418 | + |
|
| 419 | + $this->class = $class; |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * Get the embedded Value Objects |
|
| 424 | + * |
|
| 425 | + * @return array |
|
| 426 | + */ |
|
| 427 | + public function getEmbeddables() |
|
| 428 | + { |
|
| 429 | + return $this->embeddables; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * Set the embedded Value Objects |
|
| 434 | + * |
|
| 435 | + * @param array $embeddables |
|
| 436 | + */ |
|
| 437 | + public function setEmbeddables(array $embeddables) |
|
| 438 | + { |
|
| 439 | + $this->embeddables = $embeddables; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * Get the relationships to map on a custom domain |
|
| 444 | + * class. |
|
| 445 | + * |
|
| 446 | + * @return array |
|
| 447 | + */ |
|
| 448 | + public function getRelationships() |
|
| 449 | + { |
|
| 450 | + return $this->relationships; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * Relationships of the Entity type |
|
| 455 | + * |
|
| 456 | + * @return array |
|
| 457 | + */ |
|
| 458 | + public function getSingleRelationships() |
|
| 459 | + { |
|
| 460 | + return $this->singleRelations; |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + /** |
|
| 464 | + * Relationships of type Collection |
|
| 465 | + * |
|
| 466 | + * @return array |
|
| 467 | + */ |
|
| 468 | + public function getManyRelationships() |
|
| 469 | + { |
|
| 470 | + return $this->manyRelations; |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + /** |
|
| 474 | + * Relationships with foreign key in the mapped entity record. |
|
| 475 | + * |
|
| 476 | + * @return array |
|
| 477 | + */ |
|
| 478 | + public function getLocalRelationships() |
|
| 479 | + { |
|
| 480 | + return $this->localRelations; |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * Relationships with foreign key in the related Entity record |
|
| 485 | + * |
|
| 486 | + * @return array |
|
| 487 | + */ |
|
| 488 | + public function getForeignRelationships() |
|
| 489 | + { |
|
| 490 | + return $this->foreignRelations; |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + /** |
|
| 494 | + * Relationships which keys are stored in a pivot record |
|
| 495 | + * |
|
| 496 | + * @return array |
|
| 497 | + */ |
|
| 498 | + public function getPivotRelationships() |
|
| 499 | + { |
|
| 500 | + return $this->pivotRelations; |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + /** |
|
| 504 | + * Add a Dynamic Relationship method at runtime. This has to be done |
|
| 505 | + * by hooking the 'initializing' event, before entityMap is initialized. |
|
| 506 | + * |
|
| 507 | + * @param string $name Relation name |
|
| 508 | + * @param \Closure $relationship |
|
| 509 | + * |
|
| 510 | + * @return void |
|
| 511 | + */ |
|
| 512 | + public function addRelationshipMethod($name, \Closure $relationship) |
|
| 513 | + { |
|
| 514 | + $this->dynamicRelationships[$name] = $relationship; |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + /** |
|
| 518 | + * Get the dynamic relationship method names. |
|
| 519 | + * |
|
| 520 | + * @return array |
|
| 521 | + */ |
|
| 522 | + public function getDynamicRelationships() |
|
| 523 | + { |
|
| 524 | + return array_keys($this->dynamicRelationships); |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * Get the relationships that have to be eager loaded |
|
| 529 | + * on each request. |
|
| 530 | + * |
|
| 531 | + * @return array |
|
| 532 | + */ |
|
| 533 | + public function getEagerloadedRelationships() |
|
| 534 | + { |
|
| 535 | + return $this->with; |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + /** |
|
| 539 | + * Get the primary key for the entity. |
|
| 540 | + * |
|
| 541 | + * @return string |
|
| 542 | + */ |
|
| 543 | + public function getKeyName() |
|
| 544 | + { |
|
| 545 | + return $this->primaryKey; |
|
| 546 | + } |
|
| 547 | 547 | |
| 548 | - /** |
|
| 549 | - * Set the primary key for the entity. |
|
| 550 | - * |
|
| 551 | - * @param $key |
|
| 552 | - * @return void |
|
| 553 | - */ |
|
| 554 | - public function setKeyName($key) |
|
| 555 | - { |
|
| 556 | - $this->primaryKey = $key; |
|
| 557 | - } |
|
| 548 | + /** |
|
| 549 | + * Set the primary key for the entity. |
|
| 550 | + * |
|
| 551 | + * @param $key |
|
| 552 | + * @return void |
|
| 553 | + */ |
|
| 554 | + public function setKeyName($key) |
|
| 555 | + { |
|
| 556 | + $this->primaryKey = $key; |
|
| 557 | + } |
|
| 558 | 558 | |
| 559 | - /** |
|
| 560 | - * Get the table qualified key name. |
|
| 561 | - * |
|
| 562 | - * @return string |
|
| 563 | - */ |
|
| 564 | - public function getQualifiedKeyName() |
|
| 565 | - { |
|
| 566 | - return $this->getTable() . '.' . $this->getKeyName(); |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * Get the number of models to return per page. |
|
| 571 | - * |
|
| 572 | - * @return int |
|
| 573 | - */ |
|
| 574 | - public function getPerPage() |
|
| 575 | - { |
|
| 576 | - return $this->perPage; |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - /** |
|
| 580 | - * Set the number of models to return per page. |
|
| 581 | - * |
|
| 582 | - * @param int $perPage |
|
| 583 | - * @return void |
|
| 584 | - */ |
|
| 585 | - public function setPerPage($perPage) |
|
| 586 | - { |
|
| 587 | - $this->perPage = $perPage; |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - /** |
|
| 591 | - * Determine if the entity uses get. |
|
| 592 | - * |
|
| 593 | - * @return bool |
|
| 594 | - */ |
|
| 595 | - public function usesTimestamps() |
|
| 596 | - { |
|
| 597 | - return $this->timestamps; |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - /** |
|
| 601 | - * Determine if the entity uses soft deletes |
|
| 602 | - * |
|
| 603 | - * @return bool |
|
| 604 | - */ |
|
| 605 | - public function usesSoftDeletes() |
|
| 606 | - { |
|
| 607 | - return $this->softDeletes; |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - /** |
|
| 611 | - * Get the 'created_at' column name |
|
| 612 | - * |
|
| 613 | - * @return string |
|
| 614 | - */ |
|
| 615 | - public function getCreatedAtColumn() |
|
| 616 | - { |
|
| 617 | - return $this->createdAtColumn; |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - /** |
|
| 621 | - * Get the 'updated_at' column name |
|
| 622 | - * |
|
| 623 | - * @return string |
|
| 624 | - */ |
|
| 625 | - public function getUpdatedAtColumn() |
|
| 626 | - { |
|
| 627 | - return $this->updatedAtColumn; |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - /** |
|
| 631 | - * Get the deleted_at column |
|
| 632 | - * |
|
| 633 | - * @return string |
|
| 634 | - */ |
|
| 635 | - public function getQualifiedDeletedAtColumn() |
|
| 636 | - { |
|
| 637 | - return $this->deletedAtColumn; |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - /** |
|
| 641 | - * Get the default foreign key name for the model. |
|
| 642 | - * |
|
| 643 | - * @return string |
|
| 644 | - */ |
|
| 645 | - public function getForeignKey() |
|
| 646 | - { |
|
| 647 | - return snake_case(class_basename($this->getClass())) . '_id'; |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - /** |
|
| 651 | - * Define a one-to-one relationship. |
|
| 652 | - * |
|
| 653 | - * @param $entity |
|
| 654 | - * @param string $relatedClass entity class |
|
| 655 | - * @param string $foreignKey |
|
| 656 | - * @param string $localKey |
|
| 657 | - * @throws MappingException |
|
| 658 | - * @return \Analogue\ORM\Relationships\HasOne |
|
| 659 | - */ |
|
| 660 | - public function hasOne($entity, $relatedClass, $foreignKey = null, $localKey = null) |
|
| 661 | - { |
|
| 662 | - $foreignKey = $foreignKey ?: $this->getForeignKey(); |
|
| 663 | - |
|
| 664 | - $relatedMapper = $this->manager->mapper($relatedClass); |
|
| 665 | - |
|
| 666 | - $relatedMap = $relatedMapper->getEntityMap(); |
|
| 667 | - |
|
| 668 | - $localKey = $localKey ?: $this->getKeyName(); |
|
| 669 | - |
|
| 670 | - return new HasOne($relatedMapper, $entity, $relatedMap->getTable() . '.' . $foreignKey, $localKey); |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - /** |
|
| 674 | - * Define a polymorphic one-to-one relationship. |
|
| 675 | - * |
|
| 676 | - * @param mixed $entity |
|
| 677 | - * @param string $related |
|
| 678 | - * @param string $name |
|
| 679 | - * @param string|null $type |
|
| 680 | - * @param string|null $id |
|
| 681 | - * @param string|null $localKey |
|
| 682 | - * @throws MappingException |
|
| 683 | - * @return \Analogue\ORM\Relationships\MorphOne |
|
| 684 | - */ |
|
| 685 | - public function morphOne($entity, $related, $name, $type = null, $id = null, $localKey = null) |
|
| 686 | - { |
|
| 687 | - list($type, $id) = $this->getMorphs($name, $type, $id); |
|
| 688 | - |
|
| 689 | - $localKey = $localKey ?: $this->getKeyName(); |
|
| 690 | - |
|
| 691 | - $relatedMapper = $this->manager->mapper($related); |
|
| 692 | - |
|
| 693 | - $table = $relatedMapper->getEntityMap()->getTable(); |
|
| 559 | + /** |
|
| 560 | + * Get the table qualified key name. |
|
| 561 | + * |
|
| 562 | + * @return string |
|
| 563 | + */ |
|
| 564 | + public function getQualifiedKeyName() |
|
| 565 | + { |
|
| 566 | + return $this->getTable() . '.' . $this->getKeyName(); |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * Get the number of models to return per page. |
|
| 571 | + * |
|
| 572 | + * @return int |
|
| 573 | + */ |
|
| 574 | + public function getPerPage() |
|
| 575 | + { |
|
| 576 | + return $this->perPage; |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + /** |
|
| 580 | + * Set the number of models to return per page. |
|
| 581 | + * |
|
| 582 | + * @param int $perPage |
|
| 583 | + * @return void |
|
| 584 | + */ |
|
| 585 | + public function setPerPage($perPage) |
|
| 586 | + { |
|
| 587 | + $this->perPage = $perPage; |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + /** |
|
| 591 | + * Determine if the entity uses get. |
|
| 592 | + * |
|
| 593 | + * @return bool |
|
| 594 | + */ |
|
| 595 | + public function usesTimestamps() |
|
| 596 | + { |
|
| 597 | + return $this->timestamps; |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + /** |
|
| 601 | + * Determine if the entity uses soft deletes |
|
| 602 | + * |
|
| 603 | + * @return bool |
|
| 604 | + */ |
|
| 605 | + public function usesSoftDeletes() |
|
| 606 | + { |
|
| 607 | + return $this->softDeletes; |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + /** |
|
| 611 | + * Get the 'created_at' column name |
|
| 612 | + * |
|
| 613 | + * @return string |
|
| 614 | + */ |
|
| 615 | + public function getCreatedAtColumn() |
|
| 616 | + { |
|
| 617 | + return $this->createdAtColumn; |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + /** |
|
| 621 | + * Get the 'updated_at' column name |
|
| 622 | + * |
|
| 623 | + * @return string |
|
| 624 | + */ |
|
| 625 | + public function getUpdatedAtColumn() |
|
| 626 | + { |
|
| 627 | + return $this->updatedAtColumn; |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + /** |
|
| 631 | + * Get the deleted_at column |
|
| 632 | + * |
|
| 633 | + * @return string |
|
| 634 | + */ |
|
| 635 | + public function getQualifiedDeletedAtColumn() |
|
| 636 | + { |
|
| 637 | + return $this->deletedAtColumn; |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + /** |
|
| 641 | + * Get the default foreign key name for the model. |
|
| 642 | + * |
|
| 643 | + * @return string |
|
| 644 | + */ |
|
| 645 | + public function getForeignKey() |
|
| 646 | + { |
|
| 647 | + return snake_case(class_basename($this->getClass())) . '_id'; |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + /** |
|
| 651 | + * Define a one-to-one relationship. |
|
| 652 | + * |
|
| 653 | + * @param $entity |
|
| 654 | + * @param string $relatedClass entity class |
|
| 655 | + * @param string $foreignKey |
|
| 656 | + * @param string $localKey |
|
| 657 | + * @throws MappingException |
|
| 658 | + * @return \Analogue\ORM\Relationships\HasOne |
|
| 659 | + */ |
|
| 660 | + public function hasOne($entity, $relatedClass, $foreignKey = null, $localKey = null) |
|
| 661 | + { |
|
| 662 | + $foreignKey = $foreignKey ?: $this->getForeignKey(); |
|
| 663 | + |
|
| 664 | + $relatedMapper = $this->manager->mapper($relatedClass); |
|
| 665 | + |
|
| 666 | + $relatedMap = $relatedMapper->getEntityMap(); |
|
| 667 | + |
|
| 668 | + $localKey = $localKey ?: $this->getKeyName(); |
|
| 669 | + |
|
| 670 | + return new HasOne($relatedMapper, $entity, $relatedMap->getTable() . '.' . $foreignKey, $localKey); |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + /** |
|
| 674 | + * Define a polymorphic one-to-one relationship. |
|
| 675 | + * |
|
| 676 | + * @param mixed $entity |
|
| 677 | + * @param string $related |
|
| 678 | + * @param string $name |
|
| 679 | + * @param string|null $type |
|
| 680 | + * @param string|null $id |
|
| 681 | + * @param string|null $localKey |
|
| 682 | + * @throws MappingException |
|
| 683 | + * @return \Analogue\ORM\Relationships\MorphOne |
|
| 684 | + */ |
|
| 685 | + public function morphOne($entity, $related, $name, $type = null, $id = null, $localKey = null) |
|
| 686 | + { |
|
| 687 | + list($type, $id) = $this->getMorphs($name, $type, $id); |
|
| 688 | + |
|
| 689 | + $localKey = $localKey ?: $this->getKeyName(); |
|
| 690 | + |
|
| 691 | + $relatedMapper = $this->manager->mapper($related); |
|
| 692 | + |
|
| 693 | + $table = $relatedMapper->getEntityMap()->getTable(); |
|
| 694 | 694 | |
| 695 | - return new MorphOne($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey); |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - /** |
|
| 699 | - * Define an inverse one-to-one or many relationship. |
|
| 700 | - * |
|
| 701 | - * @param mixed $entity |
|
| 702 | - * @param string $related |
|
| 703 | - * @param string|null $foreignKey |
|
| 704 | - * @param string|null $otherKey |
|
| 705 | - * @param string|null $relation |
|
| 706 | - * @throws MappingException |
|
| 707 | - * @return \Analogue\ORM\Relationships\BelongsTo |
|
| 708 | - */ |
|
| 709 | - public function belongsTo($entity, $related, $foreignKey = null, $otherKey = null, $relation = null) |
|
| 710 | - { |
|
| 711 | - // If no relation name was given, we will use this debug backtrace to extract |
|
| 712 | - // the calling method's name and use that as the relationship name as most |
|
| 713 | - // of the time this will be what we desire to use for the relationships. |
|
| 714 | - if (is_null($relation)) { |
|
| 715 | - list(, $caller) = debug_backtrace(false); |
|
| 716 | - |
|
| 717 | - $relation = $caller['function']; |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - // If no foreign key was supplied, we can use a backtrace to guess the proper |
|
| 721 | - // foreign key name by using the name of the relationship function, which |
|
| 722 | - // when combined with an "_id" should conventionally match the columns. |
|
| 723 | - if (is_null($foreignKey)) { |
|
| 724 | - $foreignKey = snake_case($relation) . '_id'; |
|
| 725 | - } |
|
| 726 | - |
|
| 727 | - $relatedMapper = $this->manager->mapper($related); |
|
| 728 | - |
|
| 729 | - $otherKey = $otherKey ?: $relatedMapper->getEntityMap()->getKeyName(); |
|
| 730 | - |
|
| 731 | - return new BelongsTo($relatedMapper, $entity, $foreignKey, $otherKey, $relation); |
|
| 732 | - } |
|
| 733 | - |
|
| 734 | - /** |
|
| 735 | - * Define a polymorphic, inverse one-to-one or many relationship. |
|
| 736 | - * |
|
| 737 | - * @param mixed $entity |
|
| 738 | - * @param string|null $name |
|
| 739 | - * @param string|null $type |
|
| 740 | - * @param string|null $id |
|
| 741 | - * @throws MappingException |
|
| 742 | - * @return \Analogue\ORM\Relationships\MorphTo |
|
| 743 | - */ |
|
| 744 | - public function morphTo($entity, $name = null, $type = null, $id = null) |
|
| 745 | - { |
|
| 746 | - // If no name is provided, we will use the backtrace to get the function name |
|
| 747 | - // since that is most likely the name of the polymorphic interface. We can |
|
| 748 | - // use that to get both the class and foreign key that will be utilized. |
|
| 749 | - if (is_null($name)) { |
|
| 750 | - list(, $caller) = debug_backtrace(false); |
|
| 751 | - |
|
| 752 | - $name = snake_case($caller['function']); |
|
| 753 | - } |
|
| 754 | - |
|
| 755 | - list($type, $id) = $this->getMorphs($name, $type, $id); |
|
| 695 | + return new MorphOne($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey); |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + /** |
|
| 699 | + * Define an inverse one-to-one or many relationship. |
|
| 700 | + * |
|
| 701 | + * @param mixed $entity |
|
| 702 | + * @param string $related |
|
| 703 | + * @param string|null $foreignKey |
|
| 704 | + * @param string|null $otherKey |
|
| 705 | + * @param string|null $relation |
|
| 706 | + * @throws MappingException |
|
| 707 | + * @return \Analogue\ORM\Relationships\BelongsTo |
|
| 708 | + */ |
|
| 709 | + public function belongsTo($entity, $related, $foreignKey = null, $otherKey = null, $relation = null) |
|
| 710 | + { |
|
| 711 | + // If no relation name was given, we will use this debug backtrace to extract |
|
| 712 | + // the calling method's name and use that as the relationship name as most |
|
| 713 | + // of the time this will be what we desire to use for the relationships. |
|
| 714 | + if (is_null($relation)) { |
|
| 715 | + list(, $caller) = debug_backtrace(false); |
|
| 716 | + |
|
| 717 | + $relation = $caller['function']; |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + // If no foreign key was supplied, we can use a backtrace to guess the proper |
|
| 721 | + // foreign key name by using the name of the relationship function, which |
|
| 722 | + // when combined with an "_id" should conventionally match the columns. |
|
| 723 | + if (is_null($foreignKey)) { |
|
| 724 | + $foreignKey = snake_case($relation) . '_id'; |
|
| 725 | + } |
|
| 726 | + |
|
| 727 | + $relatedMapper = $this->manager->mapper($related); |
|
| 728 | + |
|
| 729 | + $otherKey = $otherKey ?: $relatedMapper->getEntityMap()->getKeyName(); |
|
| 730 | + |
|
| 731 | + return new BelongsTo($relatedMapper, $entity, $foreignKey, $otherKey, $relation); |
|
| 732 | + } |
|
| 733 | + |
|
| 734 | + /** |
|
| 735 | + * Define a polymorphic, inverse one-to-one or many relationship. |
|
| 736 | + * |
|
| 737 | + * @param mixed $entity |
|
| 738 | + * @param string|null $name |
|
| 739 | + * @param string|null $type |
|
| 740 | + * @param string|null $id |
|
| 741 | + * @throws MappingException |
|
| 742 | + * @return \Analogue\ORM\Relationships\MorphTo |
|
| 743 | + */ |
|
| 744 | + public function morphTo($entity, $name = null, $type = null, $id = null) |
|
| 745 | + { |
|
| 746 | + // If no name is provided, we will use the backtrace to get the function name |
|
| 747 | + // since that is most likely the name of the polymorphic interface. We can |
|
| 748 | + // use that to get both the class and foreign key that will be utilized. |
|
| 749 | + if (is_null($name)) { |
|
| 750 | + list(, $caller) = debug_backtrace(false); |
|
| 751 | + |
|
| 752 | + $name = snake_case($caller['function']); |
|
| 753 | + } |
|
| 754 | + |
|
| 755 | + list($type, $id) = $this->getMorphs($name, $type, $id); |
|
| 756 | 756 | |
| 757 | - $mapper = $this->manager->mapper(get_class($entity)); |
|
| 757 | + $mapper = $this->manager->mapper(get_class($entity)); |
|
| 758 | 758 | |
| 759 | - // If the type value is null it is probably safe to assume we're eager loading |
|
| 760 | - // the relationship. When that is the case we will pass in a dummy query as |
|
| 761 | - // there are multiple types in the morph and we can't use single queries. |
|
| 762 | - $factory = new Factory; |
|
| 763 | - $wrapper = $factory->make($entity); |
|
| 759 | + // If the type value is null it is probably safe to assume we're eager loading |
|
| 760 | + // the relationship. When that is the case we will pass in a dummy query as |
|
| 761 | + // there are multiple types in the morph and we can't use single queries. |
|
| 762 | + $factory = new Factory; |
|
| 763 | + $wrapper = $factory->make($entity); |
|
| 764 | 764 | |
| 765 | - if (is_null($class = $wrapper->getEntityAttribute($type))) { |
|
| 766 | - return new MorphTo( |
|
| 767 | - $mapper, $entity, $id, null, $type, $name |
|
| 768 | - ); |
|
| 769 | - } |
|
| 770 | - |
|
| 771 | - // If we are not eager loading the relationship we will essentially treat this |
|
| 772 | - // as a belongs-to style relationship since morph-to extends that class and |
|
| 773 | - // we will pass in the appropriate values so that it behaves as expected. |
|
| 774 | - else { |
|
| 775 | - $class = $this->manager->getInverseMorphMap($class); |
|
| 776 | - $relatedMapper = $this->manager->mapper($class); |
|
| 777 | - |
|
| 778 | - $foreignKey = $relatedMapper->getEntityMap()->getKeyName(); |
|
| 765 | + if (is_null($class = $wrapper->getEntityAttribute($type))) { |
|
| 766 | + return new MorphTo( |
|
| 767 | + $mapper, $entity, $id, null, $type, $name |
|
| 768 | + ); |
|
| 769 | + } |
|
| 770 | + |
|
| 771 | + // If we are not eager loading the relationship we will essentially treat this |
|
| 772 | + // as a belongs-to style relationship since morph-to extends that class and |
|
| 773 | + // we will pass in the appropriate values so that it behaves as expected. |
|
| 774 | + else { |
|
| 775 | + $class = $this->manager->getInverseMorphMap($class); |
|
| 776 | + $relatedMapper = $this->manager->mapper($class); |
|
| 777 | + |
|
| 778 | + $foreignKey = $relatedMapper->getEntityMap()->getKeyName(); |
|
| 779 | 779 | |
| 780 | - return new MorphTo( |
|
| 781 | - $relatedMapper, $entity, $id, $foreignKey, $type, $name |
|
| 782 | - ); |
|
| 783 | - } |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - /** |
|
| 787 | - * Define a one-to-many relationship. |
|
| 788 | - * |
|
| 789 | - * @param mixed $entity |
|
| 790 | - * @param string $related |
|
| 791 | - * @param string|null $foreignKey |
|
| 792 | - * @param string|null $localKey |
|
| 793 | - * @throws MappingException |
|
| 794 | - * @return \Analogue\ORM\Relationships\HasMany |
|
| 795 | - */ |
|
| 796 | - public function hasMany($entity, $related, $foreignKey = null, $localKey = null) |
|
| 797 | - { |
|
| 798 | - $foreignKey = $foreignKey ?: $this->getForeignKey(); |
|
| 799 | - |
|
| 800 | - $relatedMapper = $this->manager->mapper($related); |
|
| 801 | - |
|
| 802 | - $table = $relatedMapper->getEntityMap()->getTable() . '.' . $foreignKey; |
|
| 803 | - |
|
| 804 | - $localKey = $localKey ?: $this->getKeyName(); |
|
| 805 | - |
|
| 806 | - return new HasMany($relatedMapper, $entity, $table, $localKey); |
|
| 807 | - } |
|
| 808 | - |
|
| 809 | - /** |
|
| 810 | - * Define a has-many-through relationship. |
|
| 811 | - * |
|
| 812 | - * @param mixed $entity |
|
| 813 | - * @param string $related |
|
| 814 | - * @param string $through |
|
| 815 | - * @param string|null $firstKey |
|
| 816 | - * @param string|null $secondKey |
|
| 817 | - * @throws MappingException |
|
| 818 | - * @return \Analogue\ORM\Relationships\HasManyThrough |
|
| 819 | - */ |
|
| 820 | - public function hasManyThrough($entity, $related, $through, $firstKey = null, $secondKey = null) |
|
| 821 | - { |
|
| 822 | - $relatedMapper = $this->manager->mapper($related); |
|
| 823 | - |
|
| 824 | - $throughMapper = $this->manager->mapper($through); |
|
| 825 | - |
|
| 826 | - |
|
| 827 | - $firstKey = $firstKey ?: $this->getForeignKey(); |
|
| 828 | - |
|
| 829 | - $throughMap = $throughMapper->getEntityMap(); |
|
| 830 | - |
|
| 831 | - $secondKey = $secondKey ?: $throughMap->getForeignKey(); |
|
| 832 | - |
|
| 833 | - return new HasManyThrough($relatedMapper, $entity, $throughMap, $firstKey, $secondKey); |
|
| 834 | - } |
|
| 835 | - |
|
| 836 | - /** |
|
| 837 | - * Define a polymorphic one-to-many relationship. |
|
| 838 | - * |
|
| 839 | - * @param mixed $entity |
|
| 840 | - * @param string $related |
|
| 841 | - * @param string $name |
|
| 842 | - * @param string|null $type |
|
| 843 | - * @param string|null $id |
|
| 844 | - * @param string|null $localKey |
|
| 845 | - * @return \Analogue\ORM\Relationships\MorphMany |
|
| 846 | - */ |
|
| 847 | - public function morphMany($entity, $related, $name, $type = null, $id = null, $localKey = null) |
|
| 848 | - { |
|
| 849 | - // Here we will gather up the morph type and ID for the relationship so that we |
|
| 850 | - // can properly query the intermediate table of a relation. Finally, we will |
|
| 851 | - // get the table and create the relationship instances for the developers. |
|
| 852 | - list($type, $id) = $this->getMorphs($name, $type, $id); |
|
| 853 | - |
|
| 854 | - $relatedMapper = $this->manager->mapper($related); |
|
| 855 | - |
|
| 856 | - $table = $relatedMapper->getEntityMap()->getTable(); |
|
| 857 | - |
|
| 858 | - $localKey = $localKey ?: $this->getKeyName(); |
|
| 780 | + return new MorphTo( |
|
| 781 | + $relatedMapper, $entity, $id, $foreignKey, $type, $name |
|
| 782 | + ); |
|
| 783 | + } |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + /** |
|
| 787 | + * Define a one-to-many relationship. |
|
| 788 | + * |
|
| 789 | + * @param mixed $entity |
|
| 790 | + * @param string $related |
|
| 791 | + * @param string|null $foreignKey |
|
| 792 | + * @param string|null $localKey |
|
| 793 | + * @throws MappingException |
|
| 794 | + * @return \Analogue\ORM\Relationships\HasMany |
|
| 795 | + */ |
|
| 796 | + public function hasMany($entity, $related, $foreignKey = null, $localKey = null) |
|
| 797 | + { |
|
| 798 | + $foreignKey = $foreignKey ?: $this->getForeignKey(); |
|
| 799 | + |
|
| 800 | + $relatedMapper = $this->manager->mapper($related); |
|
| 801 | + |
|
| 802 | + $table = $relatedMapper->getEntityMap()->getTable() . '.' . $foreignKey; |
|
| 803 | + |
|
| 804 | + $localKey = $localKey ?: $this->getKeyName(); |
|
| 805 | + |
|
| 806 | + return new HasMany($relatedMapper, $entity, $table, $localKey); |
|
| 807 | + } |
|
| 808 | + |
|
| 809 | + /** |
|
| 810 | + * Define a has-many-through relationship. |
|
| 811 | + * |
|
| 812 | + * @param mixed $entity |
|
| 813 | + * @param string $related |
|
| 814 | + * @param string $through |
|
| 815 | + * @param string|null $firstKey |
|
| 816 | + * @param string|null $secondKey |
|
| 817 | + * @throws MappingException |
|
| 818 | + * @return \Analogue\ORM\Relationships\HasManyThrough |
|
| 819 | + */ |
|
| 820 | + public function hasManyThrough($entity, $related, $through, $firstKey = null, $secondKey = null) |
|
| 821 | + { |
|
| 822 | + $relatedMapper = $this->manager->mapper($related); |
|
| 823 | + |
|
| 824 | + $throughMapper = $this->manager->mapper($through); |
|
| 825 | + |
|
| 826 | + |
|
| 827 | + $firstKey = $firstKey ?: $this->getForeignKey(); |
|
| 828 | + |
|
| 829 | + $throughMap = $throughMapper->getEntityMap(); |
|
| 830 | + |
|
| 831 | + $secondKey = $secondKey ?: $throughMap->getForeignKey(); |
|
| 832 | + |
|
| 833 | + return new HasManyThrough($relatedMapper, $entity, $throughMap, $firstKey, $secondKey); |
|
| 834 | + } |
|
| 835 | + |
|
| 836 | + /** |
|
| 837 | + * Define a polymorphic one-to-many relationship. |
|
| 838 | + * |
|
| 839 | + * @param mixed $entity |
|
| 840 | + * @param string $related |
|
| 841 | + * @param string $name |
|
| 842 | + * @param string|null $type |
|
| 843 | + * @param string|null $id |
|
| 844 | + * @param string|null $localKey |
|
| 845 | + * @return \Analogue\ORM\Relationships\MorphMany |
|
| 846 | + */ |
|
| 847 | + public function morphMany($entity, $related, $name, $type = null, $id = null, $localKey = null) |
|
| 848 | + { |
|
| 849 | + // Here we will gather up the morph type and ID for the relationship so that we |
|
| 850 | + // can properly query the intermediate table of a relation. Finally, we will |
|
| 851 | + // get the table and create the relationship instances for the developers. |
|
| 852 | + list($type, $id) = $this->getMorphs($name, $type, $id); |
|
| 853 | + |
|
| 854 | + $relatedMapper = $this->manager->mapper($related); |
|
| 855 | + |
|
| 856 | + $table = $relatedMapper->getEntityMap()->getTable(); |
|
| 857 | + |
|
| 858 | + $localKey = $localKey ?: $this->getKeyName(); |
|
| 859 | 859 | |
| 860 | - return new MorphMany($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey); |
|
| 861 | - } |
|
| 862 | - |
|
| 863 | - /** |
|
| 864 | - * Define a many-to-many relationship. |
|
| 865 | - * |
|
| 866 | - * @param mixed $entity |
|
| 867 | - * @param string $related |
|
| 868 | - * @param string|null $table |
|
| 869 | - * @param string|null $foreignKey |
|
| 870 | - * @param string|null $otherKey |
|
| 871 | - * @param string|null $relation |
|
| 872 | - * @throws MappingException |
|
| 873 | - * @return \Analogue\ORM\Relationships\BelongsToMany |
|
| 874 | - */ |
|
| 875 | - public function belongsToMany($entity, $related, $table = null, $foreignKey = null, $otherKey = null, $relation = null) |
|
| 876 | - { |
|
| 877 | - // If no relationship name was passed, we will pull backtraces to get the |
|
| 878 | - // name of the calling function. We will use that function name as the |
|
| 879 | - // title of this relation since that is a great convention to apply. |
|
| 880 | - if (is_null($relation)) { |
|
| 881 | - $relation = $this->getBelongsToManyCaller(); |
|
| 882 | - } |
|
| 883 | - |
|
| 884 | - // First, we'll need to determine the foreign key and "other key" for the |
|
| 885 | - // relationship. Once we have determined the keys we'll make the query |
|
| 886 | - // instances as well as the relationship instances we need for this. |
|
| 887 | - $foreignKey = $foreignKey ?: $this->getForeignKey(); |
|
| 888 | - |
|
| 889 | - $relatedMapper = $this->manager->mapper($related); |
|
| 890 | - |
|
| 891 | - $relatedMap = $relatedMapper->getEntityMap(); |
|
| 892 | - |
|
| 893 | - $otherKey = $otherKey ?: $relatedMap->getForeignKey(); |
|
| 894 | - |
|
| 895 | - // If no table name was provided, we can guess it by concatenating the two |
|
| 896 | - // models using underscores in alphabetical order. The two model names |
|
| 897 | - // are transformed to snake case from their default CamelCase also. |
|
| 898 | - if (is_null($table)) { |
|
| 899 | - $table = $this->joiningTable($relatedMap); |
|
| 900 | - } |
|
| 901 | - |
|
| 902 | - return new BelongsToMany($relatedMapper, $entity, $table, $foreignKey, $otherKey, $relation); |
|
| 903 | - } |
|
| 904 | - |
|
| 905 | - /** |
|
| 906 | - * Define a polymorphic many-to-many relationship. |
|
| 907 | - * |
|
| 908 | - * @param mixed $entity |
|
| 909 | - * @param string $related |
|
| 910 | - * @param string $name |
|
| 911 | - * @param string|null $table |
|
| 912 | - * @param string|null $foreignKey |
|
| 913 | - * @param string|null $otherKey |
|
| 914 | - * @param bool $inverse |
|
| 915 | - * @throws MappingException |
|
| 916 | - * @return \Analogue\ORM\Relationships\MorphToMany |
|
| 917 | - */ |
|
| 918 | - public function morphToMany($entity, $related, $name, $table = null, $foreignKey = null, $otherKey = null, $inverse = false) |
|
| 919 | - { |
|
| 920 | - $caller = $this->getBelongsToManyCaller(); |
|
| 921 | - |
|
| 922 | - // First, we will need to determine the foreign key and "other key" for the |
|
| 923 | - // relationship. Once we have determined the keys we will make the query |
|
| 924 | - // instances, as well as the relationship instances we need for these. |
|
| 925 | - $foreignKey = $foreignKey ?: $name . '_id'; |
|
| 926 | - |
|
| 927 | - $relatedMapper = $this->manager->mapper($related); |
|
| 928 | - |
|
| 929 | - $otherKey = $otherKey ?: $relatedMapper->getEntityMap()->getForeignKey(); |
|
| 930 | - |
|
| 931 | - $table = $table ?: str_plural($name); |
|
| 932 | - |
|
| 933 | - return new MorphToMany($relatedMapper, $entity, $name, $table, $foreignKey, $otherKey, $caller, $inverse); |
|
| 934 | - } |
|
| 935 | - |
|
| 936 | - /** |
|
| 937 | - * Define a polymorphic, inverse many-to-many relationship. |
|
| 938 | - * |
|
| 939 | - * @param mixed $entity |
|
| 940 | - * @param string $related |
|
| 941 | - * @param string $name |
|
| 942 | - * @param string|null $table |
|
| 943 | - * @param string|null $foreignKey |
|
| 944 | - * @param string|null $otherKey |
|
| 945 | - * @throws MappingException |
|
| 946 | - * @return \Analogue\ORM\Relationships\MorphToMany |
|
| 947 | - */ |
|
| 948 | - public function morphedByMany($entity, $related, $name, $table = null, $foreignKey = null, $otherKey = null) |
|
| 949 | - { |
|
| 950 | - $foreignKey = $foreignKey ?: $this->getForeignKey(); |
|
| 951 | - |
|
| 952 | - // For the inverse of the polymorphic many-to-many relations, we will change |
|
| 953 | - // the way we determine the foreign and other keys, as it is the opposite |
|
| 954 | - // of the morph-to-many method since we're figuring out these inverses. |
|
| 955 | - $otherKey = $otherKey ?: $name . '_id'; |
|
| 956 | - |
|
| 957 | - return $this->morphToMany($entity, $related, $name, $table, $foreignKey, $otherKey, true); |
|
| 958 | - } |
|
| 959 | - |
|
| 960 | - /** |
|
| 961 | - * Get the relationship name of the belongs to many. |
|
| 962 | - * |
|
| 963 | - * @return string |
|
| 964 | - */ |
|
| 965 | - protected function getBelongsToManyCaller() |
|
| 966 | - { |
|
| 967 | - $self = __FUNCTION__; |
|
| 968 | - |
|
| 969 | - $caller = array_first(debug_backtrace(false), function ($key, $trace) use ($self) { |
|
| 970 | - $caller = $trace['function']; |
|
| 971 | - |
|
| 972 | - return (!in_array($caller, EntityMap::$manyMethods) && $caller != $self); |
|
| 973 | - }); |
|
| 974 | - |
|
| 975 | - return !is_null($caller) ? $caller['function'] : null; |
|
| 976 | - } |
|
| 977 | - |
|
| 978 | - /** |
|
| 979 | - * Get the joining table name for a many-to-many relation. |
|
| 980 | - * |
|
| 981 | - * @param EntityMap $relatedMap |
|
| 982 | - * @return string |
|
| 983 | - */ |
|
| 984 | - public function joiningTable($relatedMap) |
|
| 985 | - { |
|
| 986 | - // The joining table name, by convention, is simply the snake cased models |
|
| 987 | - // sorted alphabetically and concatenated with an underscore, so we can |
|
| 988 | - // just sort the models and join them together to get the table name. |
|
| 989 | - $base = $this->getTable(); |
|
| 990 | - |
|
| 991 | - $related = $relatedMap->getTable(); |
|
| 992 | - |
|
| 993 | - $tables = [$related, $base]; |
|
| 994 | - |
|
| 995 | - // Now that we have the model names in an array we can just sort them and |
|
| 996 | - // use the implode function to join them together with an underscores, |
|
| 997 | - // which is typically used by convention within the database system. |
|
| 998 | - sort($tables); |
|
| 999 | - |
|
| 1000 | - return strtolower(implode('_', $tables)); |
|
| 1001 | - } |
|
| 1002 | - |
|
| 1003 | - /** |
|
| 1004 | - * Get the polymorphic relationship columns. |
|
| 1005 | - * |
|
| 1006 | - * @param string $name |
|
| 1007 | - * @param string $type |
|
| 1008 | - * @param string $id |
|
| 1009 | - * @return string[] |
|
| 1010 | - */ |
|
| 1011 | - protected function getMorphs($name, $type, $id) |
|
| 1012 | - { |
|
| 1013 | - $type = $type ?: $name . '_type'; |
|
| 1014 | - |
|
| 1015 | - $id = $id ?: $name . '_id'; |
|
| 1016 | - |
|
| 1017 | - return [$type, $id]; |
|
| 1018 | - } |
|
| 1019 | - |
|
| 1020 | - /** |
|
| 1021 | - * Get the class name for polymorphic relations. |
|
| 1022 | - * |
|
| 1023 | - * @return string |
|
| 1024 | - */ |
|
| 1025 | - public function getMorphClass() |
|
| 1026 | - { |
|
| 1027 | - $morphClass = $this->manager->getMorphMap($this->getClass()); |
|
| 1028 | - return $this->morphClass ?: $morphClass; |
|
| 1029 | - } |
|
| 860 | + return new MorphMany($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey); |
|
| 861 | + } |
|
| 862 | + |
|
| 863 | + /** |
|
| 864 | + * Define a many-to-many relationship. |
|
| 865 | + * |
|
| 866 | + * @param mixed $entity |
|
| 867 | + * @param string $related |
|
| 868 | + * @param string|null $table |
|
| 869 | + * @param string|null $foreignKey |
|
| 870 | + * @param string|null $otherKey |
|
| 871 | + * @param string|null $relation |
|
| 872 | + * @throws MappingException |
|
| 873 | + * @return \Analogue\ORM\Relationships\BelongsToMany |
|
| 874 | + */ |
|
| 875 | + public function belongsToMany($entity, $related, $table = null, $foreignKey = null, $otherKey = null, $relation = null) |
|
| 876 | + { |
|
| 877 | + // If no relationship name was passed, we will pull backtraces to get the |
|
| 878 | + // name of the calling function. We will use that function name as the |
|
| 879 | + // title of this relation since that is a great convention to apply. |
|
| 880 | + if (is_null($relation)) { |
|
| 881 | + $relation = $this->getBelongsToManyCaller(); |
|
| 882 | + } |
|
| 883 | + |
|
| 884 | + // First, we'll need to determine the foreign key and "other key" for the |
|
| 885 | + // relationship. Once we have determined the keys we'll make the query |
|
| 886 | + // instances as well as the relationship instances we need for this. |
|
| 887 | + $foreignKey = $foreignKey ?: $this->getForeignKey(); |
|
| 888 | + |
|
| 889 | + $relatedMapper = $this->manager->mapper($related); |
|
| 890 | + |
|
| 891 | + $relatedMap = $relatedMapper->getEntityMap(); |
|
| 892 | + |
|
| 893 | + $otherKey = $otherKey ?: $relatedMap->getForeignKey(); |
|
| 894 | + |
|
| 895 | + // If no table name was provided, we can guess it by concatenating the two |
|
| 896 | + // models using underscores in alphabetical order. The two model names |
|
| 897 | + // are transformed to snake case from their default CamelCase also. |
|
| 898 | + if (is_null($table)) { |
|
| 899 | + $table = $this->joiningTable($relatedMap); |
|
| 900 | + } |
|
| 901 | + |
|
| 902 | + return new BelongsToMany($relatedMapper, $entity, $table, $foreignKey, $otherKey, $relation); |
|
| 903 | + } |
|
| 904 | + |
|
| 905 | + /** |
|
| 906 | + * Define a polymorphic many-to-many relationship. |
|
| 907 | + * |
|
| 908 | + * @param mixed $entity |
|
| 909 | + * @param string $related |
|
| 910 | + * @param string $name |
|
| 911 | + * @param string|null $table |
|
| 912 | + * @param string|null $foreignKey |
|
| 913 | + * @param string|null $otherKey |
|
| 914 | + * @param bool $inverse |
|
| 915 | + * @throws MappingException |
|
| 916 | + * @return \Analogue\ORM\Relationships\MorphToMany |
|
| 917 | + */ |
|
| 918 | + public function morphToMany($entity, $related, $name, $table = null, $foreignKey = null, $otherKey = null, $inverse = false) |
|
| 919 | + { |
|
| 920 | + $caller = $this->getBelongsToManyCaller(); |
|
| 921 | + |
|
| 922 | + // First, we will need to determine the foreign key and "other key" for the |
|
| 923 | + // relationship. Once we have determined the keys we will make the query |
|
| 924 | + // instances, as well as the relationship instances we need for these. |
|
| 925 | + $foreignKey = $foreignKey ?: $name . '_id'; |
|
| 926 | + |
|
| 927 | + $relatedMapper = $this->manager->mapper($related); |
|
| 928 | + |
|
| 929 | + $otherKey = $otherKey ?: $relatedMapper->getEntityMap()->getForeignKey(); |
|
| 930 | + |
|
| 931 | + $table = $table ?: str_plural($name); |
|
| 932 | + |
|
| 933 | + return new MorphToMany($relatedMapper, $entity, $name, $table, $foreignKey, $otherKey, $caller, $inverse); |
|
| 934 | + } |
|
| 935 | + |
|
| 936 | + /** |
|
| 937 | + * Define a polymorphic, inverse many-to-many relationship. |
|
| 938 | + * |
|
| 939 | + * @param mixed $entity |
|
| 940 | + * @param string $related |
|
| 941 | + * @param string $name |
|
| 942 | + * @param string|null $table |
|
| 943 | + * @param string|null $foreignKey |
|
| 944 | + * @param string|null $otherKey |
|
| 945 | + * @throws MappingException |
|
| 946 | + * @return \Analogue\ORM\Relationships\MorphToMany |
|
| 947 | + */ |
|
| 948 | + public function morphedByMany($entity, $related, $name, $table = null, $foreignKey = null, $otherKey = null) |
|
| 949 | + { |
|
| 950 | + $foreignKey = $foreignKey ?: $this->getForeignKey(); |
|
| 951 | + |
|
| 952 | + // For the inverse of the polymorphic many-to-many relations, we will change |
|
| 953 | + // the way we determine the foreign and other keys, as it is the opposite |
|
| 954 | + // of the morph-to-many method since we're figuring out these inverses. |
|
| 955 | + $otherKey = $otherKey ?: $name . '_id'; |
|
| 956 | + |
|
| 957 | + return $this->morphToMany($entity, $related, $name, $table, $foreignKey, $otherKey, true); |
|
| 958 | + } |
|
| 959 | + |
|
| 960 | + /** |
|
| 961 | + * Get the relationship name of the belongs to many. |
|
| 962 | + * |
|
| 963 | + * @return string |
|
| 964 | + */ |
|
| 965 | + protected function getBelongsToManyCaller() |
|
| 966 | + { |
|
| 967 | + $self = __FUNCTION__; |
|
| 968 | + |
|
| 969 | + $caller = array_first(debug_backtrace(false), function ($key, $trace) use ($self) { |
|
| 970 | + $caller = $trace['function']; |
|
| 971 | + |
|
| 972 | + return (!in_array($caller, EntityMap::$manyMethods) && $caller != $self); |
|
| 973 | + }); |
|
| 974 | + |
|
| 975 | + return !is_null($caller) ? $caller['function'] : null; |
|
| 976 | + } |
|
| 977 | + |
|
| 978 | + /** |
|
| 979 | + * Get the joining table name for a many-to-many relation. |
|
| 980 | + * |
|
| 981 | + * @param EntityMap $relatedMap |
|
| 982 | + * @return string |
|
| 983 | + */ |
|
| 984 | + public function joiningTable($relatedMap) |
|
| 985 | + { |
|
| 986 | + // The joining table name, by convention, is simply the snake cased models |
|
| 987 | + // sorted alphabetically and concatenated with an underscore, so we can |
|
| 988 | + // just sort the models and join them together to get the table name. |
|
| 989 | + $base = $this->getTable(); |
|
| 990 | + |
|
| 991 | + $related = $relatedMap->getTable(); |
|
| 992 | + |
|
| 993 | + $tables = [$related, $base]; |
|
| 994 | + |
|
| 995 | + // Now that we have the model names in an array we can just sort them and |
|
| 996 | + // use the implode function to join them together with an underscores, |
|
| 997 | + // which is typically used by convention within the database system. |
|
| 998 | + sort($tables); |
|
| 999 | + |
|
| 1000 | + return strtolower(implode('_', $tables)); |
|
| 1001 | + } |
|
| 1002 | + |
|
| 1003 | + /** |
|
| 1004 | + * Get the polymorphic relationship columns. |
|
| 1005 | + * |
|
| 1006 | + * @param string $name |
|
| 1007 | + * @param string $type |
|
| 1008 | + * @param string $id |
|
| 1009 | + * @return string[] |
|
| 1010 | + */ |
|
| 1011 | + protected function getMorphs($name, $type, $id) |
|
| 1012 | + { |
|
| 1013 | + $type = $type ?: $name . '_type'; |
|
| 1014 | + |
|
| 1015 | + $id = $id ?: $name . '_id'; |
|
| 1016 | + |
|
| 1017 | + return [$type, $id]; |
|
| 1018 | + } |
|
| 1019 | + |
|
| 1020 | + /** |
|
| 1021 | + * Get the class name for polymorphic relations. |
|
| 1022 | + * |
|
| 1023 | + * @return string |
|
| 1024 | + */ |
|
| 1025 | + public function getMorphClass() |
|
| 1026 | + { |
|
| 1027 | + $morphClass = $this->manager->getMorphMap($this->getClass()); |
|
| 1028 | + return $this->morphClass ?: $morphClass; |
|
| 1029 | + } |
|
| 1030 | 1030 | |
| 1031 | - /** |
|
| 1032 | - * Create a new Entity Collection instance. |
|
| 1033 | - * |
|
| 1034 | - * @param array $entities |
|
| 1035 | - * @return \Analogue\ORM\EntityCollection |
|
| 1036 | - */ |
|
| 1037 | - public function newCollection(array $entities = []) |
|
| 1038 | - { |
|
| 1039 | - return new EntityCollection($entities, $this); |
|
| 1040 | - } |
|
| 1041 | - |
|
| 1042 | - /** |
|
| 1043 | - * Process EntityMap parsing at initialization time |
|
| 1044 | - * |
|
| 1045 | - * @return void |
|
| 1046 | - */ |
|
| 1047 | - public function initialize() |
|
| 1048 | - { |
|
| 1049 | - $userMethods = $this->getCustomMethods(); |
|
| 1050 | - |
|
| 1051 | - // Parse EntityMap for method based relationship |
|
| 1052 | - if (count($userMethods) > 0) { |
|
| 1053 | - $this->relationships = $this->parseMethodsForRelationship($userMethods); |
|
| 1054 | - } |
|
| 1055 | - |
|
| 1056 | - // Parse EntityMap for dynamic relationships |
|
| 1057 | - if (count($this->dynamicRelationships) > 0) { |
|
| 1058 | - $this->relationships = $this->relationships + $this->getDynamicRelationships(); |
|
| 1059 | - } |
|
| 1060 | - } |
|
| 1061 | - |
|
| 1062 | - /** |
|
| 1063 | - * Parse every relationships on the EntityMap and sort |
|
| 1064 | - * them by type. |
|
| 1065 | - * |
|
| 1066 | - * @return void |
|
| 1067 | - */ |
|
| 1068 | - public function boot() |
|
| 1069 | - { |
|
| 1070 | - if (count($this->relationships > 0)) { |
|
| 1071 | - $this->sortRelationshipsByType(); |
|
| 1072 | - } |
|
| 1073 | - } |
|
| 1074 | - |
|
| 1075 | - /** |
|
| 1076 | - * Get Methods that has been added in the child class. |
|
| 1077 | - * |
|
| 1078 | - * @return array |
|
| 1079 | - */ |
|
| 1080 | - protected function getCustomMethods() |
|
| 1081 | - { |
|
| 1082 | - $mapMethods = get_class_methods($this); |
|
| 1083 | - |
|
| 1084 | - $parentsMethods = get_class_methods('Analogue\ORM\EntityMap'); |
|
| 1031 | + /** |
|
| 1032 | + * Create a new Entity Collection instance. |
|
| 1033 | + * |
|
| 1034 | + * @param array $entities |
|
| 1035 | + * @return \Analogue\ORM\EntityCollection |
|
| 1036 | + */ |
|
| 1037 | + public function newCollection(array $entities = []) |
|
| 1038 | + { |
|
| 1039 | + return new EntityCollection($entities, $this); |
|
| 1040 | + } |
|
| 1041 | + |
|
| 1042 | + /** |
|
| 1043 | + * Process EntityMap parsing at initialization time |
|
| 1044 | + * |
|
| 1045 | + * @return void |
|
| 1046 | + */ |
|
| 1047 | + public function initialize() |
|
| 1048 | + { |
|
| 1049 | + $userMethods = $this->getCustomMethods(); |
|
| 1050 | + |
|
| 1051 | + // Parse EntityMap for method based relationship |
|
| 1052 | + if (count($userMethods) > 0) { |
|
| 1053 | + $this->relationships = $this->parseMethodsForRelationship($userMethods); |
|
| 1054 | + } |
|
| 1055 | + |
|
| 1056 | + // Parse EntityMap for dynamic relationships |
|
| 1057 | + if (count($this->dynamicRelationships) > 0) { |
|
| 1058 | + $this->relationships = $this->relationships + $this->getDynamicRelationships(); |
|
| 1059 | + } |
|
| 1060 | + } |
|
| 1061 | + |
|
| 1062 | + /** |
|
| 1063 | + * Parse every relationships on the EntityMap and sort |
|
| 1064 | + * them by type. |
|
| 1065 | + * |
|
| 1066 | + * @return void |
|
| 1067 | + */ |
|
| 1068 | + public function boot() |
|
| 1069 | + { |
|
| 1070 | + if (count($this->relationships > 0)) { |
|
| 1071 | + $this->sortRelationshipsByType(); |
|
| 1072 | + } |
|
| 1073 | + } |
|
| 1074 | + |
|
| 1075 | + /** |
|
| 1076 | + * Get Methods that has been added in the child class. |
|
| 1077 | + * |
|
| 1078 | + * @return array |
|
| 1079 | + */ |
|
| 1080 | + protected function getCustomMethods() |
|
| 1081 | + { |
|
| 1082 | + $mapMethods = get_class_methods($this); |
|
| 1083 | + |
|
| 1084 | + $parentsMethods = get_class_methods('Analogue\ORM\EntityMap'); |
|
| 1085 | 1085 | |
| 1086 | - return array_diff($mapMethods, $parentsMethods); |
|
| 1087 | - } |
|
| 1088 | - |
|
| 1089 | - /** |
|
| 1090 | - * Parse user's class methods for relationships |
|
| 1091 | - * |
|
| 1092 | - * @param array $customMethods |
|
| 1093 | - * @return array |
|
| 1094 | - */ |
|
| 1095 | - protected function parseMethodsForRelationship(array $customMethods) |
|
| 1096 | - { |
|
| 1097 | - $relationships = []; |
|
| 1098 | - |
|
| 1099 | - $class = new ReflectionClass(get_class($this)); |
|
| 1100 | - |
|
| 1101 | - // Get the mapped Entity class, as we will detect relationships |
|
| 1102 | - // methods by testing that the first argument is type-hinted to |
|
| 1103 | - // the same class as the mapped Entity. |
|
| 1104 | - $entityClass = $this->getClass(); |
|
| 1105 | - |
|
| 1106 | - foreach ($customMethods as $methodName) { |
|
| 1107 | - $method = $class->getMethod($methodName); |
|
| 1108 | - |
|
| 1109 | - if ($method->getNumberOfParameters() > 0) { |
|
| 1110 | - $params = $method->getParameters(); |
|
| 1111 | - |
|
| 1112 | - if ($params[0]->getClass() && $params[0]->getClass()->name == $entityClass) { |
|
| 1113 | - $relationships[] = $methodName; |
|
| 1114 | - } |
|
| 1115 | - } |
|
| 1116 | - } |
|
| 1117 | - |
|
| 1118 | - return $relationships; |
|
| 1119 | - } |
|
| 1120 | - |
|
| 1121 | - /** |
|
| 1122 | - * Sort Relationships methods by type |
|
| 1123 | - * |
|
| 1124 | - * @return void |
|
| 1125 | - */ |
|
| 1126 | - protected function sortRelationshipsByType() |
|
| 1127 | - { |
|
| 1128 | - $entityClass = $this->getClass(); |
|
| 1129 | - |
|
| 1130 | - // Instantiate a dummy entity which we will pass to relationship methods. |
|
| 1131 | - $entity = unserialize(sprintf('O:%d:"%s":0:{}', strlen($entityClass), $entityClass)); |
|
| 1086 | + return array_diff($mapMethods, $parentsMethods); |
|
| 1087 | + } |
|
| 1088 | + |
|
| 1089 | + /** |
|
| 1090 | + * Parse user's class methods for relationships |
|
| 1091 | + * |
|
| 1092 | + * @param array $customMethods |
|
| 1093 | + * @return array |
|
| 1094 | + */ |
|
| 1095 | + protected function parseMethodsForRelationship(array $customMethods) |
|
| 1096 | + { |
|
| 1097 | + $relationships = []; |
|
| 1098 | + |
|
| 1099 | + $class = new ReflectionClass(get_class($this)); |
|
| 1100 | + |
|
| 1101 | + // Get the mapped Entity class, as we will detect relationships |
|
| 1102 | + // methods by testing that the first argument is type-hinted to |
|
| 1103 | + // the same class as the mapped Entity. |
|
| 1104 | + $entityClass = $this->getClass(); |
|
| 1105 | + |
|
| 1106 | + foreach ($customMethods as $methodName) { |
|
| 1107 | + $method = $class->getMethod($methodName); |
|
| 1108 | + |
|
| 1109 | + if ($method->getNumberOfParameters() > 0) { |
|
| 1110 | + $params = $method->getParameters(); |
|
| 1111 | + |
|
| 1112 | + if ($params[0]->getClass() && $params[0]->getClass()->name == $entityClass) { |
|
| 1113 | + $relationships[] = $methodName; |
|
| 1114 | + } |
|
| 1115 | + } |
|
| 1116 | + } |
|
| 1117 | + |
|
| 1118 | + return $relationships; |
|
| 1119 | + } |
|
| 1120 | + |
|
| 1121 | + /** |
|
| 1122 | + * Sort Relationships methods by type |
|
| 1123 | + * |
|
| 1124 | + * @return void |
|
| 1125 | + */ |
|
| 1126 | + protected function sortRelationshipsByType() |
|
| 1127 | + { |
|
| 1128 | + $entityClass = $this->getClass(); |
|
| 1129 | + |
|
| 1130 | + // Instantiate a dummy entity which we will pass to relationship methods. |
|
| 1131 | + $entity = unserialize(sprintf('O:%d:"%s":0:{}', strlen($entityClass), $entityClass)); |
|
| 1132 | 1132 | |
| 1133 | - foreach ($this->relationships as $relation) { |
|
| 1134 | - $relationObject = $this->$relation($entity); |
|
| 1135 | - |
|
| 1136 | - $class = class_basename(get_class($relationObject)); |
|
| 1137 | - |
|
| 1138 | - if (in_array($class, static::$singleClasses)) { |
|
| 1139 | - $this->singleRelations[] = $relation; |
|
| 1140 | - } |
|
| 1141 | - |
|
| 1142 | - if (in_array($class, static::$manyClasses)) { |
|
| 1143 | - $this->manyRelations[] = $relation; |
|
| 1144 | - } |
|
| 1145 | - |
|
| 1146 | - if (in_array($class, static::$localClasses)) { |
|
| 1147 | - $this->localRelations[] = $relation; |
|
| 1148 | - } |
|
| 1149 | - |
|
| 1150 | - if (in_array($class, static::$foreignClasses)) { |
|
| 1151 | - $this->foreignRelations[] = $relation; |
|
| 1152 | - } |
|
| 1153 | - |
|
| 1154 | - if (in_array($class, static::$pivotClasses)) { |
|
| 1155 | - $this->pivotRelations[] = $relation; |
|
| 1156 | - } |
|
| 1157 | - } |
|
| 1158 | - } |
|
| 1159 | - |
|
| 1160 | - /** |
|
| 1161 | - * Override this method for custom entity instantiation |
|
| 1162 | - * |
|
| 1163 | - * @return null |
|
| 1164 | - */ |
|
| 1165 | - public function activator() |
|
| 1166 | - { |
|
| 1167 | - return null; |
|
| 1168 | - } |
|
| 1169 | - |
|
| 1170 | - /** |
|
| 1171 | - * Call dynamic relationship, if it exists |
|
| 1172 | - * |
|
| 1173 | - * @param string $method |
|
| 1174 | - * @param array $parameters |
|
| 1175 | - * @throws Exception |
|
| 1176 | - * @return mixed |
|
| 1177 | - */ |
|
| 1178 | - public function __call($method, $parameters) |
|
| 1179 | - { |
|
| 1180 | - if (!array_key_exists($method, $this->dynamicRelationships)) { |
|
| 1181 | - throw new Exception(get_class($this) . " has no method $method"); |
|
| 1182 | - } |
|
| 1183 | - |
|
| 1184 | - // Add $this to parameters so the closure can call relationship method on the map. |
|
| 1185 | - $parameters[] = $this; |
|
| 1186 | - |
|
| 1187 | - return call_user_func_array([$this->dynamicRelationships[$method], $parameters]); |
|
| 1188 | - } |
|
| 1133 | + foreach ($this->relationships as $relation) { |
|
| 1134 | + $relationObject = $this->$relation($entity); |
|
| 1135 | + |
|
| 1136 | + $class = class_basename(get_class($relationObject)); |
|
| 1137 | + |
|
| 1138 | + if (in_array($class, static::$singleClasses)) { |
|
| 1139 | + $this->singleRelations[] = $relation; |
|
| 1140 | + } |
|
| 1141 | + |
|
| 1142 | + if (in_array($class, static::$manyClasses)) { |
|
| 1143 | + $this->manyRelations[] = $relation; |
|
| 1144 | + } |
|
| 1145 | + |
|
| 1146 | + if (in_array($class, static::$localClasses)) { |
|
| 1147 | + $this->localRelations[] = $relation; |
|
| 1148 | + } |
|
| 1149 | + |
|
| 1150 | + if (in_array($class, static::$foreignClasses)) { |
|
| 1151 | + $this->foreignRelations[] = $relation; |
|
| 1152 | + } |
|
| 1153 | + |
|
| 1154 | + if (in_array($class, static::$pivotClasses)) { |
|
| 1155 | + $this->pivotRelations[] = $relation; |
|
| 1156 | + } |
|
| 1157 | + } |
|
| 1158 | + } |
|
| 1159 | + |
|
| 1160 | + /** |
|
| 1161 | + * Override this method for custom entity instantiation |
|
| 1162 | + * |
|
| 1163 | + * @return null |
|
| 1164 | + */ |
|
| 1165 | + public function activator() |
|
| 1166 | + { |
|
| 1167 | + return null; |
|
| 1168 | + } |
|
| 1169 | + |
|
| 1170 | + /** |
|
| 1171 | + * Call dynamic relationship, if it exists |
|
| 1172 | + * |
|
| 1173 | + * @param string $method |
|
| 1174 | + * @param array $parameters |
|
| 1175 | + * @throws Exception |
|
| 1176 | + * @return mixed |
|
| 1177 | + */ |
|
| 1178 | + public function __call($method, $parameters) |
|
| 1179 | + { |
|
| 1180 | + if (!array_key_exists($method, $this->dynamicRelationships)) { |
|
| 1181 | + throw new Exception(get_class($this) . " has no method $method"); |
|
| 1182 | + } |
|
| 1183 | + |
|
| 1184 | + // Add $this to parameters so the closure can call relationship method on the map. |
|
| 1185 | + $parameters[] = $this; |
|
| 1186 | + |
|
| 1187 | + return call_user_func_array([$this->dynamicRelationships[$method], $parameters]); |
|
| 1188 | + } |
|
| 1189 | 1189 | } |
@@ -350,7 +350,7 @@ discard block |
||
| 350 | 350 | */ |
| 351 | 351 | public function getEntityHash() |
| 352 | 352 | { |
| 353 | - return $this->getEntityClass() . '.' . $this->getEntityId(); |
|
| 353 | + return $this->getEntityClass().'.'.$this->getEntityId(); |
|
| 354 | 354 | } |
| 355 | 355 | |
| 356 | 356 | /** |
@@ -566,7 +566,7 @@ discard block |
||
| 566 | 566 | $prefix = snake_case(class_basename($embed)); |
| 567 | 567 | |
| 568 | 568 | foreach ($valueObjectAttributes as $key=>$value) { |
| 569 | - $valueObjectAttributes[$prefix . '_' . $key] = $value; |
|
| 569 | + $valueObjectAttributes[$prefix.'_'.$key] = $value; |
|
| 570 | 570 | unset($valueObjectAttributes[$key]); |
| 571 | 571 | } |
| 572 | 572 | |
@@ -854,7 +854,7 @@ discard block |
||
| 854 | 854 | */ |
| 855 | 855 | protected function getEntityHashesFromRelation($relation) |
| 856 | 856 | { |
| 857 | - return array_map(function ($aggregate) { |
|
| 857 | + return array_map(function($aggregate) { |
|
| 858 | 858 | return $aggregate->getEntityHash(); |
| 859 | 859 | }, $this->relationships[$relation]); |
| 860 | 860 | } |
@@ -15,1016 +15,1016 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class Aggregate implements InternallyMappable |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * The Root Entity |
|
| 20 | - * |
|
| 21 | - * @var \Analogue\ORM\System\Wrappers\Wrapper |
|
| 22 | - */ |
|
| 23 | - protected $wrappedEntity; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Parent Root Aggregate |
|
| 27 | - * |
|
| 28 | - * @var \Analogue\ORM\System\Aggregate |
|
| 29 | - */ |
|
| 30 | - protected $parent; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Parent's relationship method |
|
| 34 | - * |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - protected $parentRelationship; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Root Entity |
|
| 41 | - * |
|
| 42 | - * @var \Analogue\ORM\System\Aggregate |
|
| 43 | - */ |
|
| 44 | - protected $root; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * An associative array containing entity's |
|
| 48 | - * relationships converted to Aggregates |
|
| 49 | - * |
|
| 50 | - * @var array |
|
| 51 | - */ |
|
| 52 | - protected $relationships = []; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Relationship that need post-command synchronization |
|
| 56 | - * |
|
| 57 | - * @var array |
|
| 58 | - */ |
|
| 59 | - protected $needSync = []; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Mapper |
|
| 63 | - * |
|
| 64 | - * @var \Analogue\ORM\System\Mapper; |
|
| 65 | - */ |
|
| 66 | - protected $mapper; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Entity Map |
|
| 70 | - * |
|
| 71 | - * @var \Analogue\ORM\EntityMap; |
|
| 72 | - */ |
|
| 73 | - protected $entityMap; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Create a new Aggregated Entity instance |
|
| 77 | - * |
|
| 78 | - * @param mixed $entity |
|
| 79 | - * @param Aggregate|null $parent |
|
| 80 | - * @param string $parentRelationship |
|
| 81 | - * @param Aggregate|null $root |
|
| 82 | - * @throws MappingException |
|
| 83 | - */ |
|
| 84 | - public function __construct($entity, Aggregate $parent = null, $parentRelationship = null, Aggregate $root = null) |
|
| 85 | - { |
|
| 86 | - $factory = new Factory; |
|
| 18 | + /** |
|
| 19 | + * The Root Entity |
|
| 20 | + * |
|
| 21 | + * @var \Analogue\ORM\System\Wrappers\Wrapper |
|
| 22 | + */ |
|
| 23 | + protected $wrappedEntity; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Parent Root Aggregate |
|
| 27 | + * |
|
| 28 | + * @var \Analogue\ORM\System\Aggregate |
|
| 29 | + */ |
|
| 30 | + protected $parent; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Parent's relationship method |
|
| 34 | + * |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + protected $parentRelationship; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Root Entity |
|
| 41 | + * |
|
| 42 | + * @var \Analogue\ORM\System\Aggregate |
|
| 43 | + */ |
|
| 44 | + protected $root; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * An associative array containing entity's |
|
| 48 | + * relationships converted to Aggregates |
|
| 49 | + * |
|
| 50 | + * @var array |
|
| 51 | + */ |
|
| 52 | + protected $relationships = []; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Relationship that need post-command synchronization |
|
| 56 | + * |
|
| 57 | + * @var array |
|
| 58 | + */ |
|
| 59 | + protected $needSync = []; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Mapper |
|
| 63 | + * |
|
| 64 | + * @var \Analogue\ORM\System\Mapper; |
|
| 65 | + */ |
|
| 66 | + protected $mapper; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Entity Map |
|
| 70 | + * |
|
| 71 | + * @var \Analogue\ORM\EntityMap; |
|
| 72 | + */ |
|
| 73 | + protected $entityMap; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Create a new Aggregated Entity instance |
|
| 77 | + * |
|
| 78 | + * @param mixed $entity |
|
| 79 | + * @param Aggregate|null $parent |
|
| 80 | + * @param string $parentRelationship |
|
| 81 | + * @param Aggregate|null $root |
|
| 82 | + * @throws MappingException |
|
| 83 | + */ |
|
| 84 | + public function __construct($entity, Aggregate $parent = null, $parentRelationship = null, Aggregate $root = null) |
|
| 85 | + { |
|
| 86 | + $factory = new Factory; |
|
| 87 | 87 | |
| 88 | - $this->wrappedEntity = $factory->make($entity); |
|
| 88 | + $this->wrappedEntity = $factory->make($entity); |
|
| 89 | 89 | |
| 90 | - $this->parent = $parent; |
|
| 90 | + $this->parent = $parent; |
|
| 91 | 91 | |
| 92 | - $this->parentRelationship = $parentRelationship; |
|
| 92 | + $this->parentRelationship = $parentRelationship; |
|
| 93 | 93 | |
| 94 | - $this->root = $root; |
|
| 94 | + $this->root = $root; |
|
| 95 | 95 | |
| 96 | - $this->mapper = Manager::getMapper($entity); |
|
| 96 | + $this->mapper = Manager::getMapper($entity); |
|
| 97 | 97 | |
| 98 | - $this->entityMap = $this->mapper->getEntityMap(); |
|
| 98 | + $this->entityMap = $this->mapper->getEntityMap(); |
|
| 99 | 99 | |
| 100 | - $this->parseRelationships(); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Parse Every relationships defined on the entity |
|
| 105 | - * |
|
| 106 | - * @throws MappingException |
|
| 107 | - * @return void |
|
| 108 | - */ |
|
| 109 | - protected function parseRelationships() |
|
| 110 | - { |
|
| 111 | - foreach ($this->entityMap->getSingleRelationships() as $relation) { |
|
| 112 | - $this->parseSingleRelationship($relation); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - foreach ($this->entityMap->getManyRelationships() as $relation) { |
|
| 116 | - $this->parseManyRelationship($relation); |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Parse for values common to single & many relations |
|
| 122 | - * |
|
| 123 | - * @param string $relation |
|
| 124 | - * @throws MappingException |
|
| 125 | - * @return mixed|boolean |
|
| 126 | - */ |
|
| 127 | - protected function parseForCommonValues($relation) |
|
| 128 | - { |
|
| 129 | - if (!$this->hasAttribute($relation)) { |
|
| 130 | - // If no attribute exists for this relationships |
|
| 131 | - // we'll make it a simple empty array. This will |
|
| 132 | - // save us from constantly checking for the attributes |
|
| 133 | - // actual existence. |
|
| 134 | - $this->relationships[$relation] = []; |
|
| 135 | - return false; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - $value = $this->getRelationshipValue($relation); |
|
| 139 | - |
|
| 140 | - if (is_null($value)) { |
|
| 141 | - $this->relationships[$relation] = []; |
|
| 142 | - |
|
| 143 | - // If the relationship's content is the null value |
|
| 144 | - // and the Entity's exist in DB, we'll interpret this |
|
| 145 | - // as the need to detach all related Entities, |
|
| 146 | - // therefore a sync operation is needed. |
|
| 147 | - $this->needSync[] = $relation; |
|
| 148 | - return false; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - return $value; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Parse a 'single' relationship |
|
| 156 | - * |
|
| 157 | - * @param string $relation |
|
| 158 | - * @throws MappingException |
|
| 159 | - * @return boolean |
|
| 160 | - */ |
|
| 161 | - protected function parseSingleRelationship($relation) |
|
| 162 | - { |
|
| 163 | - if (!$value = $this->parseForCommonValues($relation)) { |
|
| 164 | - return true; |
|
| 165 | - } |
|
| 100 | + $this->parseRelationships(); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Parse Every relationships defined on the entity |
|
| 105 | + * |
|
| 106 | + * @throws MappingException |
|
| 107 | + * @return void |
|
| 108 | + */ |
|
| 109 | + protected function parseRelationships() |
|
| 110 | + { |
|
| 111 | + foreach ($this->entityMap->getSingleRelationships() as $relation) { |
|
| 112 | + $this->parseSingleRelationship($relation); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + foreach ($this->entityMap->getManyRelationships() as $relation) { |
|
| 116 | + $this->parseManyRelationship($relation); |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Parse for values common to single & many relations |
|
| 122 | + * |
|
| 123 | + * @param string $relation |
|
| 124 | + * @throws MappingException |
|
| 125 | + * @return mixed|boolean |
|
| 126 | + */ |
|
| 127 | + protected function parseForCommonValues($relation) |
|
| 128 | + { |
|
| 129 | + if (!$this->hasAttribute($relation)) { |
|
| 130 | + // If no attribute exists for this relationships |
|
| 131 | + // we'll make it a simple empty array. This will |
|
| 132 | + // save us from constantly checking for the attributes |
|
| 133 | + // actual existence. |
|
| 134 | + $this->relationships[$relation] = []; |
|
| 135 | + return false; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + $value = $this->getRelationshipValue($relation); |
|
| 139 | + |
|
| 140 | + if (is_null($value)) { |
|
| 141 | + $this->relationships[$relation] = []; |
|
| 142 | + |
|
| 143 | + // If the relationship's content is the null value |
|
| 144 | + // and the Entity's exist in DB, we'll interpret this |
|
| 145 | + // as the need to detach all related Entities, |
|
| 146 | + // therefore a sync operation is needed. |
|
| 147 | + $this->needSync[] = $relation; |
|
| 148 | + return false; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + return $value; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Parse a 'single' relationship |
|
| 156 | + * |
|
| 157 | + * @param string $relation |
|
| 158 | + * @throws MappingException |
|
| 159 | + * @return boolean |
|
| 160 | + */ |
|
| 161 | + protected function parseSingleRelationship($relation) |
|
| 162 | + { |
|
| 163 | + if (!$value = $this->parseForCommonValues($relation)) { |
|
| 164 | + return true; |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - if ($value instanceof Collection || is_array($value) || $value instanceof CollectionProxy) { |
|
| 168 | - throw new MappingException("Entity's attribute $relation should not be array, or collection"); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - if ($value instanceof EntityProxy && !$value->isLoaded()) { |
|
| 172 | - $this->relationships[$relation] = []; |
|
| 173 | - return true; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - // If the attribute is a loaded proxy, swap it for its |
|
| 177 | - // loaded entity. |
|
| 178 | - if ($value instanceof EntityProxy && $value->isLoaded()) { |
|
| 179 | - $value = $value->getUnderlyingObject(); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - if ($this->isParentOrRoot($value)) { |
|
| 183 | - $this->relationships[$relation] = []; |
|
| 184 | - return true; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - // At this point, we can assume the attribute is an Entity instance |
|
| 188 | - // so we'll treat it as such. |
|
| 189 | - $subAggregate = $this->createSubAggregate($value, $relation); |
|
| 167 | + if ($value instanceof Collection || is_array($value) || $value instanceof CollectionProxy) { |
|
| 168 | + throw new MappingException("Entity's attribute $relation should not be array, or collection"); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + if ($value instanceof EntityProxy && !$value->isLoaded()) { |
|
| 172 | + $this->relationships[$relation] = []; |
|
| 173 | + return true; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + // If the attribute is a loaded proxy, swap it for its |
|
| 177 | + // loaded entity. |
|
| 178 | + if ($value instanceof EntityProxy && $value->isLoaded()) { |
|
| 179 | + $value = $value->getUnderlyingObject(); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + if ($this->isParentOrRoot($value)) { |
|
| 183 | + $this->relationships[$relation] = []; |
|
| 184 | + return true; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + // At this point, we can assume the attribute is an Entity instance |
|
| 188 | + // so we'll treat it as such. |
|
| 189 | + $subAggregate = $this->createSubAggregate($value, $relation); |
|
| 190 | 190 | |
| 191 | - // Even if it's a single entity, we'll store it as an array |
|
| 192 | - // just for consistency with other relationships |
|
| 193 | - $this->relationships[$relation] = [$subAggregate]; |
|
| 194 | - |
|
| 195 | - // We always need to check a loaded relation is in sync |
|
| 196 | - // with its local key |
|
| 197 | - $this->needSync[] = $relation; |
|
| 198 | - |
|
| 199 | - return true; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Check if value isn't parent or root in the aggregate |
|
| 204 | - * |
|
| 205 | - * @param mixed |
|
| 206 | - * @return boolean|null |
|
| 207 | - */ |
|
| 208 | - protected function isParentOrRoot($value) |
|
| 209 | - { |
|
| 210 | - if (!is_null($this->root)) { |
|
| 211 | - $rootClass = get_class($this->root->getEntityObject()); |
|
| 212 | - if ($rootClass == get_class($value)) { |
|
| 213 | - return true; |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - if (!is_null($this->parent)) { |
|
| 218 | - $parentClass = get_class($this->parent->getEntityObject()); |
|
| 219 | - if ($parentClass == get_class($value)) { |
|
| 220 | - return true; |
|
| 221 | - } |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Parse a 'many' relationship |
|
| 227 | - * |
|
| 228 | - * @param string $relation |
|
| 229 | - * @throws MappingException |
|
| 230 | - * @return boolean |
|
| 231 | - */ |
|
| 232 | - protected function parseManyRelationship($relation) |
|
| 233 | - { |
|
| 234 | - if (!$value = $this->parseForCommonValues($relation)) { |
|
| 235 | - return true; |
|
| 236 | - } |
|
| 191 | + // Even if it's a single entity, we'll store it as an array |
|
| 192 | + // just for consistency with other relationships |
|
| 193 | + $this->relationships[$relation] = [$subAggregate]; |
|
| 194 | + |
|
| 195 | + // We always need to check a loaded relation is in sync |
|
| 196 | + // with its local key |
|
| 197 | + $this->needSync[] = $relation; |
|
| 198 | + |
|
| 199 | + return true; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Check if value isn't parent or root in the aggregate |
|
| 204 | + * |
|
| 205 | + * @param mixed |
|
| 206 | + * @return boolean|null |
|
| 207 | + */ |
|
| 208 | + protected function isParentOrRoot($value) |
|
| 209 | + { |
|
| 210 | + if (!is_null($this->root)) { |
|
| 211 | + $rootClass = get_class($this->root->getEntityObject()); |
|
| 212 | + if ($rootClass == get_class($value)) { |
|
| 213 | + return true; |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + if (!is_null($this->parent)) { |
|
| 218 | + $parentClass = get_class($this->parent->getEntityObject()); |
|
| 219 | + if ($parentClass == get_class($value)) { |
|
| 220 | + return true; |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Parse a 'many' relationship |
|
| 227 | + * |
|
| 228 | + * @param string $relation |
|
| 229 | + * @throws MappingException |
|
| 230 | + * @return boolean |
|
| 231 | + */ |
|
| 232 | + protected function parseManyRelationship($relation) |
|
| 233 | + { |
|
| 234 | + if (!$value = $this->parseForCommonValues($relation)) { |
|
| 235 | + return true; |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | - if (is_array($value) || $value instanceof Collection) { |
|
| 239 | - $this->needSync[] = $relation; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - // If the relation is a proxy, we test is the relation |
|
| 243 | - // has been lazy loaded, otherwise we'll just treat |
|
| 244 | - // the subset of newly added items. |
|
| 245 | - if ($value instanceof CollectionProxy && $value->isLoaded()) { |
|
| 246 | - $this->needSync[] = $relation; |
|
| 247 | - $value = $value->getUnderlyingCollection(); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - if ($value instanceof CollectionProxy && !$value->isLoaded()) { |
|
| 251 | - $value = $value->getAddedItems(); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - // At this point $value should be either an array or an instance |
|
| 255 | - // of a collection class. |
|
| 256 | - if (!is_array($value) && !$value instanceof Collection) { |
|
| 257 | - throw new MappingException("'$relation' attribute should be array() or Collection"); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - $this->relationships[$relation] = $this->createSubAggregates($value, $relation); |
|
| 238 | + if (is_array($value) || $value instanceof Collection) { |
|
| 239 | + $this->needSync[] = $relation; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + // If the relation is a proxy, we test is the relation |
|
| 243 | + // has been lazy loaded, otherwise we'll just treat |
|
| 244 | + // the subset of newly added items. |
|
| 245 | + if ($value instanceof CollectionProxy && $value->isLoaded()) { |
|
| 246 | + $this->needSync[] = $relation; |
|
| 247 | + $value = $value->getUnderlyingCollection(); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + if ($value instanceof CollectionProxy && !$value->isLoaded()) { |
|
| 251 | + $value = $value->getAddedItems(); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + // At this point $value should be either an array or an instance |
|
| 255 | + // of a collection class. |
|
| 256 | + if (!is_array($value) && !$value instanceof Collection) { |
|
| 257 | + throw new MappingException("'$relation' attribute should be array() or Collection"); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + $this->relationships[$relation] = $this->createSubAggregates($value, $relation); |
|
| 261 | 261 | |
| 262 | - return true; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * Return Entity's relationship attribute |
|
| 267 | - * |
|
| 268 | - * @param string $relation |
|
| 269 | - * @throws MappingException |
|
| 270 | - * @return mixed |
|
| 271 | - */ |
|
| 272 | - protected function getRelationshipValue($relation) |
|
| 273 | - { |
|
| 274 | - $value = $this->getEntityAttribute($relation); |
|
| 275 | - //if($relation == "role") tdd($this->wrappedEntity->getEntityAttributes()); |
|
| 276 | - if (is_bool($value) || is_float($value) || is_int($value) || is_string($value)) { |
|
| 277 | - throw new MappingException("Entity's attribute $relation should be array, object, collection or null"); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - return $value; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Create a child, aggregated entity |
|
| 285 | - * |
|
| 286 | - * @param mixed $entities |
|
| 287 | - * @param string $relation |
|
| 288 | - * @return array |
|
| 289 | - */ |
|
| 290 | - protected function createSubAggregates($entities, $relation) |
|
| 291 | - { |
|
| 292 | - $aggregates = []; |
|
| 293 | - |
|
| 294 | - foreach ($entities as $entity) { |
|
| 295 | - $aggregates[] = $this->createSubAggregate($entity, $relation); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - return $aggregates; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Create a related subAggregate |
|
| 303 | - * |
|
| 304 | - * @param mixed $entity |
|
| 305 | - * @param string $relation |
|
| 306 | - * @throws MappingException |
|
| 307 | - * @return self |
|
| 308 | - */ |
|
| 309 | - protected function createSubAggregate($entity, $relation) |
|
| 310 | - { |
|
| 311 | - // If root isn't defined, then this is the Aggregate Root |
|
| 312 | - if (is_null($this->root)) { |
|
| 313 | - $root = $this; |
|
| 314 | - } else { |
|
| 315 | - $root = $this->root; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - return new self($entity, $this, $relation, $root); |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Get the Entity's primary key attribute |
|
| 323 | - * |
|
| 324 | - * @return string|integer |
|
| 325 | - */ |
|
| 326 | - public function getEntityId() |
|
| 327 | - { |
|
| 328 | - return $this->wrappedEntity->getEntityAttribute($this->entityMap->getKeyName()); |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * Get the name of the primary key |
|
| 333 | - * |
|
| 334 | - * @return string |
|
| 335 | - */ |
|
| 336 | - public function getEntityKey() |
|
| 337 | - { |
|
| 338 | - return $this->entityMap->getKeyName(); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Return the entity map for the current entity |
|
| 343 | - * |
|
| 344 | - * @return \Analogue\ORM\EntityMap |
|
| 345 | - */ |
|
| 346 | - public function getEntityMap() |
|
| 347 | - { |
|
| 348 | - return $this->entityMap; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * Return the Entity's hash $class.$id |
|
| 353 | - * |
|
| 354 | - * @return string |
|
| 355 | - */ |
|
| 356 | - public function getEntityHash() |
|
| 357 | - { |
|
| 358 | - return $this->getEntityClass() . '.' . $this->getEntityId(); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Get wrapped entity class |
|
| 363 | - * |
|
| 364 | - * @return string |
|
| 365 | - */ |
|
| 366 | - public function getEntityClass() |
|
| 367 | - { |
|
| 368 | - return $this->entityMap->getClass(); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Return the Mapper's entity cache |
|
| 373 | - * |
|
| 374 | - * @return \Analogue\ORM\System\EntityCache |
|
| 375 | - */ |
|
| 376 | - protected function getEntityCache() |
|
| 377 | - { |
|
| 378 | - return $this->mapper->getEntityCache(); |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * Get a relationship as an aggregated entities' array |
|
| 383 | - * |
|
| 384 | - * @param string $name |
|
| 385 | - * @return array |
|
| 386 | - */ |
|
| 387 | - public function getRelationship($name) |
|
| 388 | - { |
|
| 389 | - if (array_key_exists($name, $this->relationships)) { |
|
| 390 | - return $this->relationships[$name]; |
|
| 391 | - } else { |
|
| 392 | - return []; |
|
| 393 | - } |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * [TO IMPLEMENT] |
|
| 398 | - * |
|
| 399 | - * @return array |
|
| 400 | - */ |
|
| 401 | - public function getPivotAttributes() |
|
| 402 | - { |
|
| 403 | - return []; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * Get Non existing related entities from several relationships |
|
| 408 | - * |
|
| 409 | - * @param array $relationships |
|
| 410 | - * @return array |
|
| 411 | - */ |
|
| 412 | - public function getNonExistingRelated(array $relationships) |
|
| 413 | - { |
|
| 414 | - $nonExisting = []; |
|
| 415 | - |
|
| 416 | - foreach ($relationships as $relation) { |
|
| 417 | - if ($this->hasAttribute($relation) && array_key_exists($relation, $this->relationships)) { |
|
| 418 | - $nonExisting = array_merge($nonExisting, $this->getNonExistingFromRelation($relation)); |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - return $nonExisting; |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - /** |
|
| 426 | - * Get non-existing related entities from a single relation |
|
| 427 | - * |
|
| 428 | - * @param string $relation |
|
| 429 | - * @return array |
|
| 430 | - */ |
|
| 431 | - protected function getNonExistingFromRelation($relation) |
|
| 432 | - { |
|
| 433 | - $nonExisting = []; |
|
| 434 | - |
|
| 435 | - foreach ($this->relationships[$relation] as $aggregate) { |
|
| 436 | - if (!$aggregate->exists()) { |
|
| 437 | - $nonExisting[] = $aggregate; |
|
| 438 | - } |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - return $nonExisting; |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * Synchronize relationships if needed |
|
| 446 | - */ |
|
| 447 | - public function syncRelationships(array $relationships) |
|
| 448 | - { |
|
| 449 | - if ($this->exists()) { |
|
| 450 | - foreach ($relationships as $relation) { |
|
| 451 | - if (in_array($relation, $this->needSync)) { |
|
| 452 | - $this->synchronize($relation); |
|
| 453 | - } |
|
| 454 | - } |
|
| 455 | - } |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - /** |
|
| 459 | - * Synchronize a relationship attribute |
|
| 460 | - * |
|
| 461 | - * @param $relation |
|
| 462 | - */ |
|
| 463 | - protected function synchronize($relation) |
|
| 464 | - { |
|
| 465 | - $actualContent = $this->relationships[$relation]; |
|
| 466 | - |
|
| 467 | - $this->entityMap->$relation($this->getEntityObject())->sync($actualContent); |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - /** |
|
| 471 | - * Returns an array of Missing related Entities for the |
|
| 472 | - * given $relation |
|
| 473 | - * |
|
| 474 | - * @param string $relation |
|
| 475 | - * @return array |
|
| 476 | - */ |
|
| 477 | - public function getMissingEntities($relation) |
|
| 478 | - { |
|
| 479 | - $cachedRelations = $this->getCachedAttribute($relation); |
|
| 480 | - |
|
| 481 | - if (!is_null($cachedRelations)) { |
|
| 482 | - $missing = []; |
|
| 483 | - |
|
| 484 | - foreach ($cachedRelations as $hash) { |
|
| 485 | - if (!$this->getRelatedAggregateFromHash($hash, $relation)) { |
|
| 486 | - $missing[] = $hash; |
|
| 487 | - } |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - return $missing; |
|
| 491 | - } else { |
|
| 492 | - return []; |
|
| 493 | - } |
|
| 494 | - } |
|
| 262 | + return true; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * Return Entity's relationship attribute |
|
| 267 | + * |
|
| 268 | + * @param string $relation |
|
| 269 | + * @throws MappingException |
|
| 270 | + * @return mixed |
|
| 271 | + */ |
|
| 272 | + protected function getRelationshipValue($relation) |
|
| 273 | + { |
|
| 274 | + $value = $this->getEntityAttribute($relation); |
|
| 275 | + //if($relation == "role") tdd($this->wrappedEntity->getEntityAttributes()); |
|
| 276 | + if (is_bool($value) || is_float($value) || is_int($value) || is_string($value)) { |
|
| 277 | + throw new MappingException("Entity's attribute $relation should be array, object, collection or null"); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + return $value; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Create a child, aggregated entity |
|
| 285 | + * |
|
| 286 | + * @param mixed $entities |
|
| 287 | + * @param string $relation |
|
| 288 | + * @return array |
|
| 289 | + */ |
|
| 290 | + protected function createSubAggregates($entities, $relation) |
|
| 291 | + { |
|
| 292 | + $aggregates = []; |
|
| 293 | + |
|
| 294 | + foreach ($entities as $entity) { |
|
| 295 | + $aggregates[] = $this->createSubAggregate($entity, $relation); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + return $aggregates; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Create a related subAggregate |
|
| 303 | + * |
|
| 304 | + * @param mixed $entity |
|
| 305 | + * @param string $relation |
|
| 306 | + * @throws MappingException |
|
| 307 | + * @return self |
|
| 308 | + */ |
|
| 309 | + protected function createSubAggregate($entity, $relation) |
|
| 310 | + { |
|
| 311 | + // If root isn't defined, then this is the Aggregate Root |
|
| 312 | + if (is_null($this->root)) { |
|
| 313 | + $root = $this; |
|
| 314 | + } else { |
|
| 315 | + $root = $this->root; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + return new self($entity, $this, $relation, $root); |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Get the Entity's primary key attribute |
|
| 323 | + * |
|
| 324 | + * @return string|integer |
|
| 325 | + */ |
|
| 326 | + public function getEntityId() |
|
| 327 | + { |
|
| 328 | + return $this->wrappedEntity->getEntityAttribute($this->entityMap->getKeyName()); |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * Get the name of the primary key |
|
| 333 | + * |
|
| 334 | + * @return string |
|
| 335 | + */ |
|
| 336 | + public function getEntityKey() |
|
| 337 | + { |
|
| 338 | + return $this->entityMap->getKeyName(); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Return the entity map for the current entity |
|
| 343 | + * |
|
| 344 | + * @return \Analogue\ORM\EntityMap |
|
| 345 | + */ |
|
| 346 | + public function getEntityMap() |
|
| 347 | + { |
|
| 348 | + return $this->entityMap; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * Return the Entity's hash $class.$id |
|
| 353 | + * |
|
| 354 | + * @return string |
|
| 355 | + */ |
|
| 356 | + public function getEntityHash() |
|
| 357 | + { |
|
| 358 | + return $this->getEntityClass() . '.' . $this->getEntityId(); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Get wrapped entity class |
|
| 363 | + * |
|
| 364 | + * @return string |
|
| 365 | + */ |
|
| 366 | + public function getEntityClass() |
|
| 367 | + { |
|
| 368 | + return $this->entityMap->getClass(); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Return the Mapper's entity cache |
|
| 373 | + * |
|
| 374 | + * @return \Analogue\ORM\System\EntityCache |
|
| 375 | + */ |
|
| 376 | + protected function getEntityCache() |
|
| 377 | + { |
|
| 378 | + return $this->mapper->getEntityCache(); |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * Get a relationship as an aggregated entities' array |
|
| 383 | + * |
|
| 384 | + * @param string $name |
|
| 385 | + * @return array |
|
| 386 | + */ |
|
| 387 | + public function getRelationship($name) |
|
| 388 | + { |
|
| 389 | + if (array_key_exists($name, $this->relationships)) { |
|
| 390 | + return $this->relationships[$name]; |
|
| 391 | + } else { |
|
| 392 | + return []; |
|
| 393 | + } |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * [TO IMPLEMENT] |
|
| 398 | + * |
|
| 399 | + * @return array |
|
| 400 | + */ |
|
| 401 | + public function getPivotAttributes() |
|
| 402 | + { |
|
| 403 | + return []; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * Get Non existing related entities from several relationships |
|
| 408 | + * |
|
| 409 | + * @param array $relationships |
|
| 410 | + * @return array |
|
| 411 | + */ |
|
| 412 | + public function getNonExistingRelated(array $relationships) |
|
| 413 | + { |
|
| 414 | + $nonExisting = []; |
|
| 415 | + |
|
| 416 | + foreach ($relationships as $relation) { |
|
| 417 | + if ($this->hasAttribute($relation) && array_key_exists($relation, $this->relationships)) { |
|
| 418 | + $nonExisting = array_merge($nonExisting, $this->getNonExistingFromRelation($relation)); |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + return $nonExisting; |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + /** |
|
| 426 | + * Get non-existing related entities from a single relation |
|
| 427 | + * |
|
| 428 | + * @param string $relation |
|
| 429 | + * @return array |
|
| 430 | + */ |
|
| 431 | + protected function getNonExistingFromRelation($relation) |
|
| 432 | + { |
|
| 433 | + $nonExisting = []; |
|
| 434 | + |
|
| 435 | + foreach ($this->relationships[$relation] as $aggregate) { |
|
| 436 | + if (!$aggregate->exists()) { |
|
| 437 | + $nonExisting[] = $aggregate; |
|
| 438 | + } |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + return $nonExisting; |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * Synchronize relationships if needed |
|
| 446 | + */ |
|
| 447 | + public function syncRelationships(array $relationships) |
|
| 448 | + { |
|
| 449 | + if ($this->exists()) { |
|
| 450 | + foreach ($relationships as $relation) { |
|
| 451 | + if (in_array($relation, $this->needSync)) { |
|
| 452 | + $this->synchronize($relation); |
|
| 453 | + } |
|
| 454 | + } |
|
| 455 | + } |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + /** |
|
| 459 | + * Synchronize a relationship attribute |
|
| 460 | + * |
|
| 461 | + * @param $relation |
|
| 462 | + */ |
|
| 463 | + protected function synchronize($relation) |
|
| 464 | + { |
|
| 465 | + $actualContent = $this->relationships[$relation]; |
|
| 466 | + |
|
| 467 | + $this->entityMap->$relation($this->getEntityObject())->sync($actualContent); |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + /** |
|
| 471 | + * Returns an array of Missing related Entities for the |
|
| 472 | + * given $relation |
|
| 473 | + * |
|
| 474 | + * @param string $relation |
|
| 475 | + * @return array |
|
| 476 | + */ |
|
| 477 | + public function getMissingEntities($relation) |
|
| 478 | + { |
|
| 479 | + $cachedRelations = $this->getCachedAttribute($relation); |
|
| 480 | + |
|
| 481 | + if (!is_null($cachedRelations)) { |
|
| 482 | + $missing = []; |
|
| 483 | + |
|
| 484 | + foreach ($cachedRelations as $hash) { |
|
| 485 | + if (!$this->getRelatedAggregateFromHash($hash, $relation)) { |
|
| 486 | + $missing[] = $hash; |
|
| 487 | + } |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + return $missing; |
|
| 491 | + } else { |
|
| 492 | + return []; |
|
| 493 | + } |
|
| 494 | + } |
|
| 495 | 495 | |
| 496 | - /** |
|
| 497 | - * Get Relationships who have dirty attributes / dirty relationships |
|
| 498 | - * |
|
| 499 | - * @return array |
|
| 500 | - */ |
|
| 501 | - public function getDirtyRelationships() |
|
| 502 | - { |
|
| 503 | - $dirtyAggregates = []; |
|
| 504 | - |
|
| 505 | - foreach ($this->relationships as $relation) { |
|
| 506 | - foreach ($relation as $aggregate) { |
|
| 507 | - if (!$aggregate->exists() || $aggregate->isDirty() || count($aggregate->getDirtyRelationships() > 0)) { |
|
| 508 | - $dirtyAggregates[] = $aggregate; |
|
| 509 | - } |
|
| 510 | - } |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - return $dirtyAggregates; |
|
| 514 | - } |
|
| 496 | + /** |
|
| 497 | + * Get Relationships who have dirty attributes / dirty relationships |
|
| 498 | + * |
|
| 499 | + * @return array |
|
| 500 | + */ |
|
| 501 | + public function getDirtyRelationships() |
|
| 502 | + { |
|
| 503 | + $dirtyAggregates = []; |
|
| 504 | + |
|
| 505 | + foreach ($this->relationships as $relation) { |
|
| 506 | + foreach ($relation as $aggregate) { |
|
| 507 | + if (!$aggregate->exists() || $aggregate->isDirty() || count($aggregate->getDirtyRelationships() > 0)) { |
|
| 508 | + $dirtyAggregates[] = $aggregate; |
|
| 509 | + } |
|
| 510 | + } |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + return $dirtyAggregates; |
|
| 514 | + } |
|
| 515 | 515 | |
| 516 | - /** |
|
| 517 | - * Compare the object's raw attributes with the record in cache |
|
| 518 | - * |
|
| 519 | - * @return boolean |
|
| 520 | - */ |
|
| 521 | - public function isDirty() |
|
| 522 | - { |
|
| 523 | - if (count($this->getDirtyRawAttributes()) > 0) { |
|
| 524 | - return true; |
|
| 525 | - } else { |
|
| 526 | - return false; |
|
| 527 | - } |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - /** |
|
| 531 | - * Get Raw Entity's attributes, as they are represented |
|
| 532 | - * in the database, including value objects & foreign keys |
|
| 533 | - * |
|
| 534 | - * @return array |
|
| 535 | - */ |
|
| 536 | - public function getRawAttributes() |
|
| 537 | - { |
|
| 538 | - $attributes = $this->wrappedEntity->getEntityAttributes(); |
|
| 539 | - |
|
| 540 | - foreach ($this->entityMap->getRelationships() as $relation) { |
|
| 541 | - unset($attributes[$relation]); |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - $attributes = $this->flattenEmbeddables($attributes); |
|
| 545 | - |
|
| 546 | - $foreignKeys = $this->getForeignKeyAttributes(); |
|
| 547 | - |
|
| 548 | - return $attributes + $foreignKeys; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - /** |
|
| 552 | - * Convert Value Objects to raw db attributes |
|
| 553 | - * |
|
| 554 | - * @param array $attributes |
|
| 555 | - * @return array |
|
| 556 | - */ |
|
| 557 | - protected function flattenEmbeddables($attributes) |
|
| 558 | - { |
|
| 559 | - $embeddables = $this->entityMap->getEmbeddables(); |
|
| 516 | + /** |
|
| 517 | + * Compare the object's raw attributes with the record in cache |
|
| 518 | + * |
|
| 519 | + * @return boolean |
|
| 520 | + */ |
|
| 521 | + public function isDirty() |
|
| 522 | + { |
|
| 523 | + if (count($this->getDirtyRawAttributes()) > 0) { |
|
| 524 | + return true; |
|
| 525 | + } else { |
|
| 526 | + return false; |
|
| 527 | + } |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + /** |
|
| 531 | + * Get Raw Entity's attributes, as they are represented |
|
| 532 | + * in the database, including value objects & foreign keys |
|
| 533 | + * |
|
| 534 | + * @return array |
|
| 535 | + */ |
|
| 536 | + public function getRawAttributes() |
|
| 537 | + { |
|
| 538 | + $attributes = $this->wrappedEntity->getEntityAttributes(); |
|
| 539 | + |
|
| 540 | + foreach ($this->entityMap->getRelationships() as $relation) { |
|
| 541 | + unset($attributes[$relation]); |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + $attributes = $this->flattenEmbeddables($attributes); |
|
| 545 | + |
|
| 546 | + $foreignKeys = $this->getForeignKeyAttributes(); |
|
| 547 | + |
|
| 548 | + return $attributes + $foreignKeys; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + /** |
|
| 552 | + * Convert Value Objects to raw db attributes |
|
| 553 | + * |
|
| 554 | + * @param array $attributes |
|
| 555 | + * @return array |
|
| 556 | + */ |
|
| 557 | + protected function flattenEmbeddables($attributes) |
|
| 558 | + { |
|
| 559 | + $embeddables = $this->entityMap->getEmbeddables(); |
|
| 560 | 560 | |
| 561 | - foreach ($embeddables as $localKey => $embed) { |
|
| 562 | - // Retrieve the value object from the entity's attributes |
|
| 563 | - $valueObject = $attributes[$localKey]; |
|
| 561 | + foreach ($embeddables as $localKey => $embed) { |
|
| 562 | + // Retrieve the value object from the entity's attributes |
|
| 563 | + $valueObject = $attributes[$localKey]; |
|
| 564 | 564 | |
| 565 | - // Unset the corresponding key |
|
| 566 | - unset($attributes[$localKey]); |
|
| 565 | + // Unset the corresponding key |
|
| 566 | + unset($attributes[$localKey]); |
|
| 567 | 567 | |
| 568 | - // TODO Make wrapper object compatible with value objects |
|
| 569 | - $valueObjectAttributes = $valueObject->getEntityAttributes(); |
|
| 568 | + // TODO Make wrapper object compatible with value objects |
|
| 569 | + $valueObjectAttributes = $valueObject->getEntityAttributes(); |
|
| 570 | 570 | |
| 571 | - // Now (if setup in the entity map) we prefix the value object's |
|
| 572 | - // attributes with the snake_case name of the embedded class. |
|
| 573 | - $prefix = snake_case(class_basename($embed)); |
|
| 571 | + // Now (if setup in the entity map) we prefix the value object's |
|
| 572 | + // attributes with the snake_case name of the embedded class. |
|
| 573 | + $prefix = snake_case(class_basename($embed)); |
|
| 574 | 574 | |
| 575 | - foreach ($valueObjectAttributes as $key=>$value) { |
|
| 576 | - $valueObjectAttributes[$prefix . '_' . $key] = $value; |
|
| 577 | - unset($valueObjectAttributes[$key]); |
|
| 578 | - } |
|
| 575 | + foreach ($valueObjectAttributes as $key=>$value) { |
|
| 576 | + $valueObjectAttributes[$prefix . '_' . $key] = $value; |
|
| 577 | + unset($valueObjectAttributes[$key]); |
|
| 578 | + } |
|
| 579 | 579 | |
| 580 | - $attributes = array_merge($attributes, $valueObjectAttributes); |
|
| 581 | - } |
|
| 580 | + $attributes = array_merge($attributes, $valueObjectAttributes); |
|
| 581 | + } |
|
| 582 | 582 | |
| 583 | - return $attributes; |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - /** |
|
| 587 | - * Return's entity raw attributes in the state they were at last |
|
| 588 | - * query. |
|
| 589 | - * |
|
| 590 | - * @param array|null $columns |
|
| 591 | - * @return array |
|
| 592 | - */ |
|
| 593 | - protected function getCachedRawAttributes(array $columns = null) |
|
| 594 | - { |
|
| 595 | - $cachedAttributes = $this->getCache()->get($this->getEntityId()); |
|
| 596 | - |
|
| 597 | - if (is_null($columns)) { |
|
| 598 | - return $cachedAttributes; |
|
| 599 | - } else { |
|
| 600 | - return array_only($cachedAttributes, $columns); |
|
| 601 | - } |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - /** |
|
| 605 | - * Return a single attribute from the cache |
|
| 606 | - * @param string $key |
|
| 607 | - * @return mixed |
|
| 608 | - */ |
|
| 609 | - protected function getCachedAttribute($key) |
|
| 610 | - { |
|
| 611 | - $cachedAttributes = $this->getCache()->get($this->getEntityId()); |
|
| 612 | - |
|
| 613 | - if (!array_key_exists($key, $cachedAttributes)) { |
|
| 614 | - return null; |
|
| 615 | - } else { |
|
| 616 | - return $cachedAttributes[$key]; |
|
| 617 | - } |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - /** |
|
| 621 | - * Convert related Entity's attributes to foreign keys |
|
| 622 | - * |
|
| 623 | - * @return array |
|
| 624 | - */ |
|
| 625 | - protected function getForeignKeyAttributes() |
|
| 626 | - { |
|
| 627 | - $foreignKeys = []; |
|
| 628 | - |
|
| 629 | - foreach ($this->entityMap->getLocalRelationships() as $relation) { |
|
| 630 | - // check if relationship has been parsed, meaning it has an actual object |
|
| 631 | - // in the entity's attributes |
|
| 632 | - if ($this->isActualRelationships($relation)) { |
|
| 633 | - $foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromRelation($relation); |
|
| 634 | - } |
|
| 635 | - } |
|
| 636 | - |
|
| 637 | - if (!is_null($this->parent)) { |
|
| 638 | - $foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromParent(); |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - return $foreignKeys; |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - /** |
|
| 645 | - * Return an associative array containing the key-value pair(s) from |
|
| 646 | - * the related entity. |
|
| 647 | - * |
|
| 648 | - * @param string $relation |
|
| 649 | - * @return array |
|
| 650 | - */ |
|
| 651 | - protected function getForeignKeyAttributesFromRelation($relation) |
|
| 652 | - { |
|
| 653 | - $localRelations = $this->entityMap->getLocalRelationships(); |
|
| 654 | - |
|
| 655 | - if (in_array($relation, $localRelations)) { |
|
| 656 | - // Call Relationship's method |
|
| 657 | - $relationship = $this->entityMap->$relation($this->getEntityObject()); |
|
| 658 | - |
|
| 659 | - $relatedAggregate = $this->relationships[$relation][0]; |
|
| 660 | - |
|
| 661 | - return $relationship->getForeignKeyValuePair($relatedAggregate->getEntityObject()); |
|
| 662 | - } else { |
|
| 663 | - return []; |
|
| 664 | - } |
|
| 665 | - } |
|
| 666 | - |
|
| 667 | - /** |
|
| 668 | - * Get foreign key attribute(s) from a parent entity in this |
|
| 669 | - * aggregate context |
|
| 670 | - * |
|
| 671 | - * @return array |
|
| 672 | - */ |
|
| 673 | - protected function getForeignKeyAttributesFromParent() |
|
| 674 | - { |
|
| 675 | - $parentMap = $this->parent->getEntityMap(); |
|
| 676 | - |
|
| 677 | - $parentForeignRelations = $parentMap->getForeignRelationships(); |
|
| 678 | - $parentPivotRelations = $parentMap->getPivotRelationships(); |
|
| 679 | - |
|
| 680 | - $parentRelation = $this->parentRelationship; |
|
| 681 | - |
|
| 682 | - if (in_array($parentRelation, $parentForeignRelations) |
|
| 683 | - && !in_array($parentRelation, $parentPivotRelations) |
|
| 684 | - ) { |
|
| 685 | - $parentObject = $this->parent->getEntityObject(); |
|
| 686 | - |
|
| 687 | - // Call Relationship's method on parent map |
|
| 688 | - $relationship = $parentMap->$parentRelation($parentObject); |
|
| 689 | - |
|
| 690 | - return $relationship->getForeignKeyValuePair(); |
|
| 691 | - } else { |
|
| 692 | - return []; |
|
| 693 | - } |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - /** |
|
| 697 | - * Update Pivot records on loaded relationships, by comparing the |
|
| 698 | - * values from the Entity Cache to the actual relationship inside |
|
| 699 | - * the aggregated entity. |
|
| 700 | - * |
|
| 701 | - * @return void |
|
| 702 | - */ |
|
| 703 | - public function updatePivotRecords() |
|
| 704 | - { |
|
| 705 | - $pivots = $this->entityMap->getPivotRelationships(); |
|
| 706 | - |
|
| 707 | - foreach ($pivots as $pivot) { |
|
| 708 | - if (array_key_exists($pivot, $this->relationships)) { |
|
| 709 | - $this->updatePivotRelation($pivot); |
|
| 710 | - } |
|
| 711 | - } |
|
| 712 | - } |
|
| 713 | - |
|
| 714 | - /** |
|
| 715 | - * Update Single pivot relationship |
|
| 716 | - * |
|
| 717 | - * @param string $relation |
|
| 718 | - * @return void |
|
| 719 | - */ |
|
| 720 | - protected function updatePivotRelation($relation) |
|
| 721 | - { |
|
| 722 | - $hashes = $this->getEntityHashesFromRelation($relation); |
|
| 723 | - |
|
| 724 | - $cachedAttributes = $this->getCachedRawAttributes(); |
|
| 725 | - |
|
| 726 | - if (array_key_exists($relation, $cachedAttributes)) { |
|
| 727 | - // Compare the two array of hashes to find out existing |
|
| 728 | - // pivot records, and the ones to be created. |
|
| 729 | - $new = array_diff($hashes, array_keys($cachedAttributes[$relation])); |
|
| 730 | - $existing = array_intersect($hashes, array_keys($cachedAttributes[$relation])); |
|
| 731 | - } else { |
|
| 732 | - $existing = []; |
|
| 733 | - $new = $hashes; |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - if (count($new) > 0) { |
|
| 737 | - $pivots = $this->getRelatedAggregatesFromHashes($new, $relation); |
|
| 738 | - |
|
| 739 | - $this->entityMap->$relation($this->getEntityObject())->createPivots($pivots); |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - if (count($existing) > 0) { |
|
| 743 | - foreach ($existing as $pivotHash) { |
|
| 744 | - $this->updatePivotIfDirty($pivotHash, $relation); |
|
| 745 | - } |
|
| 746 | - } |
|
| 747 | - } |
|
| 748 | - |
|
| 749 | - /** |
|
| 750 | - * Compare existing pivot record in cache and update it |
|
| 751 | - * if the pivot attributes are dirty |
|
| 752 | - * |
|
| 753 | - * @param string $pivotHash |
|
| 754 | - * @param string $relation |
|
| 755 | - * @return void |
|
| 756 | - */ |
|
| 757 | - protected function updatePivotIfDirty($pivotHash, $relation) |
|
| 758 | - { |
|
| 759 | - $aggregate = $this->getRelatedAggregateFromHash($pivotHash, $relation); |
|
| 760 | - |
|
| 761 | - if ($aggregate->hasAttribute('pivot')) { |
|
| 762 | - $pivot = $aggregate->getEntityAttribute('pivot')->getEntityAttributes(); |
|
| 763 | - |
|
| 764 | - $cachedPivotAttributes = $this->getPivotAttributesFromCache($pivotHash, $relation); |
|
| 765 | - |
|
| 766 | - $actualPivotAttributes = array_only($pivot, array_keys($cachedPivotAttributes)); |
|
| 767 | - |
|
| 768 | - $dirty = $this->getDirtyAttributes($actualPivotAttributes, $cachedPivotAttributes); |
|
| 769 | - |
|
| 770 | - if (count($dirty) > 0) { |
|
| 771 | - $id = $aggregate->getEntityId(); |
|
| 772 | - |
|
| 773 | - $this->entityMap->$relation($this->getEntityObject())->updateExistingPivot($id, $dirty); |
|
| 774 | - } |
|
| 775 | - } |
|
| 776 | - } |
|
| 777 | - |
|
| 778 | - /** |
|
| 779 | - * Compare two attributes array and return dirty attributes |
|
| 780 | - * |
|
| 781 | - * @param array $actual |
|
| 782 | - * @param array $cached |
|
| 783 | - * @return array |
|
| 784 | - */ |
|
| 785 | - protected function getDirtyAttributes(array $actual, array $cached) |
|
| 786 | - { |
|
| 787 | - $dirty = []; |
|
| 788 | - |
|
| 789 | - foreach ($actual as $key => $value) { |
|
| 790 | - if (!$this->originalIsNumericallyEquivalent($value, $cached[$key])) { |
|
| 791 | - $dirty[$key] = $actual[$key]; |
|
| 792 | - } |
|
| 793 | - } |
|
| 794 | - |
|
| 795 | - return $dirty; |
|
| 796 | - } |
|
| 797 | - |
|
| 798 | - /** |
|
| 799 | - * |
|
| 800 | - * @param string $pivotHash |
|
| 801 | - * @param string $relation |
|
| 802 | - * @return array |
|
| 803 | - */ |
|
| 804 | - protected function getPivotAttributesFromCache($pivotHash, $relation) |
|
| 805 | - { |
|
| 806 | - $cachedAttributes = $this->getCachedRawAttributes(); |
|
| 807 | - |
|
| 808 | - $cachedRelations = $cachedAttributes[$relation]; |
|
| 809 | - |
|
| 810 | - foreach ($cachedRelations as $cachedRelation) { |
|
| 811 | - if ($cachedRelation == $pivotHash) { |
|
| 812 | - return $cachedRelation->getPivotAttributes(); |
|
| 813 | - } |
|
| 814 | - } |
|
| 815 | - } |
|
| 816 | - |
|
| 817 | - /** |
|
| 818 | - * Returns an array of related Aggregates from its entity hashes |
|
| 819 | - * |
|
| 820 | - * @param array $hashes |
|
| 821 | - * @param string $relation |
|
| 822 | - * @return array |
|
| 823 | - */ |
|
| 824 | - protected function getRelatedAggregatesFromHashes(array $hashes, $relation) |
|
| 825 | - { |
|
| 826 | - $related = []; |
|
| 827 | - |
|
| 828 | - foreach ($hashes as $hash) { |
|
| 829 | - $aggregate = $this->getRelatedAggregateFromHash($hash, $relation); |
|
| 830 | - |
|
| 831 | - if (!is_null($aggregate)) { |
|
| 832 | - $related[] = $aggregate; |
|
| 833 | - } |
|
| 834 | - } |
|
| 835 | - |
|
| 836 | - return $related; |
|
| 837 | - } |
|
| 838 | - |
|
| 839 | - /** |
|
| 840 | - * Get related aggregate from its hash |
|
| 841 | - * |
|
| 842 | - * @param string $hash |
|
| 843 | - * @param string $relation |
|
| 844 | - * @return \Analogue\ORM\System\Aggregate|null |
|
| 845 | - */ |
|
| 846 | - protected function getRelatedAggregateFromHash($hash, $relation) |
|
| 847 | - { |
|
| 848 | - foreach ($this->relationships[$relation] as $aggregate) { |
|
| 849 | - if ($aggregate->getEntityHash() == $hash) { |
|
| 850 | - return $aggregate; |
|
| 851 | - } |
|
| 852 | - } |
|
| 853 | - return null; |
|
| 854 | - } |
|
| 855 | - |
|
| 856 | - /** |
|
| 857 | - * Return an array of Entity Hashes from a specific relation |
|
| 858 | - * |
|
| 859 | - * @param string $relation |
|
| 860 | - * @return array |
|
| 861 | - */ |
|
| 862 | - protected function getEntityHashesFromRelation($relation) |
|
| 863 | - { |
|
| 864 | - return array_map(function ($aggregate) { |
|
| 865 | - return $aggregate->getEntityHash(); |
|
| 866 | - }, $this->relationships[$relation]); |
|
| 867 | - } |
|
| 868 | - |
|
| 869 | - /** |
|
| 870 | - * Check the existence of an actual relationship |
|
| 871 | - * |
|
| 872 | - * @param string $relation |
|
| 873 | - * @return boolean |
|
| 874 | - */ |
|
| 875 | - protected function isActualRelationships($relation) |
|
| 876 | - { |
|
| 877 | - return array_key_exists($relation, $this->relationships) |
|
| 878 | - && count($this->relationships[$relation]) > 0; |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - /** |
|
| 882 | - * Return cache instance for the current entity type |
|
| 883 | - * |
|
| 884 | - * @return \Analogue\ORM\System\EntityCache |
|
| 885 | - */ |
|
| 886 | - protected function getCache() |
|
| 887 | - { |
|
| 888 | - return $this->mapper->getEntityCache(); |
|
| 889 | - } |
|
| 890 | - |
|
| 891 | - /** |
|
| 892 | - * Get Only Raw Entiy's attributes which have been modified |
|
| 893 | - * since last query |
|
| 894 | - * |
|
| 895 | - * @return array |
|
| 896 | - */ |
|
| 897 | - public function getDirtyRawAttributes() |
|
| 898 | - { |
|
| 899 | - $attributes = $this->getRawAttributes(); |
|
| 900 | - $cachedAttributes = $this->getCachedRawAttributes(array_keys($attributes)); |
|
| 901 | - |
|
| 902 | - $dirty = []; |
|
| 903 | - |
|
| 904 | - foreach ($attributes as $key => $value) { |
|
| 905 | - if ($this->isRelation($key) || $key == 'pivot') { |
|
| 906 | - continue; |
|
| 907 | - } |
|
| 908 | - |
|
| 909 | - if (!array_key_exists($key, $cachedAttributes) && !$value instanceof Pivot) { |
|
| 910 | - $dirty[$key] = $value; |
|
| 911 | - } elseif ($value !== $cachedAttributes[$key] && |
|
| 912 | - !$this->originalIsNumericallyEquivalent($value, $cachedAttributes[$key])) { |
|
| 913 | - $dirty[$key] = $value; |
|
| 914 | - } |
|
| 915 | - } |
|
| 916 | - |
|
| 917 | - return $dirty; |
|
| 918 | - } |
|
| 919 | - |
|
| 920 | - /** |
|
| 921 | - * @param $key |
|
| 922 | - * @return bool |
|
| 923 | - */ |
|
| 924 | - protected function isRelation($key) |
|
| 925 | - { |
|
| 926 | - return in_array($key, $this->entityMap->getRelationships()); |
|
| 927 | - } |
|
| 928 | - |
|
| 929 | - /** |
|
| 930 | - * Determine if the new and old values for a given key are numerically equivalent. |
|
| 931 | - * |
|
| 932 | - * @param $current |
|
| 933 | - * @param $original |
|
| 934 | - * @return boolean |
|
| 935 | - */ |
|
| 936 | - protected function originalIsNumericallyEquivalent($current, $original) |
|
| 937 | - { |
|
| 938 | - return is_numeric($current) && is_numeric($original) && strcmp((string) $current, (string) $original) === 0; |
|
| 939 | - } |
|
| 940 | - |
|
| 941 | - /** |
|
| 942 | - * Get the underlying entity object |
|
| 943 | - * |
|
| 944 | - * @return mixed |
|
| 945 | - */ |
|
| 946 | - public function getEntityObject() |
|
| 947 | - { |
|
| 948 | - return $this->wrappedEntity->getObject(); |
|
| 949 | - } |
|
| 950 | - |
|
| 951 | - /** |
|
| 952 | - * Return the Mapper instance for the current Entity Type |
|
| 953 | - * |
|
| 954 | - * @return \Analogue\ORM\System\Mapper |
|
| 955 | - */ |
|
| 956 | - public function getMapper() |
|
| 957 | - { |
|
| 958 | - return $this->mapper; |
|
| 959 | - } |
|
| 960 | - |
|
| 961 | - /** |
|
| 962 | - * Check that the entity already exists in the database, by checking |
|
| 963 | - * if it has an EntityCache record |
|
| 964 | - * |
|
| 965 | - * @return boolean |
|
| 966 | - */ |
|
| 967 | - public function exists() |
|
| 968 | - { |
|
| 969 | - return $this->getCache()->has($this->getEntityId()); |
|
| 970 | - } |
|
| 971 | - |
|
| 972 | - /** |
|
| 973 | - * Set the object attribute raw values (hydration) |
|
| 974 | - * |
|
| 975 | - * @param array $attributes |
|
| 976 | - */ |
|
| 977 | - public function setEntityAttributes(array $attributes) |
|
| 978 | - { |
|
| 979 | - $this->wrappedEntity->setEntityAttributes($attributes); |
|
| 980 | - } |
|
| 981 | - |
|
| 982 | - /** |
|
| 983 | - * Get the raw object's values. |
|
| 984 | - * |
|
| 985 | - * @return array |
|
| 986 | - */ |
|
| 987 | - public function getEntityAttributes() |
|
| 988 | - { |
|
| 989 | - return $this->wrappedEntity->getEntityAttributes(); |
|
| 990 | - } |
|
| 991 | - |
|
| 992 | - /** |
|
| 993 | - * Set the raw entity attributes |
|
| 994 | - * @param string $key |
|
| 995 | - * @param string $value |
|
| 996 | - */ |
|
| 997 | - public function setEntityAttribute($key, $value) |
|
| 998 | - { |
|
| 999 | - $this->wrappedEntity->setEntityAttribute($key, $value); |
|
| 1000 | - } |
|
| 1001 | - |
|
| 1002 | - /** |
|
| 1003 | - * Return the entity's attribute |
|
| 1004 | - * @param string $key |
|
| 1005 | - * @return mixed |
|
| 1006 | - */ |
|
| 1007 | - public function getEntityAttribute($key) |
|
| 1008 | - { |
|
| 1009 | - return $this->wrappedEntity->getEntityAttribute($key); |
|
| 1010 | - } |
|
| 1011 | - |
|
| 1012 | - /** |
|
| 1013 | - * Does the attribute exists on the entity |
|
| 1014 | - * |
|
| 1015 | - * @param string $key |
|
| 1016 | - * @return boolean |
|
| 1017 | - */ |
|
| 1018 | - public function hasAttribute($key) |
|
| 1019 | - { |
|
| 1020 | - return $this->wrappedEntity->hasAttribute($key); |
|
| 1021 | - } |
|
| 1022 | - |
|
| 1023 | - /** |
|
| 1024 | - * Set the lazyloading proxies on the wrapped entity |
|
| 1025 | - */ |
|
| 1026 | - public function setProxies() |
|
| 1027 | - { |
|
| 1028 | - $this->wrappedEntity->setProxies(); |
|
| 1029 | - } |
|
| 583 | + return $attributes; |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + /** |
|
| 587 | + * Return's entity raw attributes in the state they were at last |
|
| 588 | + * query. |
|
| 589 | + * |
|
| 590 | + * @param array|null $columns |
|
| 591 | + * @return array |
|
| 592 | + */ |
|
| 593 | + protected function getCachedRawAttributes(array $columns = null) |
|
| 594 | + { |
|
| 595 | + $cachedAttributes = $this->getCache()->get($this->getEntityId()); |
|
| 596 | + |
|
| 597 | + if (is_null($columns)) { |
|
| 598 | + return $cachedAttributes; |
|
| 599 | + } else { |
|
| 600 | + return array_only($cachedAttributes, $columns); |
|
| 601 | + } |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + /** |
|
| 605 | + * Return a single attribute from the cache |
|
| 606 | + * @param string $key |
|
| 607 | + * @return mixed |
|
| 608 | + */ |
|
| 609 | + protected function getCachedAttribute($key) |
|
| 610 | + { |
|
| 611 | + $cachedAttributes = $this->getCache()->get($this->getEntityId()); |
|
| 612 | + |
|
| 613 | + if (!array_key_exists($key, $cachedAttributes)) { |
|
| 614 | + return null; |
|
| 615 | + } else { |
|
| 616 | + return $cachedAttributes[$key]; |
|
| 617 | + } |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + /** |
|
| 621 | + * Convert related Entity's attributes to foreign keys |
|
| 622 | + * |
|
| 623 | + * @return array |
|
| 624 | + */ |
|
| 625 | + protected function getForeignKeyAttributes() |
|
| 626 | + { |
|
| 627 | + $foreignKeys = []; |
|
| 628 | + |
|
| 629 | + foreach ($this->entityMap->getLocalRelationships() as $relation) { |
|
| 630 | + // check if relationship has been parsed, meaning it has an actual object |
|
| 631 | + // in the entity's attributes |
|
| 632 | + if ($this->isActualRelationships($relation)) { |
|
| 633 | + $foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromRelation($relation); |
|
| 634 | + } |
|
| 635 | + } |
|
| 636 | + |
|
| 637 | + if (!is_null($this->parent)) { |
|
| 638 | + $foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromParent(); |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + return $foreignKeys; |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + /** |
|
| 645 | + * Return an associative array containing the key-value pair(s) from |
|
| 646 | + * the related entity. |
|
| 647 | + * |
|
| 648 | + * @param string $relation |
|
| 649 | + * @return array |
|
| 650 | + */ |
|
| 651 | + protected function getForeignKeyAttributesFromRelation($relation) |
|
| 652 | + { |
|
| 653 | + $localRelations = $this->entityMap->getLocalRelationships(); |
|
| 654 | + |
|
| 655 | + if (in_array($relation, $localRelations)) { |
|
| 656 | + // Call Relationship's method |
|
| 657 | + $relationship = $this->entityMap->$relation($this->getEntityObject()); |
|
| 658 | + |
|
| 659 | + $relatedAggregate = $this->relationships[$relation][0]; |
|
| 660 | + |
|
| 661 | + return $relationship->getForeignKeyValuePair($relatedAggregate->getEntityObject()); |
|
| 662 | + } else { |
|
| 663 | + return []; |
|
| 664 | + } |
|
| 665 | + } |
|
| 666 | + |
|
| 667 | + /** |
|
| 668 | + * Get foreign key attribute(s) from a parent entity in this |
|
| 669 | + * aggregate context |
|
| 670 | + * |
|
| 671 | + * @return array |
|
| 672 | + */ |
|
| 673 | + protected function getForeignKeyAttributesFromParent() |
|
| 674 | + { |
|
| 675 | + $parentMap = $this->parent->getEntityMap(); |
|
| 676 | + |
|
| 677 | + $parentForeignRelations = $parentMap->getForeignRelationships(); |
|
| 678 | + $parentPivotRelations = $parentMap->getPivotRelationships(); |
|
| 679 | + |
|
| 680 | + $parentRelation = $this->parentRelationship; |
|
| 681 | + |
|
| 682 | + if (in_array($parentRelation, $parentForeignRelations) |
|
| 683 | + && !in_array($parentRelation, $parentPivotRelations) |
|
| 684 | + ) { |
|
| 685 | + $parentObject = $this->parent->getEntityObject(); |
|
| 686 | + |
|
| 687 | + // Call Relationship's method on parent map |
|
| 688 | + $relationship = $parentMap->$parentRelation($parentObject); |
|
| 689 | + |
|
| 690 | + return $relationship->getForeignKeyValuePair(); |
|
| 691 | + } else { |
|
| 692 | + return []; |
|
| 693 | + } |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + /** |
|
| 697 | + * Update Pivot records on loaded relationships, by comparing the |
|
| 698 | + * values from the Entity Cache to the actual relationship inside |
|
| 699 | + * the aggregated entity. |
|
| 700 | + * |
|
| 701 | + * @return void |
|
| 702 | + */ |
|
| 703 | + public function updatePivotRecords() |
|
| 704 | + { |
|
| 705 | + $pivots = $this->entityMap->getPivotRelationships(); |
|
| 706 | + |
|
| 707 | + foreach ($pivots as $pivot) { |
|
| 708 | + if (array_key_exists($pivot, $this->relationships)) { |
|
| 709 | + $this->updatePivotRelation($pivot); |
|
| 710 | + } |
|
| 711 | + } |
|
| 712 | + } |
|
| 713 | + |
|
| 714 | + /** |
|
| 715 | + * Update Single pivot relationship |
|
| 716 | + * |
|
| 717 | + * @param string $relation |
|
| 718 | + * @return void |
|
| 719 | + */ |
|
| 720 | + protected function updatePivotRelation($relation) |
|
| 721 | + { |
|
| 722 | + $hashes = $this->getEntityHashesFromRelation($relation); |
|
| 723 | + |
|
| 724 | + $cachedAttributes = $this->getCachedRawAttributes(); |
|
| 725 | + |
|
| 726 | + if (array_key_exists($relation, $cachedAttributes)) { |
|
| 727 | + // Compare the two array of hashes to find out existing |
|
| 728 | + // pivot records, and the ones to be created. |
|
| 729 | + $new = array_diff($hashes, array_keys($cachedAttributes[$relation])); |
|
| 730 | + $existing = array_intersect($hashes, array_keys($cachedAttributes[$relation])); |
|
| 731 | + } else { |
|
| 732 | + $existing = []; |
|
| 733 | + $new = $hashes; |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + if (count($new) > 0) { |
|
| 737 | + $pivots = $this->getRelatedAggregatesFromHashes($new, $relation); |
|
| 738 | + |
|
| 739 | + $this->entityMap->$relation($this->getEntityObject())->createPivots($pivots); |
|
| 740 | + } |
|
| 741 | + |
|
| 742 | + if (count($existing) > 0) { |
|
| 743 | + foreach ($existing as $pivotHash) { |
|
| 744 | + $this->updatePivotIfDirty($pivotHash, $relation); |
|
| 745 | + } |
|
| 746 | + } |
|
| 747 | + } |
|
| 748 | + |
|
| 749 | + /** |
|
| 750 | + * Compare existing pivot record in cache and update it |
|
| 751 | + * if the pivot attributes are dirty |
|
| 752 | + * |
|
| 753 | + * @param string $pivotHash |
|
| 754 | + * @param string $relation |
|
| 755 | + * @return void |
|
| 756 | + */ |
|
| 757 | + protected function updatePivotIfDirty($pivotHash, $relation) |
|
| 758 | + { |
|
| 759 | + $aggregate = $this->getRelatedAggregateFromHash($pivotHash, $relation); |
|
| 760 | + |
|
| 761 | + if ($aggregate->hasAttribute('pivot')) { |
|
| 762 | + $pivot = $aggregate->getEntityAttribute('pivot')->getEntityAttributes(); |
|
| 763 | + |
|
| 764 | + $cachedPivotAttributes = $this->getPivotAttributesFromCache($pivotHash, $relation); |
|
| 765 | + |
|
| 766 | + $actualPivotAttributes = array_only($pivot, array_keys($cachedPivotAttributes)); |
|
| 767 | + |
|
| 768 | + $dirty = $this->getDirtyAttributes($actualPivotAttributes, $cachedPivotAttributes); |
|
| 769 | + |
|
| 770 | + if (count($dirty) > 0) { |
|
| 771 | + $id = $aggregate->getEntityId(); |
|
| 772 | + |
|
| 773 | + $this->entityMap->$relation($this->getEntityObject())->updateExistingPivot($id, $dirty); |
|
| 774 | + } |
|
| 775 | + } |
|
| 776 | + } |
|
| 777 | + |
|
| 778 | + /** |
|
| 779 | + * Compare two attributes array and return dirty attributes |
|
| 780 | + * |
|
| 781 | + * @param array $actual |
|
| 782 | + * @param array $cached |
|
| 783 | + * @return array |
|
| 784 | + */ |
|
| 785 | + protected function getDirtyAttributes(array $actual, array $cached) |
|
| 786 | + { |
|
| 787 | + $dirty = []; |
|
| 788 | + |
|
| 789 | + foreach ($actual as $key => $value) { |
|
| 790 | + if (!$this->originalIsNumericallyEquivalent($value, $cached[$key])) { |
|
| 791 | + $dirty[$key] = $actual[$key]; |
|
| 792 | + } |
|
| 793 | + } |
|
| 794 | + |
|
| 795 | + return $dirty; |
|
| 796 | + } |
|
| 797 | + |
|
| 798 | + /** |
|
| 799 | + * |
|
| 800 | + * @param string $pivotHash |
|
| 801 | + * @param string $relation |
|
| 802 | + * @return array |
|
| 803 | + */ |
|
| 804 | + protected function getPivotAttributesFromCache($pivotHash, $relation) |
|
| 805 | + { |
|
| 806 | + $cachedAttributes = $this->getCachedRawAttributes(); |
|
| 807 | + |
|
| 808 | + $cachedRelations = $cachedAttributes[$relation]; |
|
| 809 | + |
|
| 810 | + foreach ($cachedRelations as $cachedRelation) { |
|
| 811 | + if ($cachedRelation == $pivotHash) { |
|
| 812 | + return $cachedRelation->getPivotAttributes(); |
|
| 813 | + } |
|
| 814 | + } |
|
| 815 | + } |
|
| 816 | + |
|
| 817 | + /** |
|
| 818 | + * Returns an array of related Aggregates from its entity hashes |
|
| 819 | + * |
|
| 820 | + * @param array $hashes |
|
| 821 | + * @param string $relation |
|
| 822 | + * @return array |
|
| 823 | + */ |
|
| 824 | + protected function getRelatedAggregatesFromHashes(array $hashes, $relation) |
|
| 825 | + { |
|
| 826 | + $related = []; |
|
| 827 | + |
|
| 828 | + foreach ($hashes as $hash) { |
|
| 829 | + $aggregate = $this->getRelatedAggregateFromHash($hash, $relation); |
|
| 830 | + |
|
| 831 | + if (!is_null($aggregate)) { |
|
| 832 | + $related[] = $aggregate; |
|
| 833 | + } |
|
| 834 | + } |
|
| 835 | + |
|
| 836 | + return $related; |
|
| 837 | + } |
|
| 838 | + |
|
| 839 | + /** |
|
| 840 | + * Get related aggregate from its hash |
|
| 841 | + * |
|
| 842 | + * @param string $hash |
|
| 843 | + * @param string $relation |
|
| 844 | + * @return \Analogue\ORM\System\Aggregate|null |
|
| 845 | + */ |
|
| 846 | + protected function getRelatedAggregateFromHash($hash, $relation) |
|
| 847 | + { |
|
| 848 | + foreach ($this->relationships[$relation] as $aggregate) { |
|
| 849 | + if ($aggregate->getEntityHash() == $hash) { |
|
| 850 | + return $aggregate; |
|
| 851 | + } |
|
| 852 | + } |
|
| 853 | + return null; |
|
| 854 | + } |
|
| 855 | + |
|
| 856 | + /** |
|
| 857 | + * Return an array of Entity Hashes from a specific relation |
|
| 858 | + * |
|
| 859 | + * @param string $relation |
|
| 860 | + * @return array |
|
| 861 | + */ |
|
| 862 | + protected function getEntityHashesFromRelation($relation) |
|
| 863 | + { |
|
| 864 | + return array_map(function ($aggregate) { |
|
| 865 | + return $aggregate->getEntityHash(); |
|
| 866 | + }, $this->relationships[$relation]); |
|
| 867 | + } |
|
| 868 | + |
|
| 869 | + /** |
|
| 870 | + * Check the existence of an actual relationship |
|
| 871 | + * |
|
| 872 | + * @param string $relation |
|
| 873 | + * @return boolean |
|
| 874 | + */ |
|
| 875 | + protected function isActualRelationships($relation) |
|
| 876 | + { |
|
| 877 | + return array_key_exists($relation, $this->relationships) |
|
| 878 | + && count($this->relationships[$relation]) > 0; |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + /** |
|
| 882 | + * Return cache instance for the current entity type |
|
| 883 | + * |
|
| 884 | + * @return \Analogue\ORM\System\EntityCache |
|
| 885 | + */ |
|
| 886 | + protected function getCache() |
|
| 887 | + { |
|
| 888 | + return $this->mapper->getEntityCache(); |
|
| 889 | + } |
|
| 890 | + |
|
| 891 | + /** |
|
| 892 | + * Get Only Raw Entiy's attributes which have been modified |
|
| 893 | + * since last query |
|
| 894 | + * |
|
| 895 | + * @return array |
|
| 896 | + */ |
|
| 897 | + public function getDirtyRawAttributes() |
|
| 898 | + { |
|
| 899 | + $attributes = $this->getRawAttributes(); |
|
| 900 | + $cachedAttributes = $this->getCachedRawAttributes(array_keys($attributes)); |
|
| 901 | + |
|
| 902 | + $dirty = []; |
|
| 903 | + |
|
| 904 | + foreach ($attributes as $key => $value) { |
|
| 905 | + if ($this->isRelation($key) || $key == 'pivot') { |
|
| 906 | + continue; |
|
| 907 | + } |
|
| 908 | + |
|
| 909 | + if (!array_key_exists($key, $cachedAttributes) && !$value instanceof Pivot) { |
|
| 910 | + $dirty[$key] = $value; |
|
| 911 | + } elseif ($value !== $cachedAttributes[$key] && |
|
| 912 | + !$this->originalIsNumericallyEquivalent($value, $cachedAttributes[$key])) { |
|
| 913 | + $dirty[$key] = $value; |
|
| 914 | + } |
|
| 915 | + } |
|
| 916 | + |
|
| 917 | + return $dirty; |
|
| 918 | + } |
|
| 919 | + |
|
| 920 | + /** |
|
| 921 | + * @param $key |
|
| 922 | + * @return bool |
|
| 923 | + */ |
|
| 924 | + protected function isRelation($key) |
|
| 925 | + { |
|
| 926 | + return in_array($key, $this->entityMap->getRelationships()); |
|
| 927 | + } |
|
| 928 | + |
|
| 929 | + /** |
|
| 930 | + * Determine if the new and old values for a given key are numerically equivalent. |
|
| 931 | + * |
|
| 932 | + * @param $current |
|
| 933 | + * @param $original |
|
| 934 | + * @return boolean |
|
| 935 | + */ |
|
| 936 | + protected function originalIsNumericallyEquivalent($current, $original) |
|
| 937 | + { |
|
| 938 | + return is_numeric($current) && is_numeric($original) && strcmp((string) $current, (string) $original) === 0; |
|
| 939 | + } |
|
| 940 | + |
|
| 941 | + /** |
|
| 942 | + * Get the underlying entity object |
|
| 943 | + * |
|
| 944 | + * @return mixed |
|
| 945 | + */ |
|
| 946 | + public function getEntityObject() |
|
| 947 | + { |
|
| 948 | + return $this->wrappedEntity->getObject(); |
|
| 949 | + } |
|
| 950 | + |
|
| 951 | + /** |
|
| 952 | + * Return the Mapper instance for the current Entity Type |
|
| 953 | + * |
|
| 954 | + * @return \Analogue\ORM\System\Mapper |
|
| 955 | + */ |
|
| 956 | + public function getMapper() |
|
| 957 | + { |
|
| 958 | + return $this->mapper; |
|
| 959 | + } |
|
| 960 | + |
|
| 961 | + /** |
|
| 962 | + * Check that the entity already exists in the database, by checking |
|
| 963 | + * if it has an EntityCache record |
|
| 964 | + * |
|
| 965 | + * @return boolean |
|
| 966 | + */ |
|
| 967 | + public function exists() |
|
| 968 | + { |
|
| 969 | + return $this->getCache()->has($this->getEntityId()); |
|
| 970 | + } |
|
| 971 | + |
|
| 972 | + /** |
|
| 973 | + * Set the object attribute raw values (hydration) |
|
| 974 | + * |
|
| 975 | + * @param array $attributes |
|
| 976 | + */ |
|
| 977 | + public function setEntityAttributes(array $attributes) |
|
| 978 | + { |
|
| 979 | + $this->wrappedEntity->setEntityAttributes($attributes); |
|
| 980 | + } |
|
| 981 | + |
|
| 982 | + /** |
|
| 983 | + * Get the raw object's values. |
|
| 984 | + * |
|
| 985 | + * @return array |
|
| 986 | + */ |
|
| 987 | + public function getEntityAttributes() |
|
| 988 | + { |
|
| 989 | + return $this->wrappedEntity->getEntityAttributes(); |
|
| 990 | + } |
|
| 991 | + |
|
| 992 | + /** |
|
| 993 | + * Set the raw entity attributes |
|
| 994 | + * @param string $key |
|
| 995 | + * @param string $value |
|
| 996 | + */ |
|
| 997 | + public function setEntityAttribute($key, $value) |
|
| 998 | + { |
|
| 999 | + $this->wrappedEntity->setEntityAttribute($key, $value); |
|
| 1000 | + } |
|
| 1001 | + |
|
| 1002 | + /** |
|
| 1003 | + * Return the entity's attribute |
|
| 1004 | + * @param string $key |
|
| 1005 | + * @return mixed |
|
| 1006 | + */ |
|
| 1007 | + public function getEntityAttribute($key) |
|
| 1008 | + { |
|
| 1009 | + return $this->wrappedEntity->getEntityAttribute($key); |
|
| 1010 | + } |
|
| 1011 | + |
|
| 1012 | + /** |
|
| 1013 | + * Does the attribute exists on the entity |
|
| 1014 | + * |
|
| 1015 | + * @param string $key |
|
| 1016 | + * @return boolean |
|
| 1017 | + */ |
|
| 1018 | + public function hasAttribute($key) |
|
| 1019 | + { |
|
| 1020 | + return $this->wrappedEntity->hasAttribute($key); |
|
| 1021 | + } |
|
| 1022 | + |
|
| 1023 | + /** |
|
| 1024 | + * Set the lazyloading proxies on the wrapped entity |
|
| 1025 | + */ |
|
| 1026 | + public function setProxies() |
|
| 1027 | + { |
|
| 1028 | + $this->wrappedEntity->setProxies(); |
|
| 1029 | + } |
|
| 1030 | 1030 | } |