21.10.2015 Views

1-33

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

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

Symfony2 – Franz Jordán 2011<br />

Now that Doctrine knows about your database, you can have it create the database for you:<br />

php app/console doctrine:database:create<br />

Creating an Entity Class<br />

Suppose you're building an application where products need to be displayed. Without even<br />

thinking about Doctrine or databases, you already know that you need a Product object to<br />

represent those products. Create this class inside the Entity directory of<br />

your AcmeStoreBundle:<br />

// src/Acme/StoreBundle/Entity/Product.php<br />

namespace Acme\StoreBundle\Entity;<br />

class Product<br />

{<br />

protected $name;<br />

protected $price;<br />

protected $description;<br />

}<br />

The class - often called an "entity", meaning a basic class that holds data - is simple and helps<br />

fulfill the business requirement of needing products in your application. This class can't be<br />

persisted to a database yet - it's just a simple PHP class.<br />

Once you learn the concepts behind Doctrine, you can have Doctrine create this entity class for<br />

you:<br />

php app/console doctrine:generate:entity --entity="AcmeStoreBundle:Product" --<br />

fields="name:string(255) price:float description:text"<br />

Add Mapping Information<br />

Doctrine allows you to work with databases in a much more interesting way than just fetching<br />

rows of column-based table into an array. Instead, Doctrine allows you to persist<br />

entire objects to the database and fetch entire objects out of the database. This works by<br />

mapping a PHP class to a database table, and the properties of that PHP class to columns on the<br />

table:<br />

For Doctrine to be able to do this, you just have to create "metadata", or configuration that tells<br />

Doctrine exactly how the Product class and its properties should be mapped to the database.<br />

84

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

Saved successfully!

Ooh no, something went wrong!