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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Custom select expression with DataObject::get()?


Go to End


3 Posts   1650 Views

Avatar
sonet

Community Member, 33 Posts

8 December 2012 at 5:43pm

How to add a custom field to DataObject::get()?

Example: DataObject::get()->addSelectField("CONCAT('My', 'S', 'QL')");

Avatar
kinglozzer

Community Member, 187 Posts

10 December 2012 at 11:23pm

http://www.silverstripe.org/data-model-questions/show/21621

If you need them as actual DataObject instances, you can loop over them like this:

		$query = new SQLQuery();
		$query->setFrom('SomeTable');
		$query->selectField("CONCAT('My', 'S', 'QL')");
		$array = $query->execute();

		$results = array();

		// Convert them from simple array items to DataObject instances
		foreach ($array as $row) {
			$results[] = new YourDataObject($row);
		}

		return new ArrayList($results);

Avatar
sonet

Community Member, 33 Posts

19 December 2012 at 8:00am

Thanks, this is what I ended up using. I was just hoping that there is an easier way to add a custom select field.