Jump to:

22977 Posts in 11720 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Return Odd DataObjects Only

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 561 Views
  • DeklinKelly
    Avatar
    Community Member
    196 Posts

    Return Odd DataObjects Only Link to this post

    I use this to get 10 "Article" items, ranked by "RankingDate":

    DataObject::get("Article", $q ,'RankingDate DESC','','10');

    I only want ODD items to be returned. Only the 1st, 3rd, 5th, 7th and 9th item should be returned.

    The MySQL query for that looks something like this:

    select *
    from
    (select *, @rn:=@rn+1 rn
    from (select @rn:=0) r, `sitetree`
    WHERE `ClassName` LIKE '%Article%'
    order by `Title`
    limit 10
    ) sq
    where rn%2=1

    How can I do this with DataObject::get()?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Return Odd DataObjects Only Link to this post

    How about....

    $dosArticle = DataObject::get("Article", '','RankingDate DESC','','20');
    $dosOnlyOddArticle = new DataObjectSet();
    if ($dosArticle)
       foreach($dosArticle as $doArticle)
       {
          if($doArticle->Odd())
          {
             $dosOnlyOddArticle->push($doArticle);
          }
       }
    }

  • DeklinKelly
    Avatar
    Community Member
    196 Posts

    Re: Return Odd DataObjects Only Link to this post

    Thanks, swaiba. But your idea is not as kind on memory as if SQL did the work.

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Return Odd DataObjects Only Link to this post

    oh, it's didn't look like that would be an issue, but I guess you know where you need to optimise... I was always taught to get it working (whilst think of memory and CPU cycles) and then come back and address the demonstrated bottlenecks later...

    why don't use directly query your datable using mysql_query or DB::query if this is a real memory hog that you want to streamline?

    561 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.