更新Doctrine2中的关联实体

I have one User entity class, with an associated ParticularData enity class. Through a form, I want to update that ParticularData, so, if I try to get User from session:

$user = $this->getRequest()->getSession()->get('user');
$userRepository = $this->getDoctrine()->getRepository('eCommerceUserBundle:User');
$user->setParticularData($data); // $data is a ParticularData instance fetched from the form 
$userRepository->update($user);

Nothing happens to database (although, for the system that ParticularData HAS changed). Then I try to get User directly from database:

$userRepository = $this->getDoctrine()->getRepository('eCommerceUserBundle:User');
$user = $userRepository->selectById(20);
$user->setParticularData($data);
$userRepository->update($user);

In that case, Doctrine2 treats that new ParticularData as a new instance, so it's trying to insert another Row in the ParticularData associated table (instead of updating the existing one).

My update method:

public function update($user){
    $this->_em->merge($user);
    $this->_em->flush();
}

So, how can I update an associated entity easily telling Doctrine2 to update, not to insert?

The solution I found was to do that in my ParticularData:

/**
* Converts one ParticularData in THIS ParticularData, in order to tell Doctrine2 that the    new ParticularData is the same as before; but with new fields.
* @param ParticularData $data
*/
public function cloneData($data){
    $this->setAddress($data->getAddress());
    $this->setCity($data->getCity());
    $this->setCountry($data->getCountry());
    $this->setName($data->getName());
    $this->setNIN($data->getNIN());
    $this->setPhone($data->getPhone());
    $this->setPostalCode($data->getPostalCode());
    $this->setProvince($data->getProvince());
    $this->setSurname($data->getSurname());

Just update the entity and call $em->flush().

If you're storing an entity in a session, you need to merge it to tell the ORM that the entity must be managed:

$user = $session->get('user');
$user = $em->merge($user);