TYPO3 Extbase: combine multiple queries

1.3k views Asked by At

I want to select only $limit records from the table with the category $cat.

foreach($cats as $cat) {
    $query = $this->createQuery();
    $query->matching($query->equals('type', $cat));
    $query->setLimit((int)$limit);
    $result[] = $query->execute();
}

Is it possible to combine the query result so one query result?

2

There are 2 answers

0
Reshma Reghunatha Panicker On

I think your query is something like ,

"SELECT * FROM table_name WHERE type IN ('".$cats."')"

Here $cats is an array of categories like,

$cats = array("cat1", "cat2", "cat3");

Then the extabse query will be,

$query = $this->createQuery();
$query->matching($query->in('type', $cats));
$query->setLimit((int)$limit);
$result = $query->execute();

In the above query in() checks if a single-valued property exists in a multi-value operand.

For more details refer : http://docs.typo3.org/typo3cms/ExtbaseFluidBook/6-Persistence/3-implement-individual-database-queries.html

3
Ralf On

Thank you, I found another way that worked for me:

public function findAllLimitedByCategory($limit, $category){


    $cats = explode(",",$category);
    $result = array();

    foreach($cats as $cat) {
        $query = $this->createQuery();
        $query->matching($query->equals('type', $cat));
        $query->setLimit((int)$limit);
        $result[] = $query->execute()->toArray();
    }

    $out = array();

    foreach($result as $r)
        $out = array_merge($out, $r);

    return $out;
}

...but I have to sort the array by php with this solution