[TYPO3-mvc] Ordering of Tx_Extbase_Persistence_ObjectStorage Objects

Tim Schoch | GSTALTIG tim.schoch at gstaltig.ch
Tue Apr 19 09:39:18 CEST 2011


Hello Falk

! -> This is a quick and dirty method. I modified extbase to add this feature, so use with caution:

To sort, add this line to your TCA (it should work with more then one field if you add a , inbetween): 
'garden_plants' => array(
  'config'  => array(
    'foreign_default_sortby' => 'name ASC',
  ),
),



Classes/Persistence/Mapper/DataMapFactory.php::setOneToManyRelation():
protected function setOneToManyRelation( ... ) {
  ...
  $columnMap->setChildSortbyFieldName($columnConfiguration['foreign_sortby']);
+ >  $columnMap->setChildDefaultSortBy($columnConfiguration['foreign_default_sortby']);
  ...
}


Classes/Persistence/Mapper/DataMapper.php::getPreparedQuery():

      if ($columnMap->getChildSortByFieldName() !== NULL) {
                $query->setOrderings(array($columnMap->getChildSortByFieldName() => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING));
          // FIX TSC, enable sorting order to be set in TCA foreign_default_sortby field. eg: name desc / age asc
      } elseif ( NULL !== $columnMap->getChildDefaultSortBy() ) {
        foreach ( t3lib_div::trimExplode( ',', $columnMap->getChildDefaultSortBy(), TRUE ) as $sortings ) {
          $sorting = t3lib_div::trimExplode( ' ', $sortings, TRUE );
          if ( 2 == count( $sorting ) ) {
            $query->setOrderings(array( $sorting[0] => $sorting[1] ) );
          } else {
            $query->setOrderings(array( $sorting[0] => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING ) );
          }
        }
      }


Classes/Persistence/Mapper/ColumnMap.php:
Added Attribute and getter/setter

protected $childDefaultSortBy;

  public function setChildDefaultSortBy($childDefaultSortBy) {
    $this->childDefaultSortBy = $childDefaultSortBy;
  }

  public function getChildDefaultSortBy() {
    return $this->childDefaultSortBy;
  }


Again, I'm pretty sure there must be a better way then introducing a new field to the TCA, but it works =]

Cheers, Tim


More information about the TYPO3-project-typo3v4mvc mailing list