Skip to content

Commit

Permalink
Fixed working with hydration modes
Browse files Browse the repository at this point in the history
- a Query returned from a `QueryObjectInterface` can define a hydration mode itself
- removed strict return type from a method `ExecutableQueryObjectInterface::fetchOne()` - the method can returns an array or a scalar value
  • Loading branch information
tg666 committed Nov 2, 2021
1 parent 68d606b commit 016b72c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/ExecutableQueryObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,14 @@ public function fetch()
->setMaxResults(NULL);
}

if (NULL !== $this->resultSetOptions && AbstractQuery::HYDRATE_OBJECT !== $this->resultSetOptions->getOption(ResultSetOptionsInterface::OPTION_HYDRATION_MODE, AbstractQuery::HYDRATE_OBJECT)) {
return $query->execute(NULL, $this->resultSetOptions->getOption(ResultSetOptionsInterface::OPTION_HYDRATION_MODE));
$hydrationMode = $query->getHydrationMode();

if (NULL !== $this->resultSetOptions) {
$hydrationMode = $this->resultSetOptions->getOption(ResultSetOptionsInterface::OPTION_HYDRATION_MODE, $hydrationMode);
}

if (AbstractQuery::HYDRATE_OBJECT !== $hydrationMode) {
return $query->execute(NULL, $hydrationMode);
}

return $this->lastResultSet;
Expand All @@ -101,7 +107,7 @@ public function fetch()
*
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function fetchOne(): ?object
public function fetchOne()
{
$query = $this->getQuery();

Expand Down
2 changes: 1 addition & 1 deletion src/ExecutableQueryObjectInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ public function fetch();
/**
* @return object|NULL|mixed
*/
public function fetchOne(): ?object;
public function fetchOne();
}
2 changes: 1 addition & 1 deletion src/ResultSet/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function getIterator(): Iterator
$query = $this->context->getQuery();
$options = $this->getOptions();

$query->setHydrationMode($options->getOption(ResultSetOptionsInterface::OPTION_HYDRATION_MODE, AbstractQuery::HYDRATE_OBJECT));
$query->setHydrationMode($options->getOption(ResultSetOptionsInterface::OPTION_HYDRATION_MODE, $query->getHydrationMode()));

if ($query instanceof Query && $options->getOption(ResultSetOptionsInterface::OPTION_FETCH_JOIN_COLLECTION) && (0 < $query->getMaxResults() || 0 < $query->getFirstResult())) {
$iterator = $this->createPaginator($query)->getIterator();
Expand Down
10 changes: 9 additions & 1 deletion src/ResultSet/ResultSetOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ class ResultSetOptions implements ResultSetOptionsInterface
{
/** @var array */
protected $options = [
self::OPTION_HYDRATION_MODE => AbstractQuery::HYDRATE_OBJECT,
self::OPTION_HYDRATION_MODE => NULL,
self::OPTION_FETCH_JOIN_COLLECTION => FALSE,
self::OPTION_USE_OUTPUT_WALKERS => NULL,
];

/**
* @return \SixtyEightPublishers\DoctrineQueryObjects\ResultSet\ResultSetOptions
*/
public function create(): self
{
return new static();
}

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit 016b72c

Please sign in to comment.