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 15: Inheritance 238<br />

echo get_class($user); // User<br />

If we do the same thing as above but for the Group record, it will return an instance of<br />

Group.<br />

Listing<br />

15-24<br />

// test.php<br />

// ...<br />

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

->from('Entity e')<br />

->where('e.id = ?');<br />

$group = $q->fetchOne(array($group->id));<br />

echo get_class($group); // Group<br />

The above is possible because of the type column. <strong>Doctrine</strong> knows which class each record<br />

was created by, so wh<strong>en</strong> data is being hydrated it can be hydrated in to the appropriate<br />

sub-class.<br />

We can also query the individual User or Group models:<br />

Listing<br />

15-25<br />

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

->select('u.id')<br />

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

Listing<br />

15-26<br />

echo $q->getSqlQuery();<br />

The above call to getSql() would output the following SQL query:<br />

SELECT<br />

e.id AS e__id<br />

FROM <strong>en</strong>tity e<br />

WHERE (e.type = '1')<br />

Notice how the type condition was automatically added to the query so that it will only<br />

return records that are of type User.<br />

Conclusion<br />

Now that we've learned about how to take advantage of PHPs inheritance features with our<br />

models we can move on to learning about <strong>Doctrine</strong> Behaviors (page 239). This is one of the<br />

most sophisticated and useful features in <strong>Doctrine</strong> for accomplishing complex models with<br />

small and easy to maintain code.<br />

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

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

Saved successfully!

Ooh no, something went wrong!