Q&D fix for suggestions which bypass ORM behaviors

This commit is contained in:
Eric Lippmann 2022-04-13 16:01:41 +02:00 committed by Johannes Meyer
parent c41dbb80f4
commit 6c1850d732
2 changed files with 27 additions and 2 deletions

View file

@ -0,0 +1,25 @@
<?php
/* Icinga DB Web | (c) 2022 Icinga GmbH | GPLv2 */
namespace Icinga\Module\Icingadb\Util;
use ipl\Sql\Cursor;
use Iterator;
class ObjectSuggestionsCursor extends Cursor
{
public function getIterator(): Iterator
{
foreach (parent::getIterator() as $key => $value) {
// TODO(lippserd): This is a quick and dirty fix for PostgreSQL binary datatypes for which PDO returns
// PHP resources that would cause exceptions since resources are not a valid type for attribute values.
// We need to do it this way as the suggestion implementation bypasses ORM behaviors here and there.
if (is_resource($value)) {
$value = stream_get_contents($value);
}
yield $key => $value;
}
}
}

View file

@ -11,6 +11,7 @@ use Icinga\Module\Icingadb\Model\Behavior\ReRoute;
use Icinga\Module\Icingadb\Model\CustomvarFlat;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\Service;
use Icinga\Module\Icingadb\Util\ObjectSuggestionsCursor;
use ipl\Html\HtmlElement;
use ipl\Orm\Exception\InvalidColumnException;
use ipl\Orm\Exception\InvalidRelationException;
@ -18,7 +19,6 @@ use ipl\Orm\Model;
use ipl\Orm\Relation\BelongsToMany;
use ipl\Orm\Resolver;
use ipl\Orm\UnionModel;
use ipl\Sql\Cursor;
use ipl\Sql\Expression;
use ipl\Sql\Select;
use ipl\Stdlib\Filter;
@ -161,7 +161,7 @@ class ObjectSuggestions extends Suggestions
$this->applyRestrictions($query);
try {
return (new Cursor($query->getDb(), $query->assembleSelect()->distinct()))
return (new ObjectSuggestionsCursor($query->getDb(), $query->assembleSelect()->distinct()))
->setFetchMode(PDO::FETCH_COLUMN);
} catch (InvalidColumnException $e) {
throw new SearchException(sprintf(t('"%s" is not a valid column'), $e->getColumn()));