HOW TO Querying Pages in Silverstripe

Last Update : 02/04/2010 2:34am
assets/Uploads/_resampled/SetWidth250-SQL-Query.jpg

Silverstripe uses a class called DataObject to interact with databases. We could say that every interaction with the database will be performed by the DataObject. For example we can do a query for any data we want on the Silverstripe with this object.

In making a new page type called ArticlePage, you may be wondering, "How do I create a list of ArticlePage pages?". We know that every page on silverstripe will be stored in MySQL database. So the answer to that question is that we live make a query on the MySQL database to get the entire list page and we'll display it on other pages.

Actually there are two ways to do it all.


1. Querying If the page it was on the child of a particular page

If the page you want to be queried is in the child of a page, we just call the control named Children in the templates (.ss file). Children will give output an array contain objects of the child page. Each array will have the attributes and methods in accordance with the type of pages.

Example we write like this:

<ul>
<% control Children %>
<li><a href="$Link">$Title</a><p>$Content.Summary(50)</p></li>
<% end_control %>
</ul>

On that page will be shown a html list that contains a list of titles and summaries from the pages of his children.

2. Querying If the pages are not on the child of a particular page

This second condition, we must create a method on the controller class of page you want to display a list of other pages. For example a page that wants to display the list is Page and type of page you want to make the list is ArticlePage. Next we must create a new method on the controller of Page.

mysite/code/Page.php

class Page_Controller extends ContentController{
. . . .
public function ArticlePageList($limit=10){
$start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
$list = DataObject::get("ArticlePage", "", "","","$start , $limit");
return $list;
}
}

This method made ArticlePageList will issue DataObjectSet output that contains a collection of DataObject. Function DataObject::get is a function that is used to retrieve an array of all DataObject(s) named ArticlePage found on the SiteTree no matter where the hierarchy location. In the method there is a parameter $limit used to determine the amount of data will be taken, for the default 10 data will be taken.

Furthermore, to use these methods we should call ArticlePageList controls on the template.

<ul>
<% control ArticlePageList %>
<li><a href="$Link">$Title</a><p>$Content.Summary(50)</p></li>
<% end_control %>
</ul>

At the function DataObject::get, there are several parameters that can be used to customize the output of the query. Suppose we want to take ArticlePage whose title has the word "silverstripe", then the code becomes:

$list = DataObject::get("ArticlePage", "Title LIKE '%silverstripe%' ", "","","$start , $limit");

or if we want to display the results sorted by the most recent data, then the code becomes:

$list = DataObject::get("ArticlePage", "", "Created DESC","","$start , $limit");

For more details about the method on the DataObject class you can learn its functions in the API Documentation of Class DataObject.

Post your comment

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments