1-33
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Symfony2 – Franz Jordán 2011<br />
public function showAction($id)<br />
{<br />
$product = $this->getDoctrine()<br />
->getRepository('AcmeStoreBundle:Product')<br />
->findOneByIdJoinedToCategory($id);<br />
$category = $product->getCategory();<br />
// ...<br />
}<br />
More Information on Associations<br />
This section has been an introduction to one common type of entity relationship, the one-tomany<br />
relationship. For more advanced details and examples of how to use other types of<br />
relations (e.g.one-to-one, many-to-many), see Doctrine's Association Mapping Documentation.<br />
If you're using annotations, you'll need to prepend all annotations<br />
with ORM\ (e.g.ORM\OneToMany), which is not reflected in Doctrine's documentation. You'll<br />
also need to include the use Doctrine\ORM\Mapping as ORM; statement,<br />
which importsthe ORM annotations prefix.<br />
Configuration<br />
Doctrine is highly configurable, though you probably won't ever need to worry about most of its<br />
options. To find out more about configuring Doctrine, see the Doctrine section of the reference<br />
manual.<br />
Lifecycle Callbacks<br />
Sometimes, you need to perform an action right before or after an entity is inserted, updated, or<br />
deleted. These types of actions are known as "lifecycle" callbacks, as they're callback methods<br />
that you need to execute during different stages of the lifecycle of an entity (e.g. the entity is<br />
inserted, updated, deleted, etc).<br />
If you're using annotations for your metadata, start by enabling the lifecycle callbacks. This is<br />
not necessary if you're using YAML or XML for your mapping:<br />
/**<br />
* @ORM\Entity()<br />
* @ORM\HasLifecycleCallbacks()<br />
*/<br />
class Product<br />
{<br />
}<br />
// ...<br />
Now, you can tell Doctrine to execute a method on any of the available lifecycle events. For<br />
example, suppose you want to set a created date column to the current date, only when the<br />
entity is first persisted (i.e. inserted):<br />
/**<br />
* @ORM\prePersist<br />
*/<br />
public function setCreatedValue()<br />
{<br />
}<br />
$this->created = new \DateTime();<br />
99