我開始通過使用多對多關系與學說來建立資料庫。我切換到另一種方法,因為我需要關聯表中的其他欄位。我使用 symfony 5
我有 3 個物體:
namespace App\Entity;
use App\Repository\TemplateRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=TemplateRepository::class)
* @ORM\Table(options={"collate"="utf8mb4_general_ci"})
*/
class Template
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity=TemplateSection::class, mappedBy="section", cascade={"persist"})
*/
private $sections;
....
}
namespace App\Entity;
use App\Repository\SectionRepository;
use App\DBAL\Types\SectionElementType;
use Doctrine\ORM\Mapping as ORM;
use Fresh\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert;
/**
* @ORM\Entity(repositoryClass=SectionRepository::class)
* @ORM\Table(options={"collate"="utf8mb4_general_ci"})
*/
class Section
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity=TemplateSection::class, mappedBy="template")
*/
private $template;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
....
}
namespace App\Entity;
use App\Repository\TemplateSectionRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=TemplateSectionRepository::class)
* @ORM\Table(options={"collate"="utf8mb4_general_ci"})
*/
class TemplateSection
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Template::class, inversedBy="id", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private $template;
/**
* @ORM\ManyToOne(targetEntity=Section::class, inversedBy="id", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private $section;
/**
* @ORM\Column(type="smallint", options={"default": "0"})
*/
private $sortOrder;
....
}
我有一個表單,可以在其中為模板定義新條目。有一個欄位secInput,我可以在其中定義要在此模板中使用的 1 個以上的部分。在 secInput 中是一個逗號分隔的值串列(Ids),用于所選部分。
當我嘗試保存表單時,只保存最后一條記錄 Template.sections
我需要更改什么才能將所有給定資料保存到資料庫?
我在 TemplateController 中的代碼:
/**
* @Route("/new", name="adminTemplateNew", methods={"GET","POST"})
*/
public function new(Request $request): Response
{
$template = new Template();
$form = $this->createForm(TemplateType::class, $template);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$data = $form->getData();
$repo = $entityManager->getRepository(Section::class);
$templateSection = new TemplateSection();
$template->setCreatedAt(new DateTime('NOW'));
$sections = explode(',', $form->get('secInput')->getData());
$count = 1;
foreach ($sections as $secId) {
if ( null !== $section = $repo->find($secId) ) {
$templateSection->setSortOrder($count);
$templateSection->setTemplate($template);
$templateSection->setSection($section);
$template->addSection($templateSection);
$entityManager->persist($templateSection);
$count ;
}
}
$entityManager->persist($templateSection);
$entityManager->persist($template);
$entityManager->flush();
return $this->redirectToRoute('template_index', ['data' => $data], Response::HTTP_SEE_OTHER);
}
uj5u.com熱心網友回復:
您正在迭代選定的部分并在不保存的情況下覆寫它們。當您呼叫 時$entityManager->persist($templateSection),您告訴 EntityManager 跟蹤它,但最終,當您呼叫 時$entityManager->flush(),只有一個物件被持久化。它恰好是最新的資料。
嘗試構造一個新物件并將其持久化,如下所示:
public function new(Request $request): Response
{
$form = $this->createForm(TemplateType::class, $template);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$data = $form->getData();
$repo = $entityManager->getRepository(Section::class);
$template = new Template();
$template->setCreatedAt(new DateTime('NOW'));
$sections = explode(',', $form->get('secInput')->getData());
$count = 1;
foreach ($sections as $secId) {
if ( null !== $section = $repo->find($secId) ) {
$templateSection = new TemplateSection(); // This is new
$templateSection->setSortOrder($count);
$templateSection->setTemplate($template);
$templateSection->setSection($section);
$template->addSection($templateSection);
$entityManager->persist($templateSection);
$count ;
}
}
$entityManager->persist($template);
$entityManager->flush();
return $this->redirectToRoute('template_index', ['data' => $data], Response::HTTP_SEE_OTHER);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/322524.html
