如何在唯一id中添加(创建)符号中的对象?



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

我用教义制作了两张表格——人和技能,以及两个控制器。此外,我进行了关联 ManyToOne,以便一个员工可以拥有多种技能。

技能实体

class Skill
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMManyToOne(targetEntity="AppEntityPerson", inversedBy="skills")
* @ORMJoinColumn(nullable=false)
*/
private $person;
/**
* @ORMColumn(type="string", length=255)
*/
private $name;
/**
* @ORMColumn(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
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="string", length=255)
*/
private $name;
/**
* @ORMOneToMany(targetEntity="AppEntitySkill", 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;
}

}

为员工创造/添加新技能的功能

/**
* @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(

您必须在同花顺之前获取该人并将其设置为技能

/**
* @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()
));
}

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

相关内容

  • 没有找到相关文章

最新更新