Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add RepositoryAlias
This is an attempt to resolve the problem of entity source being
ambiguous.

This permits (though does not ensure) that an entity has the means to
say which table object it came from. alias and repository alias are not
the same thing, repository alias is plugin-prefixed, whereas alias is
not.
  • Loading branch information
AD7six committed Feb 10, 2015
1 parent 5a3a48b commit a23e59f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/ORM/Marshaller.php
Expand Up @@ -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) {
Expand Down
38 changes: 34 additions & 4 deletions src/ORM/Table.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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']);
}
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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']],
Expand Down
1 change: 1 addition & 0 deletions src/ORM/TableRegistry.php
Expand Up @@ -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') {
Expand Down

0 comments on commit a23e59f

Please sign in to comment.