对于具有类别(n:m)的模型资产,我具有以下内容。我想计算属于1个或多个类别的资产。
/** @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_assets_domain_model_asset');
$constraints[] = $queryBuilder->expr()->in('c.uid', $categories);
$count = $queryBuilder
->count('a.uid')
->from('tx_assets_domain_model_asset', 'a')
->join('a', 'tx_assets_asset_assetcategory_mm', 'ac', 'a.uid = ac.uid_local')
->join('ac', 'tx_assets_domain_model_assetcategory', 'c', 'c.uid = ac.uid_foreign')
->where(...$constraints)
->execute()
->fetchColumn(0);
这正常。但是,资产可能属于1类以上。所以我需要一个不同的结果。根据文档[1],我应该同时使用-> groupBy()和-> orderBy()。
例如,如果我尝试这样做。我总是得到1作为计数结果。
$count = $queryBuilder
->count('a.uid')
->from('tx_assets_domain_model_asset', 'a')
->join('a', 'tx_assets_asset_assetcategory_mm', 'ac', 'a.uid = ac.uid_local')
->join('ac', 'tx_assets_domain_model_assetcategory', 'c', 'c.uid = ac.uid_foreign')
->where(...$constraints)
->groupBy('a.uid')
->orderBy('a.sorting')
->execute()
->fetchColumn(0);
在这种情况下,有任何建议如何使用groupBy和orderBy?
奇怪的是,-> fetchAll()而不是-> fetchColumn(0)可以完美地工作。但是我想这会降低性能。