10.04.2018 Views

Doctrine_manual-1-2-en

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 26: Utilities 346<br />

Listing<br />

26-15<br />

Listing<br />

26-16<br />

Listing<br />

26-17<br />

As you may see, there is no need to use other masks except the internals ones. Lets suppose<br />

we implem<strong>en</strong>t a new functionality to search for Users in our exist<strong>en</strong>t application, and we need<br />

to support this feature in pager layout too. To simplify our case, the search parameter is<br />

named "search" and is received through $_GET superglobal array. The first change we need<br />

to do is tho adjust the <strong>Doctrine</strong>_Query object and also the URL, to allow it to be s<strong>en</strong>t to<br />

other pages.<br />

Creating the pager layout:<br />

$pagerLayout = new <strong>Doctrine</strong>_Pager_Layout(<br />

new <strong>Doctrine</strong>_Pager(<br />

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

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

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

->where('LOWER(u.username) LIKE LOWER(?)', array(<br />

'%'.$_GET['search'].'%' ) )<br />

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

$curr<strong>en</strong>tPage,<br />

$resultsPerPage<br />

),<br />

new <strong>Doctrine</strong>_Pager_Range_Sliding(array(<br />

'chunk' => 5<br />

)),<br />

'http://wwww.domain.com/app/User/list/<br />

page,{%page_number}?search={%search}'<br />

);<br />

Check out the code and notice we added a new mask, called {%search}. We'll need to s<strong>en</strong>d it<br />

to the template processing at a later stage. We th<strong>en</strong> assign the templates, just as defined<br />

before, without any change. And also, we do not need to change execution of query.<br />

Assigning templates for page links creation:<br />

$pagerLayout->setTemplate('[{%page}]');<br />

$pagerLayout->setSelectedTemplate('[{%page}]');<br />

// Fetching users<br />

$users = $pagerLayout->execute();<br />

foreach ($users as $user) {<br />

// ...<br />

}<br />

The method display() is the place where we define the custom mask we created. This<br />

method accepts 2 optional argum<strong>en</strong>ts: one array of optional masks and if the output should be<br />

returned instead of printed on scre<strong>en</strong>. In our case, we need to define a new mask, the<br />

{%search}, which is the search offset of $_GET superglobal array. Also, remember that since<br />

it'll be s<strong>en</strong>t as URL, it needs to be <strong>en</strong>coded. Custom masks are defined in key => value pairs.<br />

So all needed code is to define an array with the offset we desire and the value to be<br />

replaced:<br />

// Displaying page links<br />

$pagerLayout->display( array(<br />

'search' => url<strong>en</strong>code($_GET['search'])<br />

) );<br />

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

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

Saved successfully!

Ooh no, something went wrong!