Fabian Fritz Fabian Fritz - 7 months ago 12
PHP Question

Combining two QueryResults in a Extbase Repository

I'm programming a TYPO3 - extension for a website. Since I'm using the Extbase Framework I have a Repository class (Tx_Extbase_Persistence_Repository) where I do two sql queries in a row:

$query1 = $this->createQuery();
$query1->statement($sql1);
$res1 = $query1->execute();

$query2 = $this->createQuery();
$query2->statement($sql2);
$res1 = $query2->execute();


Both
$res
1 and
$res2
contain a
Tx_Extbase_Persistence_QueryResult
. Now I want to return the combined result and I have no idea how this is done. Returning the raw array isn't an option because I'm relying on the functions of the
QueryResult
class, and also I want to avoid to combine the sql(UNION, JOIN). I already tried this:

$myResult = $this->objectManager->create('Tx_Extbase_Persistence_ObjectStorage')
foreach($res1 as $obj) {
$myResult->attach($obj);
}
//foreach $res2


..but this throws an error (
"could not determine the child object type"
)

So how do you properly combine two
Tx_Extbase_Persistence_QueryResult
?

Edit:

With combining I mean instead of two separate
QueryResults
I want just one which contains both the results from
$query1
as well as
$query2
. An SQL-UNION or JOIN unfortunately isn't an option.

Answer

QueryResult implements QueryResultInterface which extends among others ArrayAccess. With this you can use the offsetSet method.

foreach ($res2->toArray() as $result) {
  $res1->offsetSet(($res1->count()), $result);
}

The QueryResult $res1 contain now the objects from $res2 too.

Comments