Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/InnerResultIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ public function next(): void
$dbRow = $this->objectStorage->get($mainBeanTableName, $hash);
if ($dbRow !== null) {
$bean = $dbRow->getTDBMObject();
if ($bean->_getStatus() === TDBMObjectStateEnum::STATE_NOT_LOADED) {
$bean->_constructFromData($beanData, $this->tdbmService);
}
} else {
// Let's construct the bean
if (!isset($reflectionClassCache[$actualClassName])) {
Expand Down
8 changes: 8 additions & 0 deletions tests/Dao/TestCountryDao.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,12 @@ public function getCountriesUsingDistinctQuery()

return $this->findFromRawSql($sql);
}

/**
* @return CountryBean[]|Result
*/
public function findByIds(array $ids)
{
return $this->find('id IN (:ids)', ['ids' => $ids]);
}
}
59 changes: 59 additions & 0 deletions tests/TDBMDaoGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2405,6 +2405,65 @@ public function testPivotTableAreProperlyEscaped(): void
$this->assertCount(1, $accessible->getValues());
}

public function testHydrateLazyLoadedBean(): void
{
$countryDao = new TestCountryDao($this->tdbmService);
$country = $countryDao->getById(1, true);
$this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $country->_getStatus());
$countryDao->findByIds([1])->toArray(); // This `->toArray` consumes the iterator and hence resolves the objects
$this->assertSame(TDBMObjectStateEnum::STATE_LOADED, $country->_getStatus());
}

public function testHydrateNotLoadedBeanReference(): void
{
$userDao = new UserDao($this->tdbmService);
$countryDao = new TestCountryDao($this->tdbmService);
$users = $userDao->findAll()->toArray();
$countriesIds = [];
foreach ($users as $user) {
assert($user instanceof UserBean);
$countriesIds[] = $user->getCountry()->getId();
$this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $user->getCountry()->_getStatus());
}

$countryDao->findByIds($countriesIds)->toArray(); // This `->toArray` consumes the iterator and hence resolves the objects
foreach ($users as $user) {
assert($user instanceof UserBean);
$this->assertSame(TDBMObjectStateEnum::STATE_LOADED, $user->getCountry()->_getStatus());
}
}

public function testHydrateNotLoadedBeans(): void
{
$userDao = new UserDao($this->tdbmService);
$countryDao = new TestCountryDao($this->tdbmService);
$users = $userDao->findAll()->toArray();
$countriesIds = [];
foreach ($users as $user) {
assert($user instanceof UserBean);
$countriesIds[] = $user->getCountry()->getId();
$this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $user->getCountry()->_getStatus());
}

$countries = $countryDao->findByIds($countriesIds);
foreach ($countries as $country) {
assert($country instanceof CountryBean);
$this->assertSame(TDBMObjectStateEnum::STATE_LOADED, $country->_getStatus());
}
}

public function testHydrateGetByIdAfterLazyLoad(): void
{
// FIXME: It is not trivial to fix as `\TheCodingMachine\TDBM\TDBMService::findObjectOrFail` creates its own bean.
// Hence, any existing reference to the original bean won't get the change.
$this->markTestIncomplete('Test is failing because we retrieve the bean from the storage and do not hydrate it');
$countryDao = new TestCountryDao($this->tdbmService);
$country = $countryDao->getById(1, true);
$this->assertSame(TDBMObjectStateEnum::STATE_NOT_LOADED, $country->_getStatus());
$country = $countryDao->getById(1);
$this->assertSame(TDBMObjectStateEnum::STATE_LOADED, $country->_getStatus()); // This is failing
}

private function skipOracle(): void
{
if (self::getConnection()->getDatabasePlatform() instanceof OraclePlatform) {
Expand Down