Search code examples
typo3typo3-8.xtypo3-extensions

Getting unwanted where clause in query in TYPO3


I am writing a query to fetch records through createQuery(). Upon checking the SQL, there is a where clause like this:

WHERE ( `sys_category`.`pid` IN ( 47, 0 ) )

Here 47 is the sys_folder ID in which my records are saved but the problem is that the categories are stored in another folder with a different ID so it cannot find the 47 ID in the sys_category table. How can I override/fix this where clause?

Also, is it necessary that the categories I am associating my records with, need to be in the same folder as the records? if so why ?


Solution

  • You can use the $defaultQuerySettings to change the PID it searches on.

    Either you can set it in the instantiateObject() method and it will be used for every query or you can set it in your findBy method.

    $querySettings = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings::class);
    // here you can search on all PIDs
    $querySettings->setRespectStoragePage(false);
    // here you can set special PIDs
    $querySettings->setStoragePageIds([12, 15, 38]);
    
    // Set the setting
    $this->setDefaultQuerySettings($querySettings);