Skip to content

Commit

Permalink
[TASK] Cleanup CategoryRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminkott committed May 19, 2023
1 parent f79dab1 commit d55f740
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions Classes/Domain/Repository/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace T3G\AgencyPack\Blog\Domain\Repository;

use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
Expand All @@ -19,48 +20,44 @@

class CategoryRepository extends Repository
{
/**
* Initializes the repository.
*
* @throws \InvalidArgumentException
*/
protected array $settings = [];

public function initializeObject(): void
{
// @TODO: It looks like extbase ignore storage settings for sys_category.
// @TODO: this hack set the storage handling for sys_category table.
$configurationManager = $this->objectManager->get(ConfigurationManagerInterface::class);
$settings = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, 'blog');
$querySettings = $this->objectManager->get(Typo3QuerySettings::class);
$querySettings->setRespectStoragePage(true);
$querySettings->setStoragePageIds(GeneralUtility::trimExplode(',', $settings['storagePid']));
$configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class);
$this->settings = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, 'blog');

$querySettings = GeneralUtility::makeInstance(
Typo3QuerySettings::class,
GeneralUtility::makeInstance(Context::class),
$configurationManager
);
$querySettings->setRespectStoragePage(false);
$querySettings->setStoragePageIds(GeneralUtility::intExplode(',', $this->settings['persistence']['storagePid']));
$this->setDefaultQuerySettings($querySettings);

$this->defaultOrderings = [
'title' => QueryInterface::ORDER_ASCENDING,
];
}

/**
* @return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findByUids(array $uids)
{
$query = $this->createQuery();
$query->matching(
$query->in('uid', $uids)
);
$query->matching($query->in('uid', $uids));

return $query->execute();
}

/**
* @param string $table
* @param int $uid
* @param string $field
* @return array|null|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
*/
public function getByReference($table, $uid, $field = 'categories')
public function getByReference(string $table, int $uid, string $field = 'categories')
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);

$queryBuilder = $connectionPool->getConnectionForTable('sys_category_record_mm')->createQueryBuilder();
$queryBuilder
->select('uid_local')
Expand All @@ -70,7 +67,7 @@ public function getByReference($table, $uid, $field = 'categories')
$queryBuilder->expr()->eq('fieldname', $queryBuilder->createNamedParameter($field)),
$queryBuilder->expr()->eq('uid_foreign', $queryBuilder->createNamedParameter($uid))
);
$categories = array_column($queryBuilder->execute()->fetchAll(), 'uid_local');
$categories = array_column($queryBuilder->executeQuery()->fetchAllAssociative(), 'uid_local');

if (count($categories) > 0) {
$query = $this->createQuery();
Expand Down

0 comments on commit d55f740

Please sign in to comment.