我正在尝试在我的存储库中创建一个DBquery。我已经有一个工作,但我想要的是使用约束而不是使用setReturnRawQueryResult(TRUE)。所以我想在表中找到所有不同类型的$值并计算,它们中有多少存在。那是我的代码:
public function findAllDistinct($value, $category) {
$query = $this->createQuery();
$query->getQuerySettings()->setReturnRawQueryResult(TRUE);
return $query
->statement('SELECT ' . $value . ', COUNT(*) AS \'num\' '
. 'FROM tx_myextension_domain_model_job '
. 'WHERE job_category =' . $category . ' '
. 'GROUP BY ' .$value.'')
->execute();
}
顺便说一句,如何实现“SELECT DISTINCT”?
答案 0 :(得分:0)
您可以做的是提供一个新的模型和存储库,您可以在其中编写查询。例如。如果您的新模型包含将被映射的属性num
。
对于这种情况,我不确定TCA
是否需要。但请确保您提供uid
作为属性并且它是唯一的。
型号:
<?php
namespace Vendor\ExtKey\Domain\Model;
/**
* @author Daniel Siepmann <d.siepmann@web-vision.de>
*/
class FilterOption extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/**
* @var string
*/
protected $title;
/**
* @var float
*/
protected $value;
/**
* @var float
*/
protected $min;
/**
* @var float
*/
protected $max;
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @return float
*/
public function getValue()
{
return $this->value;
}
/**
* @return float
*/
public function getMin()
{
return $this->min;
}
/**
* @return float
*/
public function getMax()
{
return $this->max;
}
}
存储库:
<?php
namespace Vendor\ExtKey\Domain\Repository;
/**
*
* @author Daniel Siepmann <d.siepmann@web-vision.de>
*/
class FilterOptionRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
{
/**
* Will find all options to filter for for a specific property.
*
* @param string $property The name of the property.
*
* @return NULL|[]
*/
public function findFilterOptionForProperty($property)
{
if (in_array($property, [ 'house_types' ])) {
return $this->findMmOptions($property);
}
return $this->findNumericOptions($property);
}
/**
* Will find all options to filter for for a specific property.
*
* @param string $property The name of the property.
*
* @return NULL|[]
*/
protected function findNumericOptions($property)
{
return $this->createQuery()
->statement($this->getNumericStatement($property))
->execute();
}
/**
* Get SQL statement to fetch filter options from persistence,
* for the given property if it's values are numeric.
*
* @param string $property The property to fetch.
*
* @return string The statement
*/
protected function getNumericStatement($property)
{
// We have to fake UID, otherwise extbase will reuse previous
// objects with same uid.
$uidBase = rand(ord($property), getrandmax());
// Return statement for single column.
return '
SELECT "' . $property . '" AS title, ' . $property . ' AS value,
(SELECT min(' . $property . ') FROM tx_realty_objects WHERE ' . $this->getAdditionalWhereClause() . ') AS min,
(SELECT max(' . $property . ') FROM tx_realty_objects WHERE ' . $this->getAdditionalWhereClause() . ') AS max,
@rownum := @rownum + ' . $uidBase . ' AS uid
FROM tx_realty_objects,
(SELECT @rownum:=' . $uidBase . ') x
WHERE ' . $this->getAdditionalWhereClause() . '
GROUP BY ' . $property . '
ORDER BY value;
';
}
}