Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » How to add a property to a DataObject

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

Page: 1
Go to End
Author Topic: 2465 Views
  • Prot
    Avatar
    Community Member
    3 Posts

    How to add a property to a DataObject Link to this post

    I'm trying to output a lexicon/glossary, and have some problems grouping the output according to first letters.

    The lexicon items are DataObjects attached to all pages. In the Page_Controller they are collected

       function LexiconAllAggregator() {
          $LexiconSet = DataObject::get(
                 "Lexicon",
                 "`PageID` IN (" . implode(",", $all_lexicon) . ")",
                 "Term ASC",
                 "",
                 ""
                );

          return $LexiconSet ;
       }

    and output in the template

       <% control LexiconAllAggregator %>
          <h3><a href="{$Top.Link}lexikon/$ID">$Term</a></h3>
          <p>$Explanation.FirstParagraph</p>
       <% end_control %>

    Now i want to group them, ie to have divs (with all first letters) after first letter of the terms change. So a new property $Break for $LexiconSet is needed which is should be calculated in the Page_Controller and checked in the template:

       <% control LexiconAllAggregator %>
          <% if Break %> <div> A B C ... </div> <% end_if %>

          <h3><a href="{$Top.Link}lexikon/$ID">$Term</a></h3>
          <p>$Explanation.FirstParagraph</p>
       <% end_control %>

    But I just don't get it how it has to be done to get a new property into the DataObjectSet
    I think it will start with something like that

          $LexiconSetNew = new DataObjectSet();
          foreach ( $LexiconSet as $LexiconSetSingle ) {

    //??????

          }

    Thanks in advance.

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: How to add a property to a DataObject Link to this post

    Welcome to the forums! What you want to do is something like

    foreach ( $LexiconSet as $LexiconSetSingle ) {
    $LexiconSetSingle->FieldName = "Hi";
    }

    Or $LexiconSetSingle->setField('FieldName', 'Hi'); if you want. I believe that should work fine - the set should update with the new fields on the object, if they don't you'll have to define a new $output = new DataObjectSet() and after setting that field push the new object onto the output set and return output rather then $LexiconSet.

  • Lukin
    Avatar
    Community Member
    42 Posts

    Re: How to add a property to a DataObject Link to this post

    This isn't working in ss3?

    $images= Image::get()->leftJoin("GalleryImage", "GI.ImageID = File.ID","GI")->where("GI.GalleriesID='".$this->ID."'");   
          foreach($images as $image) {
                $image->setField('TopPos',333);   
          }
          return $images;   

    I can not use TopPos in Template.

  • Lukin
    Avatar
    Community Member
    42 Posts

    Re: How to add a property to a DataObject Link to this post

    That works...!
    Maybe someone can explain why I need to push the DataList into ArrayList

    $images= DataList::create('Image')->leftJoin("GalleryImage", "GI.ImageID = File.ID","GI")->where("GI.GalleriesID='".$this->ID."'");   
    $imagesAL=new ArrayList();
    $positions=$this->positionErmitteln();
    foreach($images as $image) {
       $image->TopPos=$positions[$image->ID]['topPos'];
       $image->LeftPos=$positions[$image->ID]['leftPos'];
       $imagesAL->push($image);
    }
    return $imagesAL;

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