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 />

This metadata can be specified in a number of different formats including YAML, XML or directly<br />

inside the Product class via annotations:<br />

A bundle can accept only one metadata definition format. For example, it's not possible to mix<br />

YAML metadata definitions with annotated PHP entity class definitions.<br />

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

namespace Acme\StoreBundle\Entity;<br />

use Doctrine\ORM\Mapping as ORM;<br />

/**<br />

* @ORM\Entity<br />

* @ORM\Table(name="product")<br />

*/<br />

class Product<br />

{<br />

/**<br />

* @ORM\Id<br />

* @ORM\Column(type="integer")<br />

* @ORM\GeneratedValue(strategy="AUTO")<br />

*/<br />

protected $id;<br />

/**<br />

* @ORM\Column(type="string", length=100)<br />

*/<br />

protected $name;<br />

/**<br />

* @ORM\Column(type="decimal", scale=2)<br />

*/<br />

protected $price;<br />

}<br />

/**<br />

* @ORM\Column(type="text")<br />

*/<br />

protected $description;<br />

The table name is optional and if omitted, will be determined automatically based on the name<br />

of the entity class.<br />

Doctrine allows you to choose from a wide variety of different field types, each with their own<br />

options. For information on the available field types, see the Doctrine Field Types<br />

Reference section.<br />

You can also check out Doctrine's Basic Mapping Documentation for all details about mapping<br />

information. If you use annotations, you'll need to prepend all annotations<br />

with ORM\ (e.g. ORM\Column(..)), which is not shown in Doctrine's documentation. You'll<br />

also need to include the use Doctrine\ORM\Mapping asORM; statement,<br />

which imports the ORM annotations prefix.<br />

85

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

Saved successfully!

Ooh no, something went wrong!