diff --git a/src/ORM/Marshaller.php b/src/ORM/Marshaller.php index 476cb4011ec..316313e4726 100644 --- a/src/ORM/Marshaller.php +++ b/src/ORM/Marshaller.php @@ -107,7 +107,7 @@ public function one(array $data, array $options = []) $schema = $this->_table->schema(); $entityClass = $this->_table->entityClass(); $entity = new $entityClass(); - $entity->source($this->_table->alias()); + $entity->source($this->_table->repositoryAlias()); if (isset($options['accessibleFields'])) { foreach ((array)$options['accessibleFields'] as $key => $value) { diff --git a/src/ORM/Table.php b/src/ORM/Table.php index a14aa9b6900..0fee7d421ac 100644 --- a/src/ORM/Table.php +++ b/src/ORM/Table.php @@ -191,6 +191,13 @@ class Table implements RepositoryInterface, EventListenerInterface */ protected $_entityClass; + /** + * Registry key used to create this table object + * + * @var string + */ + protected $_repositoryAlias; + /** * A list of validation objects indexed by name * @@ -228,6 +235,9 @@ class Table implements RepositoryInterface, EventListenerInterface */ public function __construct(array $config = []) { + if (!empty($config['repositoryAlias'])) { + $this->repositoryAlias($config['repositoryAlias']); + } if (!empty($config['table'])) { $this->table($config['table']); } @@ -348,6 +358,23 @@ public function alias($alias = null) return $this->_alias; } + /** + * Returns the table registry key used to create this table instance + * + * @param string|null $repositoryAlias the key used to access this object + * @return string + */ + public function repositoryAlias($repositoryAlias = null) + { + if ($repositoryAlias !== null) { + $this->_repositoryAlias = $repositoryAlias; + } + if ($this->_repositoryAlias === null) { + $this->_repositoryAlias = Inflector::camelize($this->alias()); + } + return $this->_repositoryAlias; + } + /** * Returns the connection instance or sets a new one * @@ -1379,7 +1406,7 @@ protected function _processSave($entity, $options) $entity->clean(); $this->dispatchEvent('Model.afterSave', compact('entity', 'options')); $entity->isNew(false); - $entity->source($this->alias()); + $entity->source($this->repositoryAlias()); $success = true; } } @@ -1845,8 +1872,7 @@ public function newEntity($data = null, array $options = []) { if ($data === null) { $class = $this->entityClass(); - $entity = new $class; - $entity->source($this->alias()); + $entity = new $class(['source' => $this->repositoryAlias()]); return $entity; } if (!isset($options['associated'])) { @@ -2007,7 +2033,11 @@ public function validateUnique($value, array $context, array $options = null) } $entity = new Entity( $options['data'], - ['useSetters' => false, 'markNew' => $options['newRecord']] + [ + 'useSetters' => false, + 'markNew' => $options['newRecord'], + 'source' => $this->repositoryAlias() + ] ); $fields = array_merge( [$options['field']], diff --git a/src/ORM/TableRegistry.php b/src/ORM/TableRegistry.php index 56b6c51ce1a..7917930a5e5 100644 --- a/src/ORM/TableRegistry.php +++ b/src/ORM/TableRegistry.php @@ -183,6 +183,7 @@ public static function get($alias, array $options = []) $options['connection'] = ConnectionManager::get($connectionName); } + $options['source'] = $alias; static::$_instances[$alias] = new $options['className']($options); if ($options['className'] === 'Cake\ORM\Table') {