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

[SOLVED] Finding and filtering multiple related data objects


Go to End


18 Posts   4855 Views

Avatar
Ryan M.

Community Member, 309 Posts

7 November 2010 at 12:45pm

You're right it doesn't make any difference. I tried your suggestion of using $do->push($record) instead of $do->push(new ArrayData($record)) but no change. I'm definitely sure it's got something to do with find()

Avatar
Ryan M.

Community Member, 309 Posts

7 November 2010 at 1:44pm

I figured out a solution without even using find().

Final code:

public function FeaturedVideo() {
		$vid = null;
		if($ClassName = "ProfileHolder" && $cat = $this->urlParams['ID']) {
			$cat = str_replace('-', ' ', $cat);
			$cat = ucwords($cat);
			$ccid = DataObject::get_one("ClientCategory", "Name = '{$cat}'")->ID;
			$profiles = DataObject::get("Profile", "ClientCategoryID = $ccid");
			$videoIDs = array();
			foreach($profiles as $profile => $data) {
				foreach($data->Videos() as $video) {
					$videoIDs[] = $video->ID;
				}
			}
			$vids = new DataObjectSet();
			foreach($videoIDs as $id) {
				$record = DataObject::get_by_id("Video", $id);
				if($record->Featured = true) {
					$vids->push($record);
				}
			}
			$vids->sort('LastEdited', 'DESC');
			$vid = $vids->First();
		}
		if($vid) {
			return DataObject::get_by_id("Video", $vid->ID);
		} else
			return DataObject::get_one("Video", "Featured = true");
	}

Go to Top