简体   繁体   English

TYPO3 Extbase:如何对子对象进行排序

[英]TYPO3 Extbase: How to sort child objects

I have an Extbase Model Article and a 1:n Relation Product. 我有一个Extbase模型文章和1:n关系产品。 In Article TCA i have an inline field configuered. 在文章TCA中,我有一个内联字段配置。 In my Article Template I want to display all related Products. 在我的文章模板中,我想显示所有相关产品。 These are oredered by uid. 这些都是由uid提供的。 How can i change the ordering of the child objects to the field sorting to be able to manually sort them. 如何将子对象的顺序更改为字段排序以便能够手动对它们进行排序。 ( In the backend form the sorting is possible, only diplaying them sorted by field sorting is not possible ) (在后端形式中可以进行排序,只能通过字段排序对它们进行双层排序)

thanks, Lukas 谢谢,卢卡斯

The easiest way to sort child elements in a object storage is to manipulate the TCA. 在对象存储中对子元素进行排序的最简单方法是操作TCA。

Example: 例:

$TCA['tx_myext_domain_model_ordering'] = array(
...
'columns' => array(
    'services' => array(
        'exclude' => 0,
        'label' => 'LLL:EXT:myext/Resources/Private/Language/locallang_db.xml:tx_myext_domain_model_ordering.services',
        'config' => array(
            'type' => 'inline',
            'foreign_table' => 'tx_myext_domain_model_service',
            'foreign_field' => 'ordering',
            'foreign_sortby' => 'sorting',
            'maxitems'      => 9999,
            'appearance' => array(
                'collapseAll' => 0,
                'levelLinksPosition' => 'top',
                'showSynchronizationLink' => 1,
                'showPossibleLocalizationRecords' => 1,
                'showAllLocalizationLink' => 1
            ),
        ),
    ),
),
...
);

With the filed 'foreign_sortby' => 'sorting', you can easily say on which field the childs should be ordered. 通过提交的'foreign_sortby'=>'排序',您可以轻松地说明应该在哪个字段中订购子项。

Here's how I do it, in the repository class: 以下是我在存储库类中的操作方法:

class ItemRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {

    /**
     * http://www.typo3.net/forum/thematik/zeige/thema/114160/?show=1
     * Returns items of this repository, sorted by sorting
     *
     * @return array An array of objects, empty if no objects found
     * @api
     */

    public function findAll() {
        $orderings = array(
            'sorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
        );

        $query = $this->createQuery();
        $query->setOrderings($orderings);

        $query->getQuerySettings()->setRespectSysLanguage(FALSE);
        $query->getQuerySettings()->setSysLanguageUid(0);

        $result = $query->execute();
        return $result;
    }

}

Using the dot notation you can sort by properties of subobjects: 使用点表示法,您可以按子对象的属性进行排序:

$query->setOrderings(array(
    'sorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
    'subobject.sorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
));

edit: Caught me off guard, I think I read your question wrong. 编辑:让我措手不及,我想我读错了你的问题。 Yes, this sorts by child objet, but it applies that to the parent object, of course. 是的,这可以通过子对象进行排序,但它当然适用于父对象。 If you want to sort the child objects themselves, you have to set that sorting in the repository of the child object, as @Urs explained. 如果要对子对象本身进行排序,则必须在子对象的存储库中设置该排序,如@Urs所述。

I dont't really like the idea of doing basic sorting inside the view (mvc). 我不喜欢在视图(mvc)中进行基本排序的想法。

The Downside of most Database abstraction Layers ist, that you are quite often restricted to either mixup mvc or have a (sometimes slightly) lower performance. 大多数数据库抽象层的缺点是,您经常被限制为混合mvc或具有(有时略微)较低的性能。

I am at the same point right now. 我现在正处于同一点。 I am thinking about retrieving the parents (with their children attached). 我正在考虑检索父母(附带孩子)。 Then go into every single parent and get children for this parent in a sorted way. 然后进入每个单亲家庭,并以有序的方式为这个父母获得孩子。

public function findAllSorted() {
    $query = $this->createQuery();
    /* sort parents */
    $query->setOrderings(array('name' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING));
    $parents = $query->execute();
    foreach($parents as $parent){
        /* get children for every parent */
        $children = $this->getChildrenSorted($parent);
        // add children to parent TBD

    }
    /* Debug output */
    \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($parents);
}

public function getChildrenSorted(\mynamespace\ $parent) {
    /* Generate an object manager and use dependency injection to retrieve children repository */
    $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\TYPO3\CMS\Extbase\Object\ObjectManager');
    $childrenRepository= $objectManager->get('MYNAME\Extname\Domain\Repository\Repository');
    $children = $versionRepository->findSortedByName($parent);
    return $children;
}

The function findSortedByName inside the children repos is using 子回购中的函数findSortedByName正在使用

$query->matching($query->equals('parent', $parent));

to retrieve only children of this parent and 只检索这个父母和孩子的孩子

$query->setOrderings(array('name' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING));

to be able to give them back in an ordered way. 能够以有序的方式回馈他们。

Warning: Depending on the size and quantity of all retrieved objects, this way might arise a huge performance issue. 警告:根据所有检索到的对象的大小和数量,这种方式可能会出现巨大的性能问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM