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 /

How to add a property to a DataObject


Reply


4 Posts   2530 Views

Avatar
Prot

Community Member, 3 Posts

3 April 2010 at 4:21am

Edited: 03/04/2010 4:22am

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.

Avatar
Willr

Forum Moderator, 5513 Posts

7 April 2010 at 4:59pm

Edited: 07/04/2010 4:59pm

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.

Avatar
Lukin

Community Member, 42 Posts

27 October 2012 at 2:58am

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.

Avatar
Lukin

Community Member, 42 Posts

27 October 2012 at 3:46am

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;