[TYPO3-mvc] query->setOrderings: order by value list

Stephan Helten stephan.helten at engage.de
Wed Jul 6 12:30:02 CEST 2011


Hi,

yes Peter that is exactly the usecase I meant.

Originally I was looking for a solution on the database site.

My solution now is:

public function findByUids($uids) {
    $query = $this->createQuery();
    $query->matching($query->in('uid', $uids));
    $result = $query->execute();

    // Order by the order of provided array
    $withIndex = array();
    $ordered = array();
    // Create an associative array
    foreach($result AS $object) {
        $withIndex[$object->getUid()] = $object;
    }
    // add to ordered array in right order
    foreach($uids AS $uid) {
        if (isset($withIndex[$uid])) {
            $ordered[] = $withIndex[$uid];
        }
    }

    return $ordered;
}

But I think it is not very nice to to the ordering in PHP...

Regards

Stephan

-----Ursprüngliche Nachricht-----
Von: typo3-project-typo3v4mvc-bounces at lists.typo3.org
[mailto:typo3-project-typo3v4mvc-bounces at lists.typo3.org] Im Auftrag von
Peter Niederlag
Gesendet: Mittwoch, 6. Juli 2011 10:44
An: typo3-project-typo3v4mvc at lists.typo3.org
Betreff: Re: [TYPO3-mvc] query->setOrderings: order by value list

Hello,

Am 05.07.2011 11:04, schrieb Franz Koch:
> Hey,
>
>> The customer is able to define a custom order of the entities in the
>> backend.
>>
>> In my person repository I have a function findByUids expecting a comma
>> separated list of uids (from the flexform value).
>>
>> Wouldn't it be nice to have the ability to call query->setOrderings
>> with the
>> provided list to define the ordering exactly as given in the backend?
>
> shouldn't the objects automatically be selected in the order you defined
> in the uid list? That's at least the case for me IIRC. Of course you
> must take care to not apply default orderings you probably set for your
> repository.

Consider a domain that has a default sorting by "name" and no manual 
sorting column available.

Now you want a "special" plugin where an editor can pick individual 
items from the list. As opposed to the default sorting you want them to 
be displayed in the order the editor has picked them. In TYPO3-BE this 
is possible and I think that's what Stephan has done.

@Stephan:
If extbase query model does not provide means to accomplish sorting by 
"order of the uids the editor has selected" you could spoof it by 
exploding the list of uids and quering each item individually and adding 
them to an array that your repository method returns:

// pseudo-code
findByUidListWithSorting($uidList) {
   $result = array();
   foreach(explode($uidList) as $uid) {
     $result[] = findByUid($uid);
   }
   return $result;
}


Greets,
Peter
-- 
Peter Niederlag
http://www.niekom.de * TYPO3 & EDV Dienstleistungen *
_______________________________________________
TYPO3-project-typo3v4mvc mailing list
TYPO3-project-typo3v4mvc at lists.typo3.org
http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-project-typo3v4mvc



More information about the TYPO3-project-typo3v4mvc mailing list