Skip to content

Commit

Permalink
Making Entity::source() just a string
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 16, 2014
1 parent 0f67c3f commit 8bb695d
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 69 deletions.
27 changes: 13 additions & 14 deletions src/Datasource/EntityTrait.php
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -709,7 +708,7 @@ public function __debugInfo() {
'dirty' => $this->_dirty,
'virtual' => $this->_virtual,
'errors' => $this->_errors,
'repository' => $this->_repositoryInfo
'repository' => $this->_repositoryAlias
];
}

Expand Down
7 changes: 2 additions & 5 deletions src/ORM/Association/BelongsToMany.php
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions src/ORM/Marshaller.php
Expand Up @@ -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];
Expand Down
19 changes: 2 additions & 17 deletions src/ORM/ResultSet.php
Expand Up @@ -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
*
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions src/ORM/Table.php
Expand Up @@ -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;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/View/Form/EntityContext.php
Expand Up @@ -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));
Expand Down
17 changes: 5 additions & 12 deletions tests/TestCase/ORM/EntityTest.php
Expand Up @@ -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,
Expand All @@ -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);
}
Expand All @@ -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());
}

}
3 changes: 1 addition & 2 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -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());
}

/**
Expand Down
10 changes: 2 additions & 8 deletions tests/TestCase/ORM/ResultSetTest.php
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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 => [
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/View/Form/EntityContextTest.php
Expand Up @@ -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,
]);
Expand Down

0 comments on commit 8bb695d

Please sign in to comment.