Php 如何将id自动添加到相关实体
我有两个实体之间的一对一关系。一个分子和一个ConcGenotox(关于遗传毒性的结论) 我来自一个页面,其中显示了关于一个分子的所有信息,并且该分子的id在地址栏中 有一个按钮可以编辑关于这个分子的新ConcGenotox,它的id总是在地址栏中 我的问题是:在添加新的ConcGenotox时,如何在ConcGenotox的id_分子关系中自动添加分子的id ConcGenotox实体:Php 如何将id自动添加到相关实体,php,symfony,doctrine,Php,Symfony,Doctrine,我有两个实体之间的一对一关系。一个分子和一个ConcGenotox(关于遗传毒性的结论) 我来自一个页面,其中显示了关于一个分子的所有信息,并且该分子的id在地址栏中 有一个按钮可以编辑关于这个分子的新ConcGenotox,它的id总是在地址栏中 我的问题是:在添加新的ConcGenotox时,如何在ConcGenotox的id_分子关系中自动添加分子的id ConcGenotox实体: class ConcGenotox { /** * @var \Molecule
class ConcGenotox
{
/**
* @var \Molecule
*
* @ORM\OneToOne(targetEntity="Molecule")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_molecule", referencedColumnName="id")
* })
*
*/
private $idMolecule;
分子实体:
class Molecule
{
/**
* @ORM\OneToOne(targetEntity="NcstoxBundle\Entity\ConcGenotox", mappedBy="idMolecule")
*/
private $concGenotox;
承包商:
/**
* Creates a new concGenotox entity.
*
* @Route("/new/{id}", name="concgenotox_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request, $id)
{
$concGenotox = new Concgenotox();
$form = $this->createForm('NcstoxBundle\Form\ConcGenotoxType', $concGenotox);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
var_dump($id);
$concGenotox->setIdMolecule($id);
$em->persist($concGenotox);
$em->flush();
return $this->redirectToRoute('concgenotox_show', array('id' => $concGenotox->getId()));
}
return $this->render('concgenotox/new.html.twig', array(
'concGenotox' => $concGenotox,
'form' => $form->createView(),
'id' => $id,
));
}
我试图->setIdMolecule($id)
但它给了我一个错误:
Expected value of type "NcstoxBundle\Entity\Molecule" for association field "NcstoxBundle\Entity\ConcGenotox#$idMolecule", got "string" instead.
试试这个:
$em = $this->getDoctrine()->getManager();
$molecule = $em->getRepository('YourBundle:Molecule')
->find($id);
$concGenotox->setIdMolecule($molecule);
您需要设置实体而不是Id。关系属性应该是实体,而不是Id 更改:
class ConcGenotox
{
/**
* @var \Molecule
*
* @ORM\OneToOne(targetEntity="Molecule")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_molecule", referencedColumnName="id")
* })
*
*/
private $idMolecule;
致:
并将您的操作更改为:
/**
* Creates a new concGenotox entity.
*
* @Route("/new/{molecule}", name="concgenotox_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request, Molecule $molecule)
{
$concGenotox = new Concgenotox();
$form = $this->createForm(ConcGenotoxType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$concGenotox = $form->getData();
$molecule->setConcGenotox($concGenotox);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('concgenotox_show', array('id' => $concGenotox->getId()));
}
return $this->render('concgenotox/new.html.twig', array(
'concGenotox' => $concGenotox,
'form' => $form->createView(),
'id' => $id,
));
}
很高兴帮助你!接受答案以帮助其他人解决同样的问题完成后,有一个计时器来接受解决方案,你回答得太快了:)如果它困扰你,只需创建一个别名
setmolector()
ofsetidmolector()
谢谢,我将把我的关系属性从idMolecule改为molecule。在我的回答中,你的控制器操作也有一些重构。是的,我也会尝试。这可能是一个很好的训练。。。皮肤敏化和systemicTox也需要这样做
/**
* Creates a new concGenotox entity.
*
* @Route("/new/{molecule}", name="concgenotox_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request, Molecule $molecule)
{
$concGenotox = new Concgenotox();
$form = $this->createForm(ConcGenotoxType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$concGenotox = $form->getData();
$molecule->setConcGenotox($concGenotox);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('concgenotox_show', array('id' => $concGenotox->getId()));
}
return $this->render('concgenotox/new.html.twig', array(
'concGenotox' => $concGenotox,
'form' => $form->createView(),
'id' => $id,
));
}