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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Get element by DataObject::get ?


Go to End


2 Posts   1883 Views

Avatar
snaip

Community Member, 181 Posts

7 June 2010 at 8:06pm

Edited: 07/06/2010 8:41pm

hi

simple problem
how to get title from DataObject::get ?

	function a() {
		$x = DataObject::get("SiteTree"); 
		return $x->Title;
	}

doesnt work

EDIT:

	function a() {
		$x = DataObject::get("SiteTree"); 
		foreach($x as $y) {
			echo '<p>' . $y->Title . ' ' . $y->URLSegment . '</p>';
		}
	}

Avatar
Willr

Forum Moderator, 5523 Posts

8 June 2010 at 10:03pm

Explaination - DataObject::get returns a set. When you have a set of objects you can't call methods, fields on the set since they exist on the objects in the set and not the set itself.

DataObject::get('SiteTree'); - returns a set of all SiteTree objects (pages)
DataObject::get_one('SiteTree'); - returns a object - the first SiteTree object
DataObject::get_by_id('SiteTree', $id); - returns a object - the SiteTree object with the ID = $id;

Hope that helps.