【问题标题】:Extbase Database-query with COUNT and GROUP BY带有 COUNT 和 GROUP BY 的 Extbase 数据库查询
【发布时间】:2014-02-27 20:00:43
【问题描述】:

我正在尝试在我的存储库中创建一个 DBquery。我已经有一个工作,但我想要的是调整它以使用约束而不是使用 setReturnRawQueryResult(TRUE)。所以我想在表中找到所有不同类型的 $values 并计算它们有多少存在。这是我的代码:

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”?

【问题讨论】:

  • 您的查询将返回不同的结果。
  • 我的意思是你已经有all different types of $values in the table and count, how much of them exist查询。
  • 是的,我知道,但我不想使用原始 sql 语句,而是使用 extbase-framework 提供的方法...
  • 对不起,我误解了你的问题。
  • 没问题,感谢您帮助我:)

标签: mysql repository typo3 extbase typo3-6.1.x


【解决方案1】:

您可以做的是提供一个新的模型和存储库,您可以在其中编写查询。例如。如果您的新模型包含将被映射的属性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;
        ';
    }
}

【讨论】:

    猜你喜欢
    • 2021-12-19
    • 2014-04-20
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多