Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I know that OneToMany relation has been changed?
    primarykey
    data
    text
    <p>I have the following entities:</p> <pre><code>class Person { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * * @ORM\OneToMany(targetEntity="PersonEmail", mappedBy="person", cascade={"persist", "remove"}) */ private $emails; /** * @var datetime $updated * * @ORM\Column(type="datetime") */ private $updated; } </code></pre> <p>.</p> <pre><code>class PersonEmail { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="email", type="string", length=255) */ private $email; /** * @ORM\ManyToOne(targetEntity="Person", inversedBy="emails") * @ORM\JoinColumn(name="person_id", referencedColumnName="id", onDelete="CASCADE") */ private $person; } </code></pre> <p>I use Symfony2 form builder to create forms. After the form is submitted I call <code>$form-&gt;bind($request)</code>. And my question is: how do I know that the emails are changed (inserted, updated or deleted) before I save the Person entity? I need it because I want to update Person::$updated field. I can do it when the Person entity is modified itself using PreUpdate lifecycle callback, but how can I update this field when only email list was changed? Predicting the easiest answer: I would like to avoid updating this field when nothing is changed at all.</p> <p>Updating the Person entity in the PersonEmail's lifecycle callback is not possible, since the Person entity is already processed at that moment.</p> <p>Controller code (not sure if it helps):</p> <pre><code>/** * * @Template() */ public function editAction($id, Request $request) { $repository = $this-&gt;getDoctrine()-&gt;getRepository('AcmeBundle:Person'); $person = $repository-&gt;find($id); if (!$person) { throw $this-&gt;createNotFoundException('No person found for id '.$id); } if ($request-&gt;isMethod('POST')) { $form-&gt;bind($request); if ($form-&gt;isValid()) { $em = $this-&gt;getDoctrine()-&gt;getEntityManager(); $em-&gt;flush(); } } return array('form' =&gt; $form-&gt;createView(), 'person' =&gt; $person); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload