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:

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