Created
November 12, 2015 08:19
-
-
Save smichaelsen/2bfdfb872d04a0776f01 to your computer and use it in GitHub Desktop.
Extend extbase QueryResult to fix count() of queries with a custom $statement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
+1
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.
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
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:
BTW, great snippet! IMHO this should be in the core.