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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

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

How to add a property to a DataObject


Go to End


10 Posts   5951 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, 5523 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, 56 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, 56 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;

Avatar
Jare

Community Member, 39 Posts

20 August 2015 at 8:44am

Bump. Has anyone found any solution to this problem in SS3 that Lukin had?

Avatar
Lukin

Community Member, 56 Posts

20 August 2015 at 8:20pm

Hi,
I solved my Problem in the post Above.
You can add fields temporarily like that:

iterate through the result-arraylist, add fields with the arrow-Operator,
Push the dataobject into an arraylist And Return it to the Template

Avatar
Jare

Community Member, 39 Posts

21 August 2015 at 12:23am

Sorry, I meant how to achieve that with DataLists. After I have pushed the objects to an ArrayList, I need to create a PaginatedList from it - with which I had issues yesterday (but that's another topic).

I take it that DataLists should not be altered because they present the results of their SQL query?

And now when I try again with ArrayList, I can't even get that working like it did yesterday. However, the PaginatedList now works with ArrayList. It's just that my custom properties are not binding to the objects :/. So which ever I fix, the other will break :D. But no need for you to ask details, I try to work this out on my own and if can't get it working, I'll make another post with a clear example code - either in this topic or a new one, depending on if it's related or not. Thanks :)

Avatar
Pyromanik

Community Member, 419 Posts

21 August 2015 at 1:16am

skim read, may not be fully understanding. But basically sounds like this exact thing: https://docs.silverstripe.org/en/3.1/developer_guides/model/how_tos/grouping_dataobject_sets/

Go to Top