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.

General Questions /

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

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

Custom select expression with DataObject::get()?


Go to End
Reply


3 Posts   628 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, 178 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.