10.04.2018 Views

Doctrine_manual-1-2-en

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 26: Utilities 340<br />

Chapter 26<br />

Utilities<br />

Listing<br />

26-1<br />

Pagination<br />

Introduction<br />

In real world applications, display cont<strong>en</strong>t from database tables is a commom task. Also,<br />

imagine that this cont<strong>en</strong>t is a search result containing thousands of items. Undoubtely, it will<br />

be a huge listing, memory exp<strong>en</strong>sive and hard for users to find the right item. That is where<br />

some organization of this cont<strong>en</strong>t display is needed and pagination comes in rescue.<br />

<strong>Doctrine</strong> implem<strong>en</strong>ts a highly flexible pager package, allowing you to not only split listing in<br />

pages, but also <strong>en</strong>abling you to control the layout of page links. In this chapter, we'll learn<br />

how to create pager objects, control pager styles and at the <strong>en</strong>d, overview the pager layout<br />

object - a powerful page links displayer of <strong>Doctrine</strong>.<br />

Working with Pager<br />

Paginating queries is as simple as effectively do the queries itself. <strong>Doctrine</strong>_Pager is the<br />

responsible to process queries and paginate them. Check out this small piece of code:<br />

// Defining initial variables<br />

$curr<strong>en</strong>tPage = 1;<br />

$resultsPerPage = 50;<br />

// Creating pager object<br />

$pager = new <strong>Doctrine</strong>_Pager(<br />

<strong>Doctrine</strong>_Query::create()<br />

->from( 'User u' )<br />

->leftJoin( 'u.Group g' )<br />

->orderby( 'u.username ASC' ),<br />

$curr<strong>en</strong>tPage, // Curr<strong>en</strong>t page of request<br />

$resultsPerPage // (Optional) Number of results per page. Default is<br />

25<br />

);<br />

Until this place, the source you have is the same as the old <strong>Doctrine</strong>_Query object. The only<br />

differ<strong>en</strong>ce is that now you have 2 new argum<strong>en</strong>ts. Your old query object plus these 2<br />

argum<strong>en</strong>ts are now <strong>en</strong>capsulated by the <strong>Doctrine</strong>_Pager object. At this stage,<br />

<strong>Doctrine</strong>_Pager defines the basic data needed to control pagination. If you want to know<br />

that actual status of the pager, all you have to do is to check if it's already executed:<br />

----------------- Brought to you by

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!