Browse Source

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.
AD7six 11 years ago
parent
commit
a23e59f602
3 changed files with 36 additions and 5 deletions
  1. 1 1
      src/ORM/Marshaller.php
  2. 34 4
      src/ORM/Table.php
  3. 1 0
      src/ORM/TableRegistry.php

+ 1 - 1
src/ORM/Marshaller.php

@@ -107,7 +107,7 @@ class Marshaller
         $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) {

+ 34 - 4
src/ORM/Table.php

@@ -192,6 +192,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
      *
      * @var array
@@ -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']);
         }
@@ -349,6 +359,23 @@ class Table implements RepositoryInterface, EventListenerInterface
     }
 
     /**
+     * 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
      *
      * @param \Cake\Database\Connection|null $conn The new connection instance
@@ -1379,7 +1406,7 @@ class Table implements RepositoryInterface, EventListenerInterface
                 $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 @@ class Table implements RepositoryInterface, EventListenerInterface
     {
         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 @@ class Table implements RepositoryInterface, EventListenerInterface
         }
         $entity = new Entity(
             $options['data'],
-            ['useSetters' => false, 'markNew' => $options['newRecord']]
+            [
+                'useSetters' => false,
+                'markNew' => $options['newRecord'],
+                'source' => $this->repositoryAlias()
+            ]
         );
         $fields = array_merge(
             [$options['field']],

+ 1 - 0
src/ORM/TableRegistry.php

@@ -183,6 +183,7 @@ class TableRegistry
             $options['connection'] = ConnectionManager::get($connectionName);
         }
 
+        $options['source'] = $alias;
         static::$_instances[$alias] = new $options['className']($options);
 
         if ($options['className'] === 'Cake\ORM\Table') {