Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Problem with dataobjectset and array

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

Page: 1
Go to End
Author Topic: 2320 Views
  • Tigermoon85
    Avatar
    Community Member
    6 Posts

    Problem with dataobjectset and array Link to this post

    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

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Problem with dataobjectset and array Link to this post

    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.

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Problem with dataobjectset and array Link to this post

    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.

  • Tigermoon85
    Avatar
    Community Member
    6 Posts

    Re: Problem with dataobjectset and array Link to this post

    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.

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Problem with dataobjectset and array Link to this post

    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

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