Skip to content

Instantly share code, notes, and snippets.

@smichaelsen
Created November 12, 2015 08:19
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smichaelsen/2bfdfb872d04a0776f01 to your computer and use it in GitHub Desktop.
Save smichaelsen/2bfdfb872d04a0776f01 to your computer and use it in GitHub Desktop.
Extend extbase QueryResult to fix count() of queries with a custom $statement
<?php
namespace AppZap\Tripshop\Persistence;
use TYPO3\CMS\Extbase\Persistence\Generic\Qom\Statement;
class QueryResult extends \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult
{
/**
* Overwrites the original implementation of Extbase
*
* When the query contains a $statement the query is regularly executed and the number of results is counted
* instead of the original implementation which tries to create a custom COUNT(*) query and delivers wrong results.
*
* @return int The number of matching objects
*/
public function count()
{
if ($this->numberOfResults === null) {
if (is_array($this->queryResult)) {
$this->numberOfResults = count($this->queryResult);
} else {
$statement = $this->query->getStatement();
if ($statement instanceof Statement) {
$this->initialize();
$this->numberOfResults = count($this->queryResult);
} else {
return parent::count();
}
}
}
return $this->numberOfResults;
}
}
@stoppeye
Copy link

In case you're wondering (as I did...): you'll also have to put some configuration into ext_typoscript_setup.txt to get this working:

config.tx_extbase {
  objects {
    TYPO3\CMS\Extbase\Persistence\QueryResultInterface {
      className = AppZap\Tripshop\Persistence\QueryResult
    }   
  }
}

BTW, great snippet! IMHO this should be in the core.

@j4k3
Copy link

j4k3 commented Mar 16, 2016

+1

@kitzberger
Copy link

Is this bug (it's a bug, right?) reported on forge? Couldn't find it there. This should at least be mentioned in the documentation.

@julianhofmann
Copy link

Since march 2017 it's reported as a bug (#80464)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment