Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 $res1 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.

like image 558
Fabian Fritz Avatar asked Jan 14 '23 18:01

Fabian Fritz


2 Answers

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.

like image 157
CalleKhan Avatar answered Jan 16 '23 07:01

CalleKhan


If you dont need the QueryResult-Object you can do this by using array_merge.

$res1 = $this->createQuery()->execute();
$res2 = $this->createQuery()->execute();
$res = array_merge($res1->toArray(), $res2->toArray());
like image 20
crz Avatar answered Jan 16 '23 06:01

crz