如何在唯一 ID 内的 symfony 中添加(创建)对象?

How to add(create) object in symfony within unique id?

我有 table 行创建了员工。每个员工都有唯一的 ID。当用户点击员工时,他可以为这个员工添加一个新的技能名称和级别字段。我写了成功创建员工的代码并展示了它,但我不知道如何为唯一 ID 添加技能,它只代表一个特定用户,而不是所有用户。

我制作了两个 table 教义 - 人物和技能,以及两个控制器。另外,我做了多对一的关联,这样一个员工就可以拥有多种技能。

技能实体

class Skill
{
  /**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Person", inversedBy="skills")
 * @ORM\JoinColumn(nullable=false)
 */
private $person;
/**
 * @ORM\Column(type="string", length=255)
 */
private $name;
/**
 * @ORM\Column(type="string", length=255)
 */
private $level;
public function getId(): ?int
{
    return $this->id;
}

public function getPerson(): ?Person
{
    return $this->person;
}

public function setPerson(?Person $person): self
{
    $this->person = $person;

    return $this;
}
public function getName(): ?string
{
    return $this->name;
}

public function setName(string $name): self
{
    $this->name = $name;

    return $this;
}
public function getLevel(): ?string
{
    return $this->level;
}

public function setLevel(string $level): self
{
    $this->level = $level;

    return $this;
}

}

人的实体

class Person
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(type="string", length=255)
 */
private $name;

/**
 * @ORM\OneToMany(targetEntity="App\Entity\Skill", mappedBy="person")
 */
private $skills;

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

public function getId(): ?int
{
    return $this->id;
}

public function getName(): ?string
{
    return $this->name;
}

public function setName(string $name): self
{
    $this->name = $name;

    return $this;
}

/**
 * @return Collection|Skill[]
 */
public function getSkills(): Collection
{
    return $this->skills;
}

public function addSkill(Skill $skill): self
{
    if (!$this->skills->contains($skill)) {
        $this->skills[] = $skill;
        $skill->setPerson($this);
    }

    return $this;
}

public function removeSkill(Skill $skill): self
{
    if ($this->skills->contains($skill)) {
        $this->skills->removeElement($skill);
        // set the owning side to null (unless already changed)
        if ($skill->getPerson() === $this) {
            $skill->setPerson(null);
        }
    }

    return $this;
}

}

应该create/add员工新技能的职能

 /**
 * @Route("/skill/new", name="new_skill")
 * Method({"GET", "POST"})
 */
public function new(Request $request) {
    $skill = new Skill();
    $form = $this->createFormBuilder($skill)
        ->add('name', TextType::class, array('attr' => array('class' => 'form-control')))
        ->add('level', TextareaType::class, array(
            'attr' => array('class' => 'form-control')
        ))
        ->add('save', SubmitType::class, array(
            'label' => 'Create',
            'attr' => array('class' => 'btn btn-primary mt-3')
        ))
        ->getForm();
    $form->handleRequest($request);
    if($form->isSubmitted() && $form->isValid()) {
        $skill = $form->getData();
        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($skill);
        $entityManager->flush();
        return $this->redirectToRoute('skill_list');
    }
    return $this->render('main/new.html.twig', array(
        'form' => $form->createView()
    ));
}

你也必须坚持($person)

您必须在同花顺之前获取 Person 并将其设置为 Skill

/**
 * @Route("/skill/new", name="new_skill")
 * Method({"GET", "POST"})
 */
public function new(Request $request) {
    $personId = $request->request->get('person_id'); //POST data
    $person = $entityManager->getRepository(Person::class)
          ->find($personId);
    $skill = new Skill();
    $skill->setPerson($person);
    $form = $this->createFormBuilder($skill)
      /// add fields
        ->getForm();
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
    /// $skill = $form->getData();
        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($skill);
        $entityManager->flush();

        return $this->redirectToRoute('skill_list');
    }

    return $this->render('main/new.html.twig', array(
        'form' => $form->createView()
    ));
}

在你的情况下,你将一项技术委托给了一个并不真正正确的人。因为当您想将此技术添加到另一个人时,它会向您发送错误。在这种情况下更可取的是 ManyToMany。否则,您始终可以创建关联 class 来检索两个 classes 的标识符。