diff --git a/Classes/Persistence/QueryFactory.php b/Classes/Persistence/QueryFactory.php index 93d1d8d..73d5dfc 100644 --- a/Classes/Persistence/QueryFactory.php +++ b/Classes/Persistence/QueryFactory.php @@ -65,10 +65,27 @@ class Tx_Extbase_Persistence_QueryFactory implements Tx_Extbase_Persistence_Quer * @return Tx_Extbase_Persistence_QueryInterface */ public function create($className) { + /** @var $query Tx_Extbase_Persistence_QueryInterface */ $query = $this->objectManager->create('Tx_Extbase_Persistence_QueryInterface', $className); - $querySettings = $this->objectManager->create('Tx_Extbase_Persistence_Typo3QuerySettings'); - $frameworkConfiguration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); - $querySettings->setStoragePageIds(t3lib_div::intExplode(',', $frameworkConfiguration['persistence']['storagePid'])); + // Try to get an instance of the corresponding repository in order to get the default query settings + $repositoryClassName = str_replace('_Model_', '_Repository_', $className) . 'Repository'; + if (class_exists($repositoryClassName)) { + /** @var $repository Tx_Extbase_Persistence_RepositoryInterface */ + $repository = t3lib_div::makeInstance($repositoryClassName); + if (method_exists($repository, 'initializeObject')) { + $repository->initializeObject(); + } + $defaultQuerySettings = $repository->getDefaultQuerySettings(); + if (isset($defaultQuerySettings)) { + $querySettings = clone $defaultQuerySettings; + } + } + // If no default query settings were found, just load the storage pid setting + if (!isset($querySettings)) { + $querySettings = $this->objectManager->create('Tx_Extbase_Persistence_Typo3QuerySettings'); + $frameworkConfiguration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); + $querySettings->setStoragePageIds(t3lib_div::intExplode(',', $frameworkConfiguration['persistence']['storagePid'])); + } $query->setQuerySettings($querySettings); return $query; diff --git a/Classes/Persistence/Repository.php b/Classes/Persistence/Repository.php index 541c852..0fa0851 100644 --- a/Classes/Persistence/Repository.php +++ b/Classes/Persistence/Repository.php @@ -341,6 +341,15 @@ class Tx_Extbase_Persistence_Repository implements Tx_Extbase_Persistence_Reposi } /** + * Gets the default query settings + * + * @return null|Tx_Extbase_Persistence_QuerySettingsInterface + */ + public function getDefaultQuerySettings() { + return $this->defaultQuerySettings; + } + + /** * Returns a query for objects of this repository * * @return Tx_Extbase_Persistence_QueryInterface diff --git a/Classes/Persistence/RepositoryInterface.php b/Classes/Persistence/RepositoryInterface.php index 2d8af1f..3701ebe 100644 --- a/Classes/Persistence/RepositoryInterface.php +++ b/Classes/Persistence/RepositoryInterface.php @@ -145,6 +145,13 @@ interface Tx_Extbase_Persistence_RepositoryInterface { public function setDefaultQuerySettings(Tx_Extbase_Persistence_QuerySettingsInterface $defaultQuerySettings); /** + * Gets the default query settings + * + * @return null|Tx_Extbase_Persistence_QuerySettingsInterface + */ + public function getDefaultQuerySettings(); + + /** * Returns a query for objects of this repository * * @return Tx_Extbase_Persistence_QueryInterface diff --git a/Classes/Persistence/Storage/Typo3DbBackend.php b/Classes/Persistence/Storage/Typo3DbBackend.php index 9fd254d..9294dd7 100644 --- a/Classes/Persistence/Storage/Typo3DbBackend.php +++ b/Classes/Persistence/Storage/Typo3DbBackend.php @@ -831,11 +831,13 @@ class Tx_Extbase_Persistence_Storage_Typo3DbBackend implements Tx_Extbase_Persis * @return void */ protected function addPageIdStatement($tableName, array &$sql, array $storagePageIds) { - if (empty($this->tableInformationCache[$tableName]['columnNames'])) { - $this->tableInformationCache[$tableName]['columnNames'] = $this->databaseHandle->admin_get_fields($tableName); - } - if (is_array($GLOBALS['TCA'][$tableName]['ctrl']) && array_key_exists('pid', $this->tableInformationCache[$tableName]['columnNames'])) { - $sql['additionalWhereClause'][] = $tableName . '.pid IN (' . implode(', ', $storagePageIds) . ')'; + if (count($storagePageIds) > 0) { + if (empty($this->tableInformationCache[$tableName]['columnNames'])) { + $this->tableInformationCache[$tableName]['columnNames'] = $this->databaseHandle->admin_get_fields($tableName); + } + if (is_array($GLOBALS['TCA'][$tableName]['ctrl']) && array_key_exists('pid', $this->tableInformationCache[$tableName]['columnNames'])) { + $sql['additionalWhereClause'][] = $tableName . '.pid IN (' . implode(', ', $storagePageIds) . ')'; + } } }