尚未通过研究找到解决此问题的方法,但是我试图将两种形式(嵌入/集合)保存到数据库中。我有彼此相关的实体,并且我希望表单提交两个实体并将其持久化到数据库中。
主要实体:
/**
* @var integer
* @ORM\OneToMany(targetEntity="Sub", mappedBy="mainId", cascade={"persist"})
*/
protected $sub;
public function __construct() {
$this->sub = new ArrayCollection();
}
子实体:
/**
* @var integer
*
* @ORM\Column(name="main_id", type="integer")
*/
protected $mainId;
.......
/**
* @ORM\ManyToOne(targetEntity="Main", inversedBy="sub")
* @ORM\JoinColumn(name="main_id", referencedColumnName="id")
*/
protected $main;
这是我的MainType表单:
class MainType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('dano', 'text', array(
'label' => 'DA: ',
'disabled' => true
))
->add('partno','text', array(
'label' => 'Part: ',
'disabled' => true
))
->add('batchno', 'text', array(
'label' => 'Batch: ',
'disabled' => true
))
->add('sub', 'collection', array('type' => new SubType()))
->add('submit', 'submit');
}......
和我的SubType形式:
class SubType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('main_id','hidden')
->add('rackno','text', array(
'label' => 'Rack No(s) '
))
->add('diecode','text', array(
'label' => 'Die Code '
))
->add('heatcode','text', array(
'label' => 'Heat Code '
))
->add('inqty','integer', array(
'label' => 'Qty In '
))
->add('onhold','choice', array(
'label' => 'Hold',
'choices' => array(
'1' => 'On Hold',
'0' => 'Released'
),
'multiple' => false,
'expanded' => true
));
而我的控制器:
/**
* @param Request $request
* @Route("/{dano}", name="subpart_part")
*/
public function submitPartByDAAction(Request $request, $dano) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('Bundle:Main')
->findOneByDano($dano);
$partno = $entity->getPartno();
$batchno = $entity->getBatchno();
$mainid = $entity->getId();
$main1 = new Main();
$main1->setDano($dano);
$main1->setPartno($partno);
$main1->setBatchno($batchno);
$sub1 = new Sub();
$sub1->setMainId($mainid);
$main1->getSub()->add($sub1);
$form = $this->createForm(new MainType(), $main1, array(
'method' => 'POST'
));
$form->handleRequest($request);
if($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($main1);
$em->flush();
return $this->redirect($this->generateUrl('subpart_home'));
}
return $this->render('Bundle:Parts:addparts.html.twig', array(
'form' => $form->createView()
));
}
让我解释一下我在这里所做的事情,起初我没有Sub的“ main_id”字段(与Main的id有关),但是当我尝试保留数据时,它给了我错误:
An exception occurred while executing 'INSERT INTO sub
(main_id, rackno, heatcode, diecode, inqty, onhold) VALUES
(?, ?, ?, ?, ?, ?)' with params [null, "46", "eterte", "seteter", 3, 0]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column
'main_id' cannot be null
因此,我创建了一个隐藏的字段“ main_id”,并通过getId()从Main获取了ID。并将其传递到Sub表单的setMainId();中。持久,它仍然给我同样的错误,即“ main_id”不能为null。
我想念什么?谢谢!
您定义的实体错误。首先了解ORM和关系的概念。您的Sub
实体不需要具有integer main_id
。只需将其映射到Main
实体。您的Main
实体看起来应该像
/**
* @var Sub
* this value is just integer in database, but doc should point it to Sub
* @ORM\OneToMany(targetEntity="Sub", mappedBy="mainId", cascade={"persist"})
*/
protected $sub;
public function __construct() {
$this->sub = new ArrayCollection();
}
而你的Sub
实体
/**
* @ORM\ManyToOne(targetEntity="Main", inversedBy="sub")
* @ORM\JoinColumn(name="main_id", referencedColumnName="id")
*/
protected $main;
您不需要main_id。ORM将为您处理。该MainType
形式是好的。只是摆脱SubType
形式的main_id 。
您应该按对象而不是其ID引用实体。在您的控制器中也要使用
$sub1->setMainId($mainid);
您应该设置对象。
$sub1->setMain($main1);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句