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

Pagination on $belongs_many_many


Go to End
Reply


6 Posts   1022 Views

Avatar
dacar

Community Member, 168 Posts

6 July 2013 at 2:12am

Hi, i am currently trying to build a pagination on a dataobject. The DO belongs to many Pages.

<?php
 
class Mitarbeiter extends DataObject {
 
  // Contact object's fields
  private static $db = array(
    'Name' => 'Varchar(255)',
	'Vorname' => 'Varchar(255)',
    'Beschreibung' => 'Text',
    'Webseite' => 'Varchar(255)',
	'Telefon' => 'Varchar(255)',
	'EMail' => 'Varchar(255)'
  );

 
  // One-to-one relationship with profile picture and contact list page
  private static $has_one = array(
    'ProfilePictures' => 'ProfilePictures_Image'
  );

	private static $belongs_many_many = array(
		'Page' => 'SiteTree'
	);

In Page.php it is referenced with

	private static $many_many = array(
		'Mitarbeiter' 		=> 'Mitarbeiter',
        ...

In the Page Controller i use

	public function PaginatedPages() {
	$paginatedList = new AjaxPaginatedList(Mitarbeiter::get(), $this->request);
        $paginatedList->setPageLength(2)->where('"PageID" = '.$this->ID.'')->Sort("SortOrder", "ASC"); 
        return $paginatedList;		 
	}

The Dataobjects are linked to the page via

// Start Mitarbeiterzuordnung
			$MitarbeiterFieldConfig = GridFieldConfig::create()->addComponents(
			  new GridFieldToolbarHeader(),
			  new GridFieldSortableHeader(),
			  new GridFieldDataColumns(),
			  new GridFieldPaginator(10),
			  new GridFieldDeleteAction('unlinkrelation'),
			  new GridFieldSortableRows('SortOrder'),
			  new GridFieldManyRelationHandler(), 'GridFieldPaginator'
			);
			$MitarbeiterField = new GridField("Mitarbeiter", "Mitarbeiter", $this->Mitarbeiter()->sort('SortOrder'), $MitarbeiterFieldConfig);
			$fields->addFieldToTab('Root.Mitarbeiter', $MitarbeiterField);		

But The PaginatedList now lists all Dataobjects. How can i filter over the Page_Mitarbeiter table to only show those Mitarbeiter that are linked to the current page?

Can anybody help?

Greetings, Carsten.

Avatar
(deleted)

Community Member, 473 Posts

6 July 2013 at 10:37am

Use $this->Mitarbeiter() to get the ManyManyList, instead of Mitarbeiter::get().

Avatar
dacar

Community Member, 168 Posts

7 July 2013 at 11:17pm

Edited: 07/07/2013 11:17pm

Hi Simon,

thanks for your reply. I have changed the function to

	public function PaginatedPages() {
        $paginatedList = new PaginatedList($this->Mitarbeiter(), $this->request);
		//var_dump($paginatedList);
        $paginatedList->setPageLength(2);
        $paginatedList->setPageStart(1);
        return $paginatedList;
	}

The <% if PaginatedPages %> returns true. The Headline is showen. But the <% loop PaginatedPages %> returns nothing. Am i missing something? Can you help? Greetings, Carsten.

<% if PaginatedPages %>
                                <div class="clear"></div>
                                <br /><br />
                                <div class="page-title-wrapper">
                                	<h3 class="page-title-left"><% _t('Page.UNSEREMITARBEITER','Ihr Team vom Hotel Hilling') %></h3>
                                </div>
                                <div class="pagination-content">
                                    
                                    <ul>
                                        <% loop PaginatedPages %>
                                        <li><span>Anzahl Datensätze: PaginatedPages.Count</span>
                                        <div class="cpt-item six columns isotope-item" id="Mitarbeiter">
                                            <div class="thumb-wrapper<% if First %> l0<% end_if %>">
                                                
                                                <img class="banner-image inline b5" src="$ProfilePictures.Mitarbeiter.URL" alt="$Vorname $Name">
                                                <div class="thumb-control-wrapper">
                                                    <ul class="thumb-control clearfix">
                                                        <li><a style="margin-left:20px;" rel="prettyPhoto[gallery1]" title="$Title" href="$ProfilePictures.URL" class="go-gallery">Galerie &ouml;ffnen</a></li>
                                                    </ul>
                                                </div>
                                                <p class="red">$PageID $Vorname $Name<br /><span>$Beschreibung</span></p>
                                            </div>
                                        </div>
                                        </li>
                                        <% end_loop %>
                                    </ul>
                                </div>
                                <%-- <img class='pagination-indicator' alt="Loading" src="themes/hilling/images/loader.gif"> --%>
                                <div class="clear"></div>
                                <div class="pagination" $PaginatedPages.PaginationMetadata(2)>
                                    <% if PaginatedPages.MoreThanOnePage %>
                                        <% if PaginatedPages.NotFirstPage %>
                                            <a class="prev" href="$PaginatedPages.PrevLink#Mitarbeiter"><% _t('Page.PREV','die vorherige Seite anschauen') %></a>
                                        <% end_if %>
                                        <% loop PaginatedPages.Pages %>
                                            <% if CurrentBool %>
                                            	$PageNum
                                            <% else %>
                                                <% if Link %>
                                                    <a href="$Link#Mitarbeiter">$PageNum</a>
                                                <% else %>
                                                    ...
                                                <% end_if %>
                                            <% end_if %>
                                        <% end_loop %>
                                        <% if PaginatedPages.NotLastPage %>
                                        	<a class="next" href="$PaginatedPages.NextLink#Mitarbeiter"><% _t('Page.NEXT','Die nächste Seite anschauen') %></a>
                                        <% end_if %>
                                    <% end_if %>
                                </div>
                                <br /><br />
                                <% end_if %>

Avatar
dacar

Community Member, 168 Posts

11 July 2013 at 1:15am

Hi, i am still stucked on this. In page.php i have a many_many relation to the employees (Mitarbeiter). They are related to any page via GridFieldConfig::create(). That works fine. Now i want to show a PaginatedList of these emloyees. Therefore i use the following code in the page.php controller as described in the documentation:

public function PaginatedPages() {
$paginatedList = new PaginatedList($this->Mitarbeiter(), $this->request);
$paginatedList->setPageLength(2);
$paginatedList->setPageStart(1);
$paginatedList->setLimitItems(0);
//$paginatedList->setTotalItems(2);
return $paginatedList;
}

But the function returns all 6 emloyees in the template instead of two? Also the Pagination is showen like

1                                  
<a href="hotel/ueber-uns/team/?start=2#Mitarbeiter">2</a> 
<a href="hotel/ueber-uns/team/?start=4#Mitarbeiter">3</a>
<a class="next" href="hotel/ueber-uns/team/?start=3#Mitarbeiter">Die nächste Seite anschauen</a>

I think there went something wrong, too. Even i used the template code from the documentation http://doc.silverstripe.org/framework/en/3.1/howto/pagination

I have read the Documentation, many forum entries and many IRC Logs. I cant get this one working. Can anybody help, please? Has anybody a working example of a PaginatedList from a many_many relation?

Avatar
dacar

Community Member, 168 Posts

11 July 2013 at 4:13am

Here are some aditional informations:

var_dump($this->Mitarbeiter());

object(ManyManyList)#1263 (15) {
  ["joinTable":protected]=>
  string(16) "Page_Mitarbeiter"
  ["localKey":protected]=>
  string(13) "MitarbeiterID"
  ["foreignKey":protected]=>
  string(6) "PageID"
  ["extraFields":protected]=>
  array(1) {
    ["SortOrder"]=>
    string(3) "Int"
  }
  ["dataClass":protected]=>
  string(11) "Mitarbeiter"
  ["dataQuery":protected]=>
  object(DataQuery)#1264 (8) {
    ["dataClass":protected]=>
    string(11) "Mitarbeiter"
    ["query":protected]=>
    object(SQLQuery)#1265 (12) {
      ["select":protected]=>
      array(1) {
        ["SortOrder"]=>
        string(30) ""Page_Mitarbeiter"."SortOrder""
      }
      ["from":protected]=>
      array(2) {
        ["Mitarbeiter"]=>
        string(13) ""Mitarbeiter""
        ["Page_Mitarbeiter"]=>
        array(4) {
          ["type"]=>
          string(5) "INNER"
          ["table"]=>
          string(16) "Page_Mitarbeiter"
          ["filter"]=>
          array(1) {
            [0]=>
            string(55) ""Page_Mitarbeiter"."MitarbeiterID" = "Mitarbeiter"."ID""
          }
          ["order"]=>
          int(20)
        }
      }
      ["where":protected]=>
      array(1) {
        [0]=>
        string(34) ""Page_Mitarbeiter"."PageID" = '13'"
      }
      ["orderby":protected]=>
      array(0) {
      }
      ["groupby":protected]=>
      array(0) {
      }
      ["having":protected]=>
      array(0) {
      }
      ["limit":protected]=>
      array(0) {
      }
      ["distinct":protected]=>
      bool(true)
      ["delete":protected]=>
      bool(false)
      ["connective":protected]=>
      string(3) "AND"
      ["replacementsOld":protected]=>
      array(0) {
      }
      ["replacementsNew":protected]=>
      array(0) {
      }
    }
    ["collidingFields":protected]=>
    array(0) {
    }
    ["queriedColumns":"DataQuery":private]=>
    NULL
    ["queryFinalised":"DataQuery":private]=>
    bool(false)
    ["querySubclasses":protected]=>
    bool(true)
    ["filterByClassName":protected]=>
    bool(true)
    ["queryParams":"DataQuery":private]=>
    array(2) {
      ["Foreign.ID"]=>
      int(13)
      ["Foreign.Filter"]=>
      string(34) ""Page_Mitarbeiter"."PageID" = '13'"
    }
  }
  ["model":protected]=>
  object(DataModel)#1034 (1) {
    ["customDataLists":protected]=>
    array(0) {
    }
  }
  ["inAlterDataQueryCall":protected]=>
  bool(false)
  ["failover":protected]=>
  NULL
  ["customisedObject":protected]=>
  NULL
  ["objCache":"ViewableData":private]=>
  array(0) {
  }
  ["class"]=>
  string(12) "ManyManyList"
  ["extension_instances":protected]=>
  array(0) {
  }
  ["beforeExtendCallbacks":protected]=>
  array(0) {
  }
  ["afterExtendCallbacks":protected]=>
  array(0) {
  }
}

and

var_dump(array($paginatedList));

array(1) {
  [0]=>
  object(PaginatedList)#1240 (14) {
    ["request":protected]=>
    object(SS_HTTPRequest)#1035 (12) {
      ["url":protected]=>
      string(26) "ueber-uns/philosopie"
      ["dirParts":protected]=>
      array(0) {
      }
      ["extension":protected]=>
      NULL
      ["httpMethod":protected]=>
      string(3) "GET"
      ["getVars":protected]=>
      array(2) {
        ["url"]=>
        string(49) "/ueber-uns/philosopie/"
        ["start"]=>
        string(1) "2"
      }
      ["postVars":protected]=>
      array(0) {
      }
      ["headers":protected]=>
      array(9) {
        ["Host"]=>
        string(15) "www.....de"
        ["User-Agent"]=>
        string(65) "Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0"
        ["Accept"]=>
        string(63) "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
        ["Accept-Language"]=>
        string(35) "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3"
        ["Accept-Encoding"]=>
        string(13) "gzip, deflate"
        ["Dnt"]=>
        string(1) "1"
        ["Referer"]=>
        string(71) "http://www.....de/..."
        ["Cookie"]=>
        string(285) "PastMember=1; PHPSESSID=7ac71f34a6d41fae8c211235fb57b39b; bypassStaticCache=1; cms-panel-collapsed-cms-content-tools-CMSMain=false; cms-panel-collapsed-cms-menu=false; cms-panel-collapsed-cms-content-tools-CMSPagesController=true; cms-panel-collapsed-cms-content-tools-ModelAdmin=false"
        ["Connection"]=>
        string(10) "keep-alive"
      }
      ["body":protected]=>
      string(0) ""
      ["allParams":protected]=>
      array(4) {
        ["URLSegment"]=>
        string(10) "philosopie"
        ["Action"]=>
        NULL
        ["ID"]=>
        NULL
        ["OtherID"]=>
        NULL
      }
      ["latestParams":protected]=>
      array(3) {
        ["Action"]=>
        NULL
        ["ID"]=>
        NULL
        ["OtherID"]=>
        NULL
      }
      ["routeParams":protected]=>
      array(1) {
        ["Controller"]=>
        string(17) "ModelAsController"
      }
      ["unshiftedButParsedParts":protected]=>
      int(2)
    }
    ["getVar":protected]=>
    string(5) "start"
    ["pageLength":protected]=>
    int(2)
    ["pageStart":protected]=>
    NULL
    ["totalItems":protected]=>
    NULL
    ["limitItems":protected]=>
    bool(true)
    ["list":protected]=>
    object(ManyManyList)#1260 (15) {
      ["joinTable":protected]=>
      string(16) "Page_Mitarbeiter"
      ["localKey":protected]=>
      string(13) "MitarbeiterID"
      ["foreignKey":protected]=>
      string(6) "PageID"
      ["extraFields":protected]=>
      array(1) {
        ["SortOrder"]=>
        string(3) "Int"
      }
      ["dataClass":protected]=>
      string(11) "Mitarbeiter"
      ["dataQuery":protected]=>
      object(DataQuery)#1261 (8) {
        ["dataClass":protected]=>
        string(11) "Mitarbeiter"
        ["query":protected]=>
        object(SQLQuery)#1262 (12) {
          ["select":protected]=>
          array(1) {
            ["SortOrder"]=>
            string(30) ""Page_Mitarbeiter"."SortOrder""
          }
          ["from":protected]=>
          array(2) {
            ["Mitarbeiter"]=>
            string(13) ""Mitarbeiter""
            ["Page_Mitarbeiter"]=>
            array(4) {
              ["type"]=>
              string(5) "INNER"
              ["table"]=>
              string(16) "Page_Mitarbeiter"
              ["filter"]=>
              array(1) {
                [0]=>
                string(55) ""Page_Mitarbeiter"."MitarbeiterID" = "Mitarbeiter"."ID""
              }
              ["order"]=>
              int(20)
            }
          }
          ["where":protected]=>
          array(1) {
            [0]=>
            string(34) ""Page_Mitarbeiter"."PageID" = '13'"
          }
          ["orderby":protected]=>
          array(0) {

          }
          ["groupby":protected]=>
          array(0) {
          }
          ["having":protected]=>
          array(0) {
          }
          ["limit":protected]=>
          array(0) {
          }
          ["distinct":protected]=>
          bool(true)
          ["delete":protected]=>
          bool(false)
          ["connective":protected]=>
          string(3) "AND"
          ["replacementsOld":protected]=>
          array(0) {
          }
          ["replacementsNew":protected]=>
          array(0) {
          }
        }
        ["collidingFields":protected]=>
        array(0) {
        }
        ["queriedColumns":"DataQuery":private]=>
        NULL
        ["queryFinalised":"DataQuery":private]=>
        bool(false)
        ["querySubclasses":protected]=>
        bool(true)
        ["filterByClassName":protected]=>
        bool(true)
        ["queryParams":"DataQuery":private]=>
        array(2) {
          ["Foreign.ID"]=>
          int(13)
          ["Foreign.Filter"]=>
          string(34) ""Page_Mitarbeiter"."PageID" = '13'"
        }
      }
      ["model":protected]=>
      object(DataModel)#1034 (1) {
        ["customDataLists":protected]=>
        array(0) {
        }
      }
      ["inAlterDataQueryCall":protected]=>
      bool(false)
      ["failover":protected]=>
      NULL
      ["customisedObject":protected]=>
      NULL
      ["objCache":"ViewableData":private]=>
      array(0) {
      }
      ["class"]=>
      string(12) "ManyManyList"
      ["extension_instances":protected]=>
      array(0) {
      }
      ["beforeExtendCallbacks":protected]=>
      array(0) {
      }
      ["afterExtendCallbacks":protected]=>
      array(0) {
      }
    }
    ["failover":protected]=>
    object(ManyManyList)#1260 (15) {
      ["joinTable":protected]=>
      string(16) "Page_Mitarbeiter"
      ["localKey":protected]=>
      string(13) "MitarbeiterID"
      ["foreignKey":protected]=>
      string(6) "PageID"
      ["extraFields":protected]=>
      array(1) {
        ["SortOrder"]=>
        string(3) "Int"
      }
      ["dataClass":protected]=>
      string(11) "Mitarbeiter"
      ["dataQuery":protected]=>
      object(DataQuery)#1261 (8) {
        ["dataClass":protected]=>
        string(11) "Mitarbeiter"
        ["query":protected]=>
        object(SQLQuery)#1262 (12) {
          ["select":protected]=>
          array(1) {
            ["SortOrder"]=>
            string(30) ""Page_Mitarbeiter"."SortOrder""
          }
          ["from":protected]=>
          array(2) {
            ["Mitarbeiter"]=>
            string(13) ""Mitarbeiter""
            ["Page_Mitarbeiter"]=>
            array(4) {
              ["type"]=>
              string(5) "INNER"
              ["table"]=>
              string(16) "Page_Mitarbeiter"
              ["filter"]=>
              array(1) {
                [0]=>
                string(55) ""Page_Mitarbeiter"."MitarbeiterID" = "Mitarbeiter"."ID""
              }
              ["order"]=>
              int(20)
            }
          }
          ["where":protected]=>
          array(1) {
            [0]=>
            string(34) ""Page_Mitarbeiter"."PageID" = '13'"
          }
          ["orderby":protected]=>
          array(0) {
          }
          ["groupby":protected]=>
          array(0) {
          }
          ["having":protected]=>
          array(0) {
          }
          ["limit":protected]=>
          array(0) {
          }
          ["distinct":protected]=>
          bool(true)
          ["delete":protected]=>
          bool(false)
          ["connective":protected]=>
          string(3) "AND"
          ["replacementsOld":protected]=>
          array(0) {
          }
          ["replacementsNew":protected]=>
          array(0) {
          }
        }
        ["collidingFields":protected]=>
        array(0) {
        }
        ["queriedColumns":"DataQuery":private]=>
        NULL
        ["queryFinalised":"DataQuery":private]=>
        bool(false)
        ["querySubclasses":protected]=>
        bool(true)
        ["filterByClassName":protected]=>
        bool(true)
        ["queryParams":"DataQuery":private]=>
        array(2) {
          ["Foreign.ID"]=>
          int(13)
          ["Foreign.Filter"]=>
          string(34) ""Page_Mitarbeiter"."PageID" = '13'"
        }
      }
      ["model":protected]=>
      object(DataModel)#1034 (1) {
        ["customDataLists":protected]=>
        array(0) {
        }
      }
      ["inAlterDataQueryCall":protected]=>
      bool(false)
      ["failover":protected]=>
      NULL
      ["customisedObject":protected]=>
      NULL
      ["objCache":"ViewableData":private]=>
      array(0) {
      }
      ["class"]=>
      string(12) "ManyManyList"
      ["extension_instances":protected]=>
      array(0) {
      }
      ["beforeExtendCallbacks":protected]=>
      array(0) {
      }
      ["afterExtendCallbacks":protected]=>
      array(0) {
      }
    }
    ["customisedObject":protected]=>
    NULL
    ["objCache":"ViewableData":private]=>
    array(0) {
    }
    ["class"]=>
    string(13) "PaginatedList"
    ["extension_instances":protected]=>
    array(0) {
    }
    ["beforeExtendCallbacks":protected]=>
    array(0) {
    }
    ["afterExtendCallbacks":protected]=>
    array(0) {
    }
  }
}

Avatar
dacar

Community Member, 168 Posts

13 July 2013 at 10:01pm

Hi, just made a 1-to-1 test with the example from [link]http://doc.silverstripe.org/framework/en/3.1/howto/pagination[/link]. This doesn't work on 3.1, too. Has any body any experiences with pagination and SS3.1?

Greetings, Carsten.