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   548 Views

Avatar
Ryan M.

Community Member, 309 Posts

9 November 2011 at 10:17am

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

Community Member, 288 Posts

9 November 2011 at 10:48am

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

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

Avatar
Ryan M.

Community Member, 309 Posts

9 November 2011 at 11:16am

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

Community Member, 288 Posts

9 November 2011 at 11:39am

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.