Skip to content

Commit

Permalink
Merge pull request #12192 from andrew-demb/3.0.x
Browse files Browse the repository at this point in the history
Fixed  #12191 not standard column map in `Phalcon\Mvc\Model::cloneResultMapHydrate`
  • Loading branch information
sergeyklay authored Sep 1, 2016
2 parents ba46c8d + c183b23 commit d369efd
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fixed `Phalcon\Cache\Backend\Memcache::flush` to remove all query keys after flush memcached [#12182](https://github.com/phalcon/cphalcon/issues/12182)
- Fixed `Phalcon\Cache\Backend\Memory::delete` to correct remove items from `Phalcon\Cache\Backend\Memory::$_data`
- Fixed `Phalcon\Cache\Frontend\Data::afterRetrieve`, `Phalcon\Cache\Frontend\Igbinary::afterRetrieve`, `Phalcon\Cache\Frontend\Msgpack::afterRetrieve` to unserialize only raw data [#12186](https://github.com/phalcon/cphalcon/issues/12186)
- Fixed `Phalcon\Mvc\Model::cloneResultMapHydrate` to correct create array/objects from data by column map with types [#12191](https://github.com/phalcon/cphalcon/issues/12191)


# [3.0.1](https://github.com/phalcon/cphalcon/releases/tag/v3.0.1) (2016-08-24)
Expand Down
15 changes: 12 additions & 3 deletions phalcon/mvc/model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
*/
public static function cloneResultMapHydrate(array! data, var columnMap, int hydrationMode)
{
var hydrateArray, hydrateObject, key, value, attribute;
var hydrateArray, hydrateObject, key, value, attribute, attributeName;

/**
* If there is no column map and the hydration mode is arrays return the data as it is
Expand Down Expand Up @@ -649,10 +649,19 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
}
}

/**
* Attribute can store info about his type
*/
if (typeof attribute == "array") {
let attributeName = attribute[0];
} else {
let attributeName = attribute;
}

if hydrationMode == Resultset::HYDRATE_ARRAYS {
let hydrateArray[attribute] = value;
let hydrateArray[attributeName] = value;
} else {
let hydrateObject->{attribute} = value;
let hydrateObject->{attributeName} = value;
}
} else {
if hydrationMode == Resultset::HYDRATE_ARRAYS {
Expand Down
230 changes: 230 additions & 0 deletions unit-tests/ModelsHydrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ public function testModelsMysql()
}, true);

$this->_executeTestsNormal($di);
$this->_executeTestsNormalCastHydrate($di);
$this->_executeTestsRenamed($di);
$this->_executeTestsRenamedCastHydrate($di);
$this->_executeTestsNormalComplex($di);
$this->_executeTestsNormalComplexCastHydrate($di);
}

public function testModelsPostgresql()
Expand All @@ -92,8 +95,11 @@ public function testModelsPostgresql()
}, true);

$this->_executeTestsNormal($di);
$this->_executeTestsNormalCastHydrate($di);
$this->_executeTestsRenamed($di);
$this->_executeTestsRenamedCastHydrate($di);
$this->_executeTestsNormalComplex($di);
$this->_executeTestsNormalComplexCastHydrate($di);
}

public function testModelsSQLite()
Expand All @@ -112,8 +118,11 @@ public function testModelsSQLite()
}, true);

$this->_executeTestsNormal($di);
$this->_executeTestsNormalCastHydrate($di);
$this->_executeTestsRenamed($di);
$this->_executeTestsRenamedCastHydrate($di);
$this->_executeTestsNormalComplex($di);
$this->_executeTestsNormalComplexCastHydrate($di);
}

protected function _executeTestsNormal($di)
Expand Down Expand Up @@ -329,4 +338,225 @@ protected function _executeTestsNormalComplex($di)

}

protected function _executeTestsNormalCastHydrate($di)
{
Phalcon\Mvc\Model::setup(['castOnHydrate' => true]);

$number = 0;

$robots = Robots::find();

foreach ($robots as $robot) {
$this->assertTrue(is_object($robot));
$this->assertEquals(get_class($robot), 'Robots');
$number++;
}

$robots->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_RECORDS);
foreach ($robots as $robot) {
$this->assertTrue(is_object($robot));
$this->assertEquals(get_class($robot), 'Robots');
$number++;
}

$robots->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS);
foreach ($robots as $robot) {
$this->assertTrue(is_array($robot));
$this->assertEquals(7, count($robot));
$number++;
}

$robots->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS);
foreach ($robots as $robot) {
$this->assertTrue(is_object($robot));
$this->assertEquals(get_class($robot), 'stdClass');
$number++;
}

$this->assertEquals($number, 12);

$number = 0;

$people = People::find(array('limit' => 33));

foreach ($people as $person) {
$this->assertTrue(is_object($person));
$this->assertEquals(get_class($person), 'People');
$number++;
}

$people->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_RECORDS);
foreach ($people as $person) {
$this->assertTrue(is_object($person));
$this->assertEquals(get_class($person), 'People');
$number++;
}

$people->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS);
foreach ($people as $person) {
$this->assertTrue(is_array($person));
$number++;
}

$people->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS);
foreach ($people as $person) {
$this->assertTrue(is_object($person));
$this->assertEquals(get_class($person), 'stdClass');
$number++;
}

$this->assertEquals($number, 33 * 4);

Phalcon\Mvc\Model::setup(['castOnHydrate' => false]);
}

protected function _executeTestsRenamedCastHydrate($di)
{
Phalcon\Mvc\Model::setup(['castOnHydrate' => true]);

$number = 0;

$robots = Robotters::find();

foreach ($robots as $robot) {
$this->assertTrue(is_object($robot));
$this->assertEquals(get_class($robot), 'Robotters');
$number++;
}

$robots->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_RECORDS);
foreach ($robots as $robot) {
$this->assertTrue(is_object($robot));
$this->assertEquals(get_class($robot), 'Robotters');
$number++;
}

$robots->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS);
foreach ($robots as $robot) {
$this->assertTrue(is_array($robot));
$this->assertEquals(count($robot), 7);
$number++;
}

$robots->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS);
foreach ($robots as $robot) {
$this->assertTrue(is_object($robot));
$this->assertEquals(get_class($robot), 'stdClass');
$number++;
}

$this->assertEquals($number, 12);

$number = 0;

$people = Personers::find(array('limit' => 33));

foreach ($people as $person) {
$this->assertTrue(is_object($person));
$this->assertEquals(get_class($person), 'Personers');
$number++;
}

$people->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_RECORDS);
foreach ($people as $person) {
$this->assertTrue(is_object($person));
$this->assertEquals(get_class($person), 'Personers');
$number++;
}

$people->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS);

foreach ($people as $person) {
$this->assertTrue(is_array($person));
$this->assertTrue(isset($person['navnes']));
$number++;
}

$people->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS);
foreach ($people as $person) {
$this->assertTrue(is_object($person));
$this->assertEquals(get_class($person), 'stdClass');
$this->assertTrue(isset($person->navnes));
$number++;
}

$this->assertEquals($number, 33 * 4);

Phalcon\Mvc\Model::setup(['castOnHydrate' => false]);
}

protected function _executeTestsNormalComplexCastHydrate($di)
{
Phalcon\Mvc\Model::setup(['castOnHydrate' => true]);

$result = $di->get('modelsManager')->executeQuery('SELECT id FROM Robots');

//Scalar complex query
foreach ($result as $row) {
$this->assertEquals(get_class($row), 'Phalcon\Mvc\Model\Row');
$this->assertTrue(is_numeric($row->id));
}

$result->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_RECORDS);
foreach ($result as $row) {
$this->assertEquals(get_class($row), 'Phalcon\Mvc\Model\Row');
$this->assertTrue(is_numeric($row->id));
}

$result->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS);
foreach ($result as $row) {
$this->assertTrue(is_array($row));
$this->assertTrue(is_numeric($row['id']));
}

$result->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS);
foreach ($result as $row) {
$this->assertEquals(get_class($row), 'stdClass');
$this->assertTrue(is_numeric($row->id));
}

//Complex resultset including scalars and complete objects
$result = $di->get('modelsManager')->executeQuery('SELECT Robots.id, Robots.*, RobotsParts.* FROM Robots JOIN RobotsParts');
foreach ($result as $row) {
$this->assertEquals(get_class($row), 'Phalcon\Mvc\Model\Row');
$this->assertTrue(is_numeric($row->id));
$this->assertEquals(gettype($row->robots), 'object');
$this->assertEquals(get_class($row->robots), 'Robots');
$this->assertEquals(gettype($row->robotsParts), 'object');
$this->assertEquals(get_class($row->robotsParts), 'RobotsParts');
}

$result->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_RECORDS);
foreach ($result as $row) {
$this->assertEquals(get_class($row), 'Phalcon\Mvc\Model\Row');
$this->assertTrue(is_numeric($row->id));
$this->assertEquals(gettype($row->robots), 'object');
$this->assertEquals(get_class($row->robots), 'Robots');
$this->assertEquals(gettype($row->robotsParts), 'object');
$this->assertEquals(get_class($row->robotsParts), 'RobotsParts');
}

$result->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_ARRAYS);
foreach ($result as $row) {
$this->assertTrue(is_array($row));
$this->assertTrue(is_numeric($row['id']));
$this->assertEquals(gettype($row['robots']), 'array');
$this->assertEquals(count($row['robots']), 7);
$this->assertEquals(gettype($row['robotsParts']), 'array');
$this->assertEquals(count($row['robotsParts']), 3);
}

$result->setHydrateMode(Phalcon\Mvc\Model\Resultset::HYDRATE_OBJECTS);
foreach ($result as $row) {
$this->assertEquals(get_class($row), 'stdClass');
$this->assertTrue(is_numeric($row->id));
$this->assertEquals(gettype($row->robots), 'object');
$this->assertEquals(get_class($row->robots), 'stdClass');
$this->assertEquals(gettype($row->robotsParts), 'object');
$this->assertEquals(get_class($row->robotsParts), 'stdClass');
}

Phalcon\Mvc\Model::setup(['castOnHydrate' => false]);
}

}

0 comments on commit d369efd

Please sign in to comment.