我会为两个之间的拯救而疯狂。controller action
entities
我有 2 个实体:
第一个是扩展的 FOSUser
class User extends BaseUser
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var Namespace\LoginBundle\Entity\T $t
*
* @ORM\OneToOne(targetEntity="Namespace\LoginBundle\Entity\T", cascade={"persist"})
*/
private $t;
}
第二个是:
class T
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToOne(targetEntity="Namespace\LoginBundle\Entity\User", cascade={"persist"})
*/
private $user;
}
当我登录到我的应用程序并且User
仍然保存在我的数据库中时,我有一行T
字段null
。
在我的控制器中,我有这个方法:
public function createAction()
{
$entity = new T();
// user
$user = $this->get('security.context')->getToken()->getUser();
$entity->setUser($user);
$user->setT($entity);
$request = $this->getRequest();
$form = $this->createForm(new TType(), $entity);
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($entity);
$em->persist($entity->getUser());
$em->persist($user->getT());
$em->flush();
return $this->redirect($this->generateUrl('t_show', array('id' => $entity->getId())));
}
return $this->render('NamespaceXXXXBundle:T:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView()
));
}
我不明白为什么,我有这个错误。
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`namespace`.`t`, CONSTRAINT `FK_58C6694C54EE02A4` FOREIGN KEY (`user_id`) REFERENCES `User` (`id`))
请帮我
山姆