当我提交表单并持久化对象模型时,出现SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "object_id"错误。
我有两个 Doctrine 实体:
class Object {
/**
* @var Document[]|ArrayCollection
* @ORM\OneToMany(targetEntity="App\Entity\Document", mappedBy="mainObject", cascade={"persist"})
*/
private $documents;
}
class Document
{
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Object", inversedBy="documents")
* @ORM\JoinColumn(nullable=false)
*/
private $object;
}
和 Symfony 形式:
class ObjectType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('documents', CollectionType::class, [
'allow_add' => true,
'entry_type' => DocumentType::class,
])
;
}
}
我的控制器代码:
$object = new Object();
$form = $this->formFactory->create(ObjectType::class, $object);
$form->submit(json_decode($request->getContent(), true), false);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->persist($object);
$this->entityManager->flush();
}
发生错误是因为 Doctrine 将 Document 保存在 Object 之前。是否可以更改保存行为?
精慕HU