第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

表單 OneToMany 外鍵空值沖突

表單 OneToMany 外鍵空值沖突

PHP
江戶川亂折騰 2021-12-03 14:33:34
當(dāng)我提交表單并持久化對(duì)象模型時(shí),出現(xiàn)SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "object_id"錯(cuò)誤。我有兩個(gè) Doctrine 實(shí)體: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();}發(fā)生錯(cuò)誤是因?yàn)?Doctrine 將 Document 保存在 Object 之前。是否可以更改保存行為?
查看完整描述

1 回答

?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊

為了解決這個(gè)問(wèn)題,您可以by_reference => false在集合類型上使用。通過(guò)將此設(shè)置為 false,您是在說(shuō)始終使用方法而不是訪問(wèn)屬性。


$builder

    ->add('documents', CollectionType::class, [

        'allow_add' => true,

        'entry_type' => DocumentType::class,

        'by_reference' => false,

    ]);

在某些情況下,將直接使用屬性而不是方法。您可以在此處閱讀有關(guān)此屬性的更多信息。


在對(duì)象的addDocument()方法上添加新文檔時(shí),您還需要設(shè)置對(duì)象。這應(yīng)該類似于:


public function addDocument(Document $document): object

{

    $document->setObject($this);


    $this->getDocuments()->add($document);


    return $this;

}

那應(yīng)該可以解決您的問(wèn)題。我也認(rèn)為命名實(shí)體object有點(diǎn)令人困惑。


查看完整回答
反對(duì) 回復(fù) 2021-12-03
  • 1 回答
  • 0 關(guān)注
  • 188 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)