Skip to content

Commit

Permalink
Merge pull request #61 from ErwanLP/4.0
Browse files Browse the repository at this point in the history
Fixing date retrieval for lazily loaded objects
  • Loading branch information
moufmouf committed Mar 2, 2016
2 parents f44cfef + 1e4c42d commit 14a1f2e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
10 changes: 9 additions & 1 deletion src/Mouf/Database/TDBM/DbRow.php
Expand Up @@ -169,7 +169,15 @@ public function _dbLoadIfNotLoaded()
throw new TDBMException("Could not retrieve object from table \"$this->dbTableName\" using filter \"\".");
}

$this->dbRow = $result->fetch(\PDO::FETCH_ASSOC);

$row = $result->fetch(\PDO::FETCH_ASSOC);

$this->dbRow = [];
$types = $this->tdbmService->_getColumnTypesForTable($this->dbTableName);

foreach ($row as $key => $value) {
$this->dbRow[$key] = $types[$key]->convertToPHPValue($value, $connection->getDatabasePlatform());
}

$result->closeCursor();

Expand Down
28 changes: 28 additions & 0 deletions src/Mouf/Database/TDBM/TDBMService.php
Expand Up @@ -27,6 +27,7 @@
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
use Mouf\Database\MagicQuery;
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
use Mouf\Database\TDBM\Utils\TDBMDaoGenerator;
Expand Down Expand Up @@ -1315,6 +1316,7 @@ public function findObjectByPk($table, array $primaryKeys, array $additionalTabl
$bean = $this->reflectionClassCache[$className]->newInstanceWithoutConstructor();
/* @var $bean AbstractTDBMObject */
$bean->_constructLazy($table, $primaryKeys, $this);
return $bean;
}
}

Expand Down Expand Up @@ -1513,4 +1515,30 @@ public function _getPivotTablesLinkedToBean(AbstractTDBMObject $bean)

return $junctionTables;
}

/**
* Array of types for tables.
* Key: table name
* Value: array of types indexed by column.
*
* @var array[]
*/
private $typesForTable = [];

/**
* @internal
* @param string $tableName
* @return Type[]
*/
public function _getColumnTypesForTable($tableName)
{
if (!isset($typesForTable[$tableName])) {
$columns = $this->tdbmSchemaAnalyzer->getSchema()->getTable($tableName)->getColumns();
$typesForTable[$tableName] = array_map(function(Column $column) {
return $column->getType();
}, $columns);

}
return $typesForTable[$tableName];
}
}
11 changes: 11 additions & 0 deletions tests/Mouf/Database/TDBM/TDBMDaoGeneratorTest.php
Expand Up @@ -134,6 +134,17 @@ public function testGeneratedGetById()
// FIXME: Question: que faire du paramètre stockage "UTC"????
}

/**
* @depends testDaoGeneration
*/
public function testGeneratedGetByIdLazyLoaded()
{
$roleDao = new RoleDao($this->tdbmService);
$roleBean = $roleDao->getById(1, true);
$this->assertEquals(1, $roleBean->getId());
$this->assertInstanceOf('\\DateTimeInterface', $roleBean->getCreatedAt());
}

/**
* @depends testDaoGeneration
*/
Expand Down
11 changes: 6 additions & 5 deletions tests/sql/tdbmunittest.sql
Expand Up @@ -110,17 +110,18 @@ INSERT INTO `rights` (`label`) VALUES

CREATE TABLE IF NOT EXISTS `roles` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL
`name` varchar(255) NOT NULL,
`created_at` date NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

--
-- Dumping data for table `roles`
--

INSERT INTO `roles` (`id`, `name`) VALUES
(1, 'Admins'),
(2, 'Writers'),
(3, 'Singers');
INSERT INTO `roles` (`id`, `name`, `created_at`) VALUES
(1, 'Admins', '2015-10-24'),
(2, 'Writers', '2015-10-24'),
(3, 'Singers', '2015-10-24');

-- --------------------------------------------------------

Expand Down

0 comments on commit 14a1f2e

Please sign in to comment.