Skip to content
Permalink
Browse files

Implementing custom entity class for main model result

  • Loading branch information...
lorenzo committed Sep 15, 2013
1 parent 77d4170 commit 9ea437ffa2173ce7e4d083257a7aec4415c7a4c1
Showing with 60 additions and 1 deletion.
  1. +9 −1 lib/Cake/ORM/ResultSet.php
  2. +24 −0 lib/Cake/ORM/Table.php
  3. +27 −0 lib/Cake/Test/TestCase/ORM/QueryTest.php
@@ -104,6 +104,13 @@ class ResultSet implements Iterator, Serializable, JsonSerializable {
*/
protected $_hydrate = false;
/**
* The fully namespaced name of the class to use for hydrating results
*
* @var string
*/
protected $_entityClass;
/**
* Constructor
*
@@ -117,6 +124,7 @@ public function __construct($query, $statement) {
$this->_defaultTable = $this->_query->repository();
$this->_calculateAssociationMap();
$this->_hydrate = $this->_query->hydrate();
$this->_entityClass = $query->repository()->entityClass();
}
/**
@@ -271,7 +279,7 @@ protected function _groupResult($row) {
$results = $results[$defaultAlias];
if ($this->_hydrate && !($results instanceof Entity)) {
$results = (new Entity)->set($results, false);
$results = (new $this->_entityClass)->set($results, false);
}
return $results;
}
@@ -119,6 +119,13 @@ class Table {
*/
protected $_eventManager;
/**
* The name of the class that represent a single row for this table
*
* @var string
*/
protected $_entityClass = '\Cake\ORM\Entity';
/**
* Initializes a new instance
*
@@ -153,6 +160,9 @@ public function __construct($config = []) {
if (!empty($config['schema'])) {
$this->schema($config['schema']);
}
if (!empty($config['entityClass'])) {
$this->entityClass($config['entityClass']);
}
$eventManager = null;
if (!empty($config['eventManager'])) {
$eventManager = $config['eventManager'];
@@ -367,6 +377,20 @@ public function displayField($key = null) {
return $this->_displayField;
}
/**
* Returns the class used to hydrate rows for this table or sets
* a new one
*
* @param string $name the name of the class to use
* @return string
*/
public function entityClass($name = null) {
if ($name !== null) {
$this->_entityClass = $name;
}
return $this->_entityClass;
}
/**
* Returns a association objected configured for the specified alias if any
*
@@ -1251,4 +1251,31 @@ public function testHydrateDeep() {
$this->assertFalse(isset($results[3]->articles));
}
/**
* Tests that it is possible to use a custom entity class
*
* @return void
*/
public function testHydrateCustomObject() {
$this->_createTables();
$class = $this->getMockClass('\Cake\ORM\Entity', ['fakeMethod']);
$table = Table::build('article', [
'table' => 'articles',
'entityClass' => $class
]);
$query = new Query($this->connection, $table);
$results = $query->select()->hydrate(true)->execute()->toArray();
$this->assertCount(3, $results);
foreach ($results as $r) {
$this->assertInstanceOf($class, $r);
}
$first = $results[0];
$this->assertEquals(1, $first->id);
$this->assertEquals(1, $first->author_id);
$this->assertEquals('First Article', $first->title);
$this->assertEquals('First Article Body', $first->body);
$this->assertEquals('Y', $first->published);
}
}

0 comments on commit 9ea437f

Please sign in to comment.
You can’t perform that action at this time.