ezpublish 5.4根据父字段排序

bn31dyow  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(248)

我正在使用ezpublish搜索api获取 session 内容类型对象按其父对象的 training 字段(字段名为 code ).
因此,如果我有两个会话s1(链接到代码t1的训练)和s2(链接到代码t2的训练),我希望在列表中的s2之前列出会话s1。

$criterions = array (
            new Criterion\ContentTypeIdentifier( 'session' )
        );
$locationQuery = new LocationQuery();
        $locationQuery->query = $criteriaArray;
        $locationQuery->sortClauses = array(
            //new SortClause\Field('session','price'),<- this works
            new SortClause\Field('training','code',Query::SORT_ASC)
           //this second sorClause does not work as code is not a field of session while price is
            );

使用本机mysql查询可能有解决这个问题的方法,但是它不可移植,并且一些id将被硬编码。
我的问题是我们能不能达成一种 sesssions 根据他们父母的说法 training' 代码​​`field?

woobm2wo

woobm2wo1#

多亏了@edi和我的一位同事,我才得以成功。
要创建的两个类是:field类:

class ParentSortClauseField extends SortClause
{

    public function __construct($typeIdentifier, $fieldIdentifier, $sortDirection = Query::SORT_ASC)
    {
        parent::__construct(
            'field',
            $sortDirection,
            new FieldTarget($typeIdentifier, $fieldIdentifier)
        );
    }

}

处理程序类:

class ParentSortClauseHandler extends SortClauseHandler
{

    protected $contentTypeHandler;

    public function __construct(
        DatabaseHandler $dbHandler,
        ContentTypeHandler $contentTypeHandler
    )
    {
        $this->contentTypeHandler = $contentTypeHandler;

        parent::__construct($dbHandler);
    }

    public function accept(SortClause $sortClause)
    {
        return $sortClause instanceof ParentSortClauseField;
    }

    public function applySelect(SelectQuery $query, SortClause $sortClause, $number)
    {
        $query
            ->select(
                $query->alias(
                    $query->expr->not(
                        $query->expr->isNull(
                            $this->dbHandler->quoteColumn(
                                'sort_key_int',
                                $this->getSortTableName($number)
                            )
                        )
                    ),
                    $column1 = $this->getSortColumnName($number . '_null')
                ),
                $query->alias(
                    $query->expr->not(
                        $query->expr->isNull(
                            $this->dbHandler->quoteColumn(
                                'sort_key_string',
                                $this->getSortTableName($number)
                            )
                        )
                    ),
                    $column2 = $this->getSortColumnName($number . '_bis_null')
                ),
                $query->alias(
                    $this->dbHandler->quoteColumn(
                        'sort_key_int',
                        $this->getSortTableName($number)
                    ),
                    $column3 = $this->getSortColumnName($number)
                ),
                $query->alias(
                    $this->dbHandler->quoteColumn(
                        'sort_key_string',
                        $this->getSortTableName($number)
                    ),
                    $column4 = $this->getSortColumnName($number . '_bis')
                )
            );

        return array($column1, $column2, $column3, $column4);
    }

    public function applyJoin(
        SelectQuery $query,
        SortClause $sortClause,
        $number,
        array $languageSettings
    )
    {

        /**@var \eZ\Publish\API\Repository\Values\Content\Query\SortClause\Target\FieldTarget $fieldTarget */

        $fieldTarget = $sortClause->targetData;
        $fieldMap = $this->contentTypeHandler->getSearchableFieldMap();
        if (!isset($fieldMap[$fieldTarget->typeIdentifier][$fieldTarget->fieldIdentifier]['field_definition_id'])) {
            throw new \InvalidArgumentException(
                '$sortClause->targetData',
                'No searchable fields found for the given sort clause target ' .
                "'{$fieldTarget->fieldIdentifier}' on '{$fieldTarget->typeIdentifier}'."
            );
        }
        $fieldDefinitionId = $fieldMap[$fieldTarget->typeIdentifier][$fieldTarget->fieldIdentifier]['field_definition_id'];

        $table = $this->getSortTableName($number);
        $query
            ->innerJoin(
                $query->alias('ezcontentobject_tree', 'parent')
                ,
                $query->expr->eq(
                    $this->dbHandler->quoteColumn('parent_node_id', 'ezcontentobject_tree'),
                    $this->dbHandler->quoteColumn('node_id', 'parent')
                )
            );
        $query
            ->leftJoin(
                $query->alias(
                    $this->dbHandler->quoteTable('ezcontentobject_attribute'),
                    $this->dbHandler->quoteIdentifier($table)
                ),
                $query->expr->lAnd(
                    $query->expr->eq(
                        $query->bindValue($fieldDefinitionId, null, PDO::PARAM_INT),
                        $this->dbHandler->quoteColumn('contentclassattribute_id', $table)
                    ),
                    $query->expr->eq(
                        $this->dbHandler->quoteColumn('contentobject_id', $table),
                        $this->dbHandler->quoteColumn('contentobject_id', 'parent')
                    ),
                    $query->expr->eq(
                        $this->dbHandler->quoteColumn('version', $table),
                        $this->dbHandler->quoteColumn('contentobject_version', 'parent')
                    ),
                    $query->expr->eq(
                        $this->dbHandler->quoteColumn('language_code', $table),
                        $query->bindValue('fre-FR')
                    )
                )
            );
    }
}

将处理程序声明为服务:

app.sort_clause_handler.location.parent:
            class: ...\SortClauseHandler\ParentSortClauseHandler
            arguments:
                - @ezpublish.api.storage_engine.legacy.dbhandler
                - @ezpublish.spi.persistence.content_type_handler
            tags:
                - {name: ezpublish.search.legacy.gateway.sort_clause_handler.content}
                - {name: ezpublish.search.legacy.gateway.sort_clause_handler.location}

为了把这些碎片放在一起,我做了这样一件事:

$sortQuery[] = new ParentSortClauseField('parentclassname', 'field', $sortOrder);
igetnqfo

igetnqfo2#

不幸的是,对于内置的sort子句,这是不可能的。
你所能做的就是按 code 字段,然后分别为每个培训加载其会话。当然,如果您的数据库中有许多内容对象,这将是不高效的。
另一种可能是实现一个定制的sort子句。

相关问题