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 9: DQL (<strong>Doctrine</strong> Query Language) 120<br />

Chapter 9<br />

DQL (<strong>Doctrine</strong> Query Language)<br />

Introduction<br />

<strong>Doctrine</strong> Query Language (DQL) is an Object Query Language created for helping users in<br />

complex object retrieval. You should always consider using DQL (or raw SQL) wh<strong>en</strong> retrieving<br />

relational data effici<strong>en</strong>tly (eg. wh<strong>en</strong> fetching users and their phon<strong>en</strong>umbers).<br />

In this chapter we will execute doz<strong>en</strong>s of examples of how to use the <strong>Doctrine</strong> Query<br />

Language. All of these examples assume you are using the schemas defined in the previous<br />

chapters, primarily the Defining Models (page 53) chapter. We will define one additional<br />

model for our testing purposes:<br />

Listing<br />

9-1<br />

// models/Account.php<br />

class Account ext<strong>en</strong>ds <strong>Doctrine</strong>_Record<br />

{<br />

public function setTableDefinition()<br />

{<br />

$this->hasColumn('name', 'string', 255);<br />

$this->hasColumn('amount', 'decimal');<br />

}<br />

}<br />

Here is the same example in YAML format. You can read more about YAML in the YAML<br />

Schema Files (page 195) chapter:<br />

Listing<br />

9-2<br />

---<br />

# schema.yml<br />

# ...<br />

Account:<br />

columns:<br />

name: string(255)<br />

amount: decimal<br />

Wh<strong>en</strong> compared to using raw SQL, DQL has several b<strong>en</strong>efits:<br />

• From the start it has be<strong>en</strong> designed to retrieve records(objects) not result set rows<br />

• DQL understands relations so you don't have to type <strong>manual</strong>ly sql joins and join<br />

conditions<br />

• DQL is portable on differ<strong>en</strong>t databases<br />

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

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

Saved successfully!

Ooh no, something went wrong!