2014-02-27 31 views
2

我想在我的存儲庫中創建一個DBquery。我已經有一個工作,但我想要適應它使用約束,而不是使用setReturnRawQueryResult(TRUE)。所以我想要在表格中找到所有不同類型的$值,並計數它們有多少。這是我的代碼:使用COUNT和GROUP BY的Extbase數據庫查詢

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

你是什麼意思? – sinini

+0

我的意思是你已經擁有'表中的所有不同類型的$值,並且有多少存在查詢。 –

回答

0

您可以做的是提供一個新的模型和存儲庫,您可以在其中編寫查詢。例如。如果您的新模型包含將被映射的屬性num。我不確定TCA這個案件的需要。但請確保您提供uid作爲屬性,並且它是唯一的。

型號:

<?php 
namespace Vendor\ExtKey\Domain\Model; 


/** 
* @author Daniel Siepmann <[email protected]> 
*/ 
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 <[email protected]> 
*/ 
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; 
     '; 
    } 
}