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?