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

Sebastian Schreiber me at schreibersebastian.de
Sun Jul 17 00:53:41 CEST 2011


Hi,
stumbled upon your post here.
If you would like to do the ordering on the DB-Side you could just do 
the following order by at lease for MySQL:
$orderBy = 'FIELD (uid,"' .implode('","', t3lib_div::intExplode(',', 
$uids) ). '")';

PS: Actually this post has nothing to do with extbase or fluid.


Am 06.07.2011 12:30, schrieb Stephan Helten:
> 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


-- 
Sebastian Schreiber
(Medieninformatiker)
(TYPO3 Certified Integrator)

Sülzburgstraße 36
D-50937 Köln

T  0221 677 88 541
M  0176 431 05 790

Skype schreibersebastian.de

me at schreibersebastian.de
www.schreibersebastian.de

Steuernummer: 219 / 5302 / 2302



More information about the TYPO3-project-typo3v4mvc mailing list