Skip to content

Commit

Permalink
fixed issue with missing support for non hydrated results in ConstStr…
Browse files Browse the repository at this point in the history
…ategy.php
  • Loading branch information
arusinowski committed Apr 24, 2024
1 parent 10c8540 commit 26f4841
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
Changelog
=========

* 3.0.0-dev CakePHP 5.x support
* 3.1.0
* fixed issue with $constant beeing of type /Cake/ORM/Entity
* fixed issue with missing support for non hydrated results in src/Model/Behavior/Strategy/ConstStrategy.php

* 2.0.0 CakePHP 4.x support
* 3.0.0
* CakePHP 5.x support

* 2.0.0
* CakePHP 4.x support

* 1.2.0
* Documentation improved
Expand Down
20 changes: 14 additions & 6 deletions src/Model/Behavior/Strategy/ConstStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,30 @@ public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObjec
$query->clearContain()->contain($contain);

$query->formatResults(fn (CollectionInterface $results) => $results
->map(function (EntityInterface $row): EntityInterface {
$constant = Hash::get($row, $this->getConfig('field'));

if ($constant instanceof Entity) {
->map(function (mixed $row): mixed {
if (is_string($row) || !$row) {
return $row;
}

$constant = Hash::get($row, $this->getConfig('field'));

$field = Inflector::singularize(Inflector::underscore($this->alias));
$value = new Entity([
'label' => Hash::get($this->getConstants(), $constant, $constant),
'prefix' => $this->getConfig('prefix'),
'value' => $constant,
], ['markClean' => true, 'markNew' => false]);

$row->set($field, $value);
$row->setDirty($field, false);
if (is_array($row)) {
$row[$field] = $value->toArray();

return $row;
}

if ($row instanceof EntityInterface) {
$row->set($field, $value);
$row->setDirty($field, false);
}

return $row;
}));
Expand Down

0 comments on commit 26f4841

Please sign in to comment.