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 10: Compon<strong>en</strong>t Overview 190<br />

First lets create a new <strong>Doctrine</strong>_Query instance to work with:<br />

Listing<br />

10-106<br />

// test.php<br />

// ...<br />

$q = <strong>Doctrine</strong>_Query::create()<br />

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

->leftJoin('u.Phon<strong>en</strong>umber p')<br />

->limit(20);<br />

Now lets create the <strong>Doctrine</strong>_View instance and pass it the <strong>Doctrine</strong>_Query instance as<br />

well as a name for id<strong>en</strong>tifying that database view:<br />

Listing<br />

10-107<br />

// test.php<br />

// ...<br />

$view<br />

= new <strong>Doctrine</strong>_View($q, 'RetrieveUsersAndPhon<strong>en</strong>umbers');<br />

Now we can easily create the view by using the <strong>Doctrine</strong>_View::create() method:<br />

Listing<br />

10-108<br />

// test.php<br />

// ...<br />

try {<br />

$view->create();<br />

} catch (Exception $e) {}<br />

Alternatively if you want to drop the database view you use the <strong>Doctrine</strong>_View::drop()<br />

method:<br />

Listing<br />

10-109<br />

// test.php<br />

// ...<br />

try {<br />

$view->drop();<br />

} catch (Exception $e) {}<br />

Using views are extremely easy. Just use the <strong>Doctrine</strong>_View::execute() for executing<br />

the view and returning the results just as a normal <strong>Doctrine</strong>_Query object would:<br />

Listing<br />

10-110<br />

// test.php<br />

// ...<br />

$users<br />

= $view->execute();<br />

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

print_r($us->toArray());<br />

}<br />

Conclusion<br />

We now have be<strong>en</strong> exposed to a very large perc<strong>en</strong>tage of the core functionality provided by<br />

<strong>Doctrine</strong>. The next chapters of this book are docum<strong>en</strong>tation that cover some of the optional<br />

functionality that can help make your life easier on a day to day basis.<br />

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

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

Saved successfully!

Ooh no, something went wrong!