Apply DefaultQuerySettings to all magic methods in an Extbase Repository

20. Januar 2023PHPTYPO3

Sometimes you want or need a certain set of default settings on all queries, selfmade as well as the magic queries TYPO3\CMS\Extbase\Persistence\Repository offers.

To achieve that you can add the following to the beginning of your Repository Class:

namespace DEMO\DemoExtension\Domain\Repository;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Repository;
use TYPO3\CMS\Extbase\Persistence\QueryInterface

class DemoRepository extends Repository
  protected $defaultOrderings = [
    'title' => QueryInterface::ORDER_ASCENDING,

  public function initializeObject()
    $querySettings = $this->createQuery()->getQuerySettings();

  public function getMySpecialStuff()
    $query = $this->createQuery(); // will have the above querySettings applied
    // ...



Now every query for this DemoRepository will be sorted by title in ascending order, and more importantly all queries will - in this example - ignore the storagePid settings and the enableFields (start/stop/hidden), even if you use magic methods like this:


// ....
$demoRepository = GeneralUtility::makeinstance(DemoRepository::class);

// these methods do not exist in the DemoRepository Class and will be 
// dispatched as magic Methods by the parent Class Repository
// but it will have the storagepage restriction and removed the enable fields

$demorecords = $demoRepository->findByTitle('Test Record');
$demorecordsCount = $demoRepository->countByTitle('Test Record');
$demorecord = $demoRepository->findOneByTitle('Test Record');