Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Forcing getRange to work

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

Page: 1
Go to End
Author Topic: 469 Views
  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Forcing getRange to work Link to this post

    I feel so noobish for posting this. I've been unable to force getRange to work! It displays more posts than I want.

    public function getCombinedNews($limit) {
          $name = $this->Title;
          $entries = DataObject::get("BlogEntry", "Tags LIKE '%{$name}%'");
          $articles = $this->Articles();
          $articles->merge($entries);
          $articles->sort('Created', 'DESC');
          if(isset($limit)) {
             $articles->getRange(0,$limit);
          }
          return $articles;
       }

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Forcing getRange to work Link to this post

    It returns the range as a new DataObjectSet so it needs to be

    $articles = $articles->getRange(0,$limit);

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: Forcing getRange to work Link to this post

    Thanks for the tip! Unfortunately, that didn't work. To make sure it wasn't the if(isset()) part, I commented it out so that the limit would get applied regardless. It returns no objects at all now!

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Forcing getRange to work Link to this post

    Then I would be checking exactly what you're feeding the function. getRange is just

       public function getRange($offset, $length) {
          $set = array_slice($this->items, (int)$offset, (int)$length);
          return new DataObjectSet($set);
       }

    As you can see it's just array_slice() with one variable for where to start and one for how many to grab. Likely your $limit variable is either 0 or something else that gets turned into 0 by the int cast. Also since your function definition requires that $limit is sent to the function your isset will always return true.

    469 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.