Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions

Forcing getRange to work


Reply

4 Posts   503 Views

Avatar
Ryan M.

9 November 2011 at 10:17am Community Member, 309 Posts

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;
   }

Avatar
MarcusDalgren

9 November 2011 at 10:48am Community Member, 288 Posts

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

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

Avatar
Ryan M.

9 November 2011 at 11:16am Community Member, 309 Posts

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!

Avatar
MarcusDalgren

9 November 2011 at 11:39am Community Member, 288 Posts

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.