我犯了一个来自symfony 4的错误,“无法猜测如何从参数"person_id”的请求信息中获得一个Doctrine实例,我已经尝试了我在堆栈溢出中找到的相关问题的选项,但是所有这些选项都建议使用@参数转换器来解决这个问题,但是这个方法与@路由有关,我不认为这是我需要的。
以下是控制器中的代码:
/**
* @Route("/skill/new/", name="new_skill")
* Method({"GET", "POST"})
* @param Request $request
* @param Person $person_id
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
*/
public function new(Request $request, Person $person_id) {
$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();
$person = $entityManager->getRepository(Person::class)->find($person_id);
$person->addSkill($skill);
$entityManager->persist($skill);
$entityManager->persist($person);
$entityManager->flush();
return $this->redirectToRoute('skill_list');
}
return $this->render('main/new.html.twig', array(
'form' => $form->createView()
));
}来自人的实体
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;
}}
使用@参数转换器,我在路径中写了id param,如“*@ With (”/skill/new/{id}“,name="new_skill”),但他又给出了另一个错误“找不到"GET /skill/new”“
我试图实现的是,当我创建新技能时,它会绑定到具有特定身份的特定人,因此我做了ManyToOne assosiation。因此,当我在途中“/person/{ person.id }”时,我需要向这个特定的id添加技能,而不是每个人。
我想我在函数参数上写person_id犯了错误,但否则它在实体管理器中找不到这个param。我怎么才能解决这个问题?
发布于 2019-10-15 12:38:02
问题在路由定义和方法签名中。Symfony无法推断它应该获取哪个Person $person_id。如果您希望这是一个实体,您应该为id指定一个url参数。
@Route("/skill/new/{person_id}", name="new_skill")这将将URL从http://example.com/skill/new更改为http://example.com/skill/new/123,其中123是要获取Person-object的id。现在,您必须在您的URL中有一个人id,否则路由将不匹配(正如您已经注意到的那样)。您可以通过更改方法签名使此选项可选:
/**
* @Route("/skill/new/{person_id}", name="new_skill")
* Method({"GET", "POST"})
* @param Request $request
* @param Person $person_id
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
*/
public function new(Request $request, Person $person_id = null) {通过允许$person_id为null,url参数应该是可选的,因此您应该能够使用http://example.com/skill/skill/new或http://example.com/skill/skill/new/123。
如果您不想要该实体,并且只想要一种可选的方式从URL中获取它,而不显式地指定路由参数,则只需稍微更改代码:
/**
* @Route("/skill/new", name="new_skill")
* Method({"GET", "POST"})
* @param Request $request
* @param Person $person_id
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
*/
public function new(Request $request) {
$person_id = $request->query->get('person_id');
...如果您现在使用您现有的URL并添加一个URL参数,它将在您的操作中读取,例如http://example.com/skill/new?person_id=1234将$person_id设置为1234。当您不指定参数时,它将为空。
Symfony还提供了调试命令,这些命令可以帮助您检查有哪些路由以及它们是否匹配:
bin/console debug:router
bin/console router:match /skill/newhttps://stackoverflow.com/questions/58390110
复制相似问题