Jump to:

3433 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Query multiple page classes for pagination - can't merge DataObjectSets?

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

Page: 1
Go to End
Author Topic: 468 Views
  • KINKCreative
    Avatar
    Community Member
    56 Posts

    Query multiple page classes for pagination - can't merge DataObjectSets? Link to this post

    Hello,

    In the course of a project, due to poor planning we added two unrelated page types, VideoPage and ArticlePage that both have a YouTube string holding the YouTube ID. ArticlePage, the newer one was added later for some content purposes and -could- absorb a VideoPage (it has more variables, but the video variable is the same and really for the client's workflow, it would be a hindrance AND more than 200 VideoPages are already live and still being added to some sections. Also the holder pages function with them as well.

    Now. On a particular page I need to aggregate both these page types to show ALL the pages where the YouTube string isn't empty - that means, ArticlePages and VideoPages, and the dataobjectset needs to be paginated.

    I was thinking first to migrate the old page type into the new one. Due to the live and draft stages and the fragile SiteTree table this sounds like a bit of a dangerous task. The VideoPages either wouldn't be in the ArticlePage tables or I would lose the video variable if changing the class type.

    So now I am guessing a custom SQL query will do. How can I select VideoPages and ArticlePages WHERE Video!="" but still preserve the pagination capabilities? Merging dataobjectsets therefore doesn't work.

    VideoPage

    class VideoPage extends Page {

        static $db = array(
           "YouTube" => "Varchar(255)"
        );

    ...

    ArticlePage

    class ArticlePage extends Page implements PermissionProvider {

       static $db = array(
          "YouTube" => "Varchar(255)",
    ...

    VideoAggregatorPage

    class PhotoVideoCollector extends Page {

    ...

    function getPaginatedVideos() {
       if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0;
       $SQL_start = (int)$_GET['start'];
       if(!isset($this->NumberItems) || $this->NumberItems<=0) {
          $n=12;
       }
       else {
          $n= $this->NumberItems;
       }
       
       return $this->getAllVideos($SQL_start,$n);
    }

    public function getAllVideos($start=0,$n=0, $random=false) {
          
          if($random) {
             $sort="RAND()";
          }
          else {
             $sort = "Created DESC";
          }
          
          $doSet = DataObject::get(
             $callerClass = "VideoPage",
             $filter = "YouTube !=''",
             $sort,
             $join = "",
             $limit = "{$start},$n"
          );
          
          return $doSet ? $doSet : false;
       }

    Thanks for any insight!

  • swaiba
    Avatar
    Forum Moderator
    1785 Posts
    468 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.