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 /

Problem with dataobjectset and array


Reply


5 Posts   2354 Views

Avatar
Tigermoon85

Community Member, 6 Posts

26 April 2010 at 7:26pm

Hi!
i have a function,

function GetNews(){
$doSet = new DataObjectSet();
   $news = DataObject::get("News","");
   for($i=0; $i<$news->count(); $i++){
    $header = $news->HEADER_;
    $summary = $news->$SUMMARY_;
    $date = $news->$DAYNEWS_;
      $records = array (
'HEADER' => $header,
'SUMMARY' => $summary,
'DATE' => $date
'NUM' => $i+1);
         $doSet->push(new ArrayData($records));
      }
      return $doSet;
}

and when I call the template function GetNew

<% control GetNew %>
$HEADER
-------$NUM
<% end_control %>

only the output value of $NUM. And $HEADER does not return anything, database has data

Avatar
MarcusDalgren

Community Member, 288 Posts

27 April 2010 at 2:20am

Is this a copy of your code?

In that case you missed an s at the end of GetNews in the template.
If this isn't a copy the next question is why are you simply copying over a DataObjectSet to ArrayData?

You aren't adding anything to the data and you aren't filtering so the function could simply be
function GetNews(){
return DataObject::get("News");
}
Simply use $Pos in the template to get the order number instead of iterating through it and adding it manually as NUM.

Avatar
Pigeon

Community Member, 243 Posts

27 April 2010 at 10:40am

And don't use a for, use a foreach.

If you were set on using a for, don't use $i < $news->Count() because that evaluates the count EVERY ITERATION to compare it to $i. Store the count as a var if you must really use a for.

Avatar
Tigermoon85

Community Member, 6 Posts

27 April 2010 at 9:40pm

Thanks, I also had use $Pos. But when the next page, the next number does not increase. I want the new page it continues to increase. For example, page number 1 i was getting 1,2,3,4,5, then the next page i will getting 6,7,8,9,10 ...

Is this a copy my code:

function GetNews(){
if(!isset($_GET['start']) || !is_numeric($_GET['start'])) $_GET['start'] = 0;
   return $this->start = (int)$_GET['start'];
$doSet = new DataObjectSet();
$news = DataObject::get("News","",,"ORDER_ DESC","", "$this->start,5");
for($i=0; $i<$news->count(); $i++){
$header = $news->HEADER_;
$summary = $news->$SUMMARY_;
$date = $news->$DAYNEWS_;
$records = array (
'HEADER' => $header,
'SUMMARY' => $summary,
'DATE' => $date
'NUM' => $i+1+$this->start);
$doSet->push(new ArrayData($records));
}
return $doSet;
}

I use paging is available in silverstripe, and $start is the number of records in a page.

Avatar
Pigeon

Community Member, 243 Posts

28 April 2010 at 1:54am

Right, there are so many things wrong with that code.

1. for loop. You are accessing $news in every loop of the for, thus creating 4 references to the DOS.
2. you are refering to $news->$SUMMARY_; and $HEADER_; thus, returning null for you.
3. you have too many commas in your get().
4. its inefficient in many ways that i wont go into.

is that really your code? because you have a return right at the top.. so how on earth it would ever create a DOS for you, i have no idea.

try this:

   function GetNews(){
      if(!isset($_GET['start']) || !is_numeric($_GET['start']) || $_GET['start'] < 0) $_GET['start'] = 0;
      $this->start = $_GET['start'];
      
      $news = DataObject::get("News",null,"ORDER_ DESC",null,$this->start . ',5');
      
      foreach ($news as $item) {
         $item->Pos = $item->Pos()+$this->start;
      }
      return $news;
   }

This is *not* tested, but that should be some kind of starting point for you.

Enjoy