ezpublish 5.4根据父字段排序

时间:2018-04-09 08:21:24

标签: php mysql ezpublish

我正在使用ezpublish Search 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'吗?

2 个答案:

答案 0 :(得分:1)

对于内置的排序条款,这是不可能的。

您可以做的是获取按code字段排序的培训,然后为每个培训分别加载其会话。如果您的数据库中有许多内容对象,那么这将无效。

另一种可能性可能是实现可能起作用的custom sort clause

答案 1 :(得分:1)

感谢@Edi和我的同事,我设法让它工作。

创造的两个方面是: 字段类:

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}

要在cotroller中将这些碎片召集在一起,我就这样做了:

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