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

[SOLVED] Finding and filtering multiple related data objects


Go to End
Reply


18 Posts   2667 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