diff --git a/src/Datasource/EntityTrait.php b/src/Datasource/EntityTrait.php index 05cc6ecf883..c66aa5aa979 100644 --- a/src/Datasource/EntityTrait.php +++ b/src/Datasource/EntityTrait.php @@ -98,12 +98,11 @@ trait EntityTrait { protected $_accessible = []; /** - * An array containing the alias and full table classname from which - * this entity comes. + * The alias of the repository this entity came from * - * @var array + * @var string */ - protected $_repositoryInfo = []; + protected $_repositoryAlias; /** * Magic getter to access properties that has be set in this entity @@ -671,19 +670,19 @@ public function accessible($property, $set = null) { } /** - * Returns an array containing the alias and classname of the repository - * this entity came from, if it is known. + * Returns the alias of the repository from wich this entity came from. * - * If an array is passed, the repository info will be set to it. + * If called with no arguments, it returns the alias of the repository + * this entity came from if it is known. * - * @param array $info the repository info to store - * @return array + * @param string the alias of the repository + * @return string */ - public function source(array $info = null) { - if ($info === null) { - return $this->_repositoryInfo; + public function source($alias = null) { + if ($alias === null) { + return $this->_repositoryAlias; } - $this->_repositoryInfo = $info; + $this->_repositoryAlias = $alias; } /** @@ -709,7 +708,7 @@ public function __debugInfo() { 'dirty' => $this->_dirty, 'virtual' => $this->_virtual, 'errors' => $this->_errors, - 'repository' => $this->_repositoryInfo + 'repository' => $this->_repositoryAlias ]; } diff --git a/src/ORM/Association/BelongsToMany.php b/src/ORM/Association/BelongsToMany.php index 1a6693fbcc7..6640c5d0cee 100644 --- a/src/ORM/Association/BelongsToMany.php +++ b/src/ORM/Association/BelongsToMany.php @@ -514,10 +514,7 @@ protected function _saveLinks(Entity $sourceEntity, $targetEntities, $options) { $targetPrimaryKey = (array)$target->primaryKey(); $sourcePrimaryKey = (array)$source->primaryKey(); $jointProperty = $this->_junctionProperty; - $junctionInfo = [ - 'alias' => $junction->alias(), - 'className' => get_class($junction) - ]; + $junctionAlias = $junction->alias(); foreach ($targetEntities as $k => $e) { $joint = $e->get($jointProperty); @@ -539,7 +536,7 @@ protected function _saveLinks(Entity $sourceEntity, $targetEntities, $options) { $e->set($jointProperty, $joint); $e->dirty($jointProperty, false); - $joint->source($junctionInfo); + $joint->source($junctionAlias); } return true; diff --git a/src/ORM/Marshaller.php b/src/ORM/Marshaller.php index 0ed4c28f21c..5c3c60ee9d4 100644 --- a/src/ORM/Marshaller.php +++ b/src/ORM/Marshaller.php @@ -98,10 +98,7 @@ public function one(array $data, $include = []) { $tableName = $this->_table->alias(); $entityClass = $this->_table->entityClass(); $entity = new $entityClass(); - $entity->source([ - 'alias' => $this->_table->alias(), - 'className' => get_class($this->_table) - ]); + $entity->source($this->_table->alias()); if (isset($data[$tableName])) { $data = $data[$tableName]; diff --git a/src/ORM/ResultSet.php b/src/ORM/ResultSet.php index d2f92d455de..d7b195d80b5 100644 --- a/src/ORM/ResultSet.php +++ b/src/ORM/ResultSet.php @@ -120,13 +120,6 @@ class ResultSet implements Countable, Iterator, Serializable, JsonSerializable { */ protected $_count; -/** - * Contains information about the default table that is used for hydrating - * - * @var array - */ - protected $_sourceInfo = []; - /** * Constructor * @@ -142,10 +135,6 @@ public function __construct($query, $statement) { $this->_hydrate = $this->_query->hydrate(); $this->_entityClass = $repository->entityClass(); $this->_useBuffering = $query->bufferResults(); - $this->_sourceInfo = [ - 'alias' => $repository->alias(), - 'className' => get_class($repository) - ]; if ($statement) { $this->count(); @@ -358,7 +347,6 @@ protected function _fetchResult() { */ protected function _groupResult($row) { $defaultAlias = $this->_defaultTable->alias(); - $defaultClass = get_class($this->_defaultTable); $results = []; foreach ($row as $key => $value) { $table = $defaultAlias; @@ -396,10 +384,7 @@ protected function _groupResult($row) { $instance = $assoc['instance']; $target = $instance->target(); $results[$alias] = $this->_castValues($target, $results[$alias]); - $options['source'] = [ - 'alias' => $target->alias(), - 'className' => get_class($target) - ]; + $options['source'] = $target->alias(); if ($this->_hydrate && $assoc['canBeJoined']) { $entity = new $assoc['entityClass']($results[$alias], $options); @@ -409,7 +394,7 @@ protected function _groupResult($row) { $results = $instance->transformRow($results); } - $options['source'] = $this->_sourceInfo; + $options['source'] = $defaultAlias; $results = $results[$defaultAlias]; if ($this->_hydrate && !($results instanceof Entity)) { $results = new $this->_entityClass($results, $options); diff --git a/src/ORM/Table.php b/src/ORM/Table.php index 5ce2c3ec647..42445bdf314 100644 --- a/src/ORM/Table.php +++ b/src/ORM/Table.php @@ -1170,10 +1170,7 @@ protected function _processSave($entity, $options) { $event = new Event('Model.afterSave', $this, compact('entity', 'options')); $this->getEventManager()->dispatch($event); $entity->isNew(false); - $entity->source([ - 'alias' => $this->alias(), - 'className' => get_class($this) - ]); + $entity->source($this->alias()); $success = true; } } diff --git a/src/View/Form/EntityContext.php b/src/View/Form/EntityContext.php index 6c39964a8f2..e4e2cacc890 100644 --- a/src/View/Form/EntityContext.php +++ b/src/View/Form/EntityContext.php @@ -123,8 +123,7 @@ protected function _prepare() { $isEntity = $entity instanceof Entity; if ($isEntity) { - $source = $entity->source(); - $table = isset($source['alias']) ? $source['alias'] : null; + $table = $entity->source(); } if (!$table && $isEntity && get_class($entity) !== 'Cake\ORM\Entity') { list($ns, $entityClass) = namespaceSplit(get_class($entity)); diff --git a/tests/TestCase/ORM/EntityTest.php b/tests/TestCase/ORM/EntityTest.php index 91221b9549a..50596011bed 100644 --- a/tests/TestCase/ORM/EntityTest.php +++ b/tests/TestCase/ORM/EntityTest.php @@ -1007,10 +1007,7 @@ public function testDebugInfo() { $entity->virtualProperties(['baz']); $entity->dirty('foo', true); $entity->errors('foo', ['An error']); - $entity->source([ - 'alias' => 'foos', - 'className' => 'Something' - ]); + $entity->source('foos'); $result = $entity->__debugInfo(); $expected = [ 'new' => null, @@ -1019,10 +1016,7 @@ public function testDebugInfo() { 'dirty' => ['foo' => true], 'virtual' => ['baz'], 'errors' => ['foo' => ['An error']], - 'repository' => [ - 'alias' => 'foos', - 'className' => 'Something' - ] + 'repository' => 'foos' ]; $this->assertSame($expected, $result); } @@ -1034,10 +1028,9 @@ public function testDebugInfo() { */ public function testSource() { $entity = new Entity; - $this->assertEquals([], $entity->source()); - $source = ['alias' => 'foo', 'className' => 'bar']; - $entity->source($source); - $this->assertEquals($source, $entity->source()); + $this->assertNull($entity->source()); + $entity->source('foos'); + $this->assertEquals('foos', $entity->source()); } } diff --git a/tests/TestCase/ORM/MarshallerTest.php b/tests/TestCase/ORM/MarshallerTest.php index 81cd7476229..f90e0ff6e4c 100644 --- a/tests/TestCase/ORM/MarshallerTest.php +++ b/tests/TestCase/ORM/MarshallerTest.php @@ -110,8 +110,7 @@ public function testOneSimple() { $this->assertEquals($data, $result->toArray()); $this->assertTrue($result->dirty(), 'Should be a dirty entity.'); $this->assertNull($result->isNew(), 'Should be detached'); - $source = ['alias' => 'Articles', 'className' => get_class($this->articles)]; - $this->assertEquals($source, $result->source()); + $this->assertEquals('Articles', $result->source()); } /** diff --git a/tests/TestCase/ORM/ResultSetTest.php b/tests/TestCase/ORM/ResultSetTest.php index a877fbbf656..04d4a58ce3f 100644 --- a/tests/TestCase/ORM/ResultSetTest.php +++ b/tests/TestCase/ORM/ResultSetTest.php @@ -134,10 +134,7 @@ public function testIteratorAfterSerializationHydrated() { foreach ($results as $i => $row) { $expected = new \Cake\ORM\Entity($this->fixtureData[$i]); $expected->isNew(false); - $expected->source([ - 'alias' => $this->table->alias(), - 'className' => get_class($this->table) - ]); + $expected->source($this->table->alias()); $expected->clean(); $this->assertEquals($expected, $row, "Row $i does not match"); } @@ -225,10 +222,7 @@ public function testGroupBy() { $options = [ 'markNew' => false, 'markClean' => true, - 'source' => [ - 'alias' => $this->table->alias(), - 'className' => get_class($this->table) - ] + 'source' => $this->table->alias() ]; $expected = [ 1 => [ diff --git a/tests/TestCase/View/Form/EntityContextTest.php b/tests/TestCase/View/Form/EntityContextTest.php index ca12ad4d511..9a54d03eb95 100644 --- a/tests/TestCase/View/Form/EntityContextTest.php +++ b/tests/TestCase/View/Form/EntityContextTest.php @@ -150,7 +150,7 @@ public function testDefaultEntityError() { */ public function testTableFromEntitySource() { $entity = new Entity; - $entity->source(['alias' => 'Articles']); + $entity->source('Articles'); $context = new EntityContext($this->request, [ 'entity' => $entity, ]);