簡體   English   中英

Symfony OneToMany更新而不是插入

[英]Symfony OneToMany updates instead of insert

我是Symfony的初學者。

我的表格有一個奇怪的問題。

我有2個實體:Proposal_Lsi和Lsi_Beams。 一個提議可以有多個提議,但是一個提議只能有一個提議。 我以為我應該使用OneToMany / ManyToOne關系,而我擁有的一面是梁一,相反的一面是提議。

我遵循了https://symfony.com/doc/3.1/form/form_collections.html上有關表單集合的官方指南。

一切都很好,我可以提交帶有多個光束的新提案,並且所有提案都正確存儲在數據庫中。

每當我嘗試向提案中添加新的梁時都會出現問題 :系統會覆蓋(更新查詢)現有的梁(從數據庫中的第一個梁開始),而不是添加新的梁(插入查詢)。

我想念什么?

如果可以的話,這是我的一些代碼。

提案類別:

class Proposal_lsi{
/**
* @ORM\OneToOne(targetEntity="Emir2Bundle\Entity\Proposal", inversedBy="proposal_lsi")
* @ORM\JoinColumn(name="proposal", referencedColumnName="id")
* @ORM\Id
*/  
private $proposal;

/**
* @ORM\OneToMany(targetEntity="Emir2Bundle\Entity\Lsi_beams", mappedBy="proposal_lsi")
*/  
private $lsi_beams;

...

/**
 * Add lsiBeam
 *
 * @param \Emir2Bundle\Entity\Lsi_beams $lsiBeam
 * @return Proposal_lsi
 */
public function addLsiBeam(\Emir2Bundle\Entity\Lsi_beams $lsiBeam)
{
    $lsiBeam->setProposalLsi($this);
    $this->lsi_beams[] = $lsiBeam;

    return $this;
}

}

梁類:

class Lsi_beams{
/**
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
* @ORM\ManyToOne(targetEntity="Emir2Bundle\Entity\Proposal_lsi", inversedBy="lsi_beams", cascade={"persist"})
* @ORM\JoinColumn(name="proposal_lsi", referencedColumnName="proposal", nullable=false)
*/
private $proposal_lsi;

...
}

以及控制器中的表格:

$form = $this->createFormBuilder($proposallsi)
        ->setAction($this->generateUrl('lsi_submission', array('id' => $id)))
        ->setMethod('POST')
        ->add('lsi_beams', CollectionType::class, array(
            'entry_type'    => LsiBeamsType::class,
            'allow_add'     => true,
            'allow_delete'      => true,
            'prototype'     => true,
            'by_reference'  => false
            )
        )
...

我究竟做錯了什么 ? 讓我知道您是否需要更多代碼。

感謝您的回復!

筆記:

  1. 使用Doctrine ArrayCollection更好地跟蹤集合
  2. cascade={"persist"}放在關聯的反面(您已在mappedBy
  3. 保持實體名稱Lsi_beam (例如Lsi_beam而不是Lsi_beams
  4. 保持清晰的命名策略。 不要在類和屬性名稱中使用undescores(例如,使用$ lsiBeams代替$ lsi_beams)

ProposalLsi

use Doctrine\Common\Collections\ArrayCollection;

class ProposalLsi
{
    /**
    * @ORM\OneToMany(targetEntity="LsiBeam", mappedBy="proposalLsi", cascade={"persist"})
    */  
    private $lsiBeams;

    public function __construct()
    {
        $this->lsiBeams = new ArrayCollection();
    }

    public function addLsiBeam(LsiBeams $lsiBeam)
    {
        if ($this->lsiBeams->contains($lsiBeam)) {

            return;
        } else {

            $lsiBeam->setProposalLsi($this);
            $this->lsiBeams->add($lsiBeam);
        }

        return $this;
    }

    public function removeLsiBeam(LsiBeams $lsiBeam)
    {
        if (!$this->lsiBeams->contains($lsiBeam)) {

            return;
        } else {

            $lsiBeam->setProposalLsi(null);
            $this->lsiBeams->removeElement($lsiBeam);
        }

        return $this;
    }
}

LsiBeam

class LsiBeam
{
    /**
    * @ORM\ManyToOne(targetEntity="ProposalLsi", inversedBy="lsiBeams")
    */
    private $proposalLsi;

    public function setProposalLsi(?ProposalLsi $proposalLsi)
    {
        $this->proposalLsi = $proposalLsi;
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM