我有OneToOne关系:用户< - >通知:
class User implements UserInterface
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=50, unique=true)
*/
protected $username;
/**
* @ORM\OneToOne(targetEntity="Wannity\PrivateBundle\Entity\Notification", mappedBy="user", cascade={"all"})
*/
protected $notifications;
}
class Notification
{
/**
* @ORM\Id
* @ORM\OneToOne(targetEntity="Wannity\PublicBundle\Entity\User")
*/
private $user;
/**
* @var boolean
*
* @ORM\Column(name="follower", type="boolean")
*/
private $followerNotif;
class SignupCompleteType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('email', 'email', array('attr' => array('class' => 'text')))
->add('notifications', new NotificationsType(), array('by_reference' => false));
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array('data_class' => 'Wannity\PublicBundle\Entity\User',
'cascade_validation' => true,));
}
public function getName() {
return 'wannity_privatebundle_signupcomplete';
}
}
class ProfileController extends Controller
{
public function configurationAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$form = $this->createForm(new SignupCompleteType(), $user);
$form->handleRequest($request);
if($form->isValid()) {
$em->persist($user);
$em->flush();
return $this->redirect($this->generateUrl('home'));
}
return $this->render('PrivateBundle:Profile:configuration.html.twig', array(
'form' => $form->createView()
));
}
}
表单中显示了包含其值的所有字段(包括嵌入表单),但是当我提交时,Symfony尝试插入新的“通知”而不是更新现有的。为什么呢?
对不起我的英语水平。