从数据库中删除的实现不起作用,我不明白为什么
The implementation of deleting from the database does not work, I can not understand why
我有一个实体可以与基地合作
class Task
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $status;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getStatus(): ?bool
{
return $this->status;
}
public function setStatus(?bool $status): self
{
$this->status = $status;
return $this;
}
}
我还有一个控制器可以处理这个对象的 crud。
其中create,update,read实现了,但是删除不起作用
/**
* @Route("/delete/{id}", name="delete_task")
* @param Task $id
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function delete(Task $id)
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($id);
$entityManager->flush();
return $this->redirectToRoute('to_do_list');
}
从列表中删除对象时,会产生此错误
如何解决这个问题。
添加
您很可能需要安装 SensioFrameworkExtraBundle
,然后 Symfony 会自动将您的标识符转换为实体。
这是文档https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html
试试这个方法,准确指定您希望删除的内容
/**
* @Route("/delete/{id}", name="delete_task")
* @param $id
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function delete($id)
{
$tasks = $this->getDoctrine()->getRepository(Task::class)->find($id);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($tasks);
$entityManager->flush();
return $this->redirectToRoute('to_do_list');
}
也试试这个方法,具体说明要删除的内容。我在我的项目中使用这个
if($request->get("id_product_remove"))
{
$id = intval($request->get("id_product_remove"));
$Product= $this->getDoctrine()->getRepository(Product::class)->find($id);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($Product);
$entityManager->flush();
}
我有一个实体可以与基地合作
class Task
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $status;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getStatus(): ?bool
{
return $this->status;
}
public function setStatus(?bool $status): self
{
$this->status = $status;
return $this;
}
}
我还有一个控制器可以处理这个对象的 crud。 其中create,update,read实现了,但是删除不起作用
/**
* @Route("/delete/{id}", name="delete_task")
* @param Task $id
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function delete(Task $id)
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($id);
$entityManager->flush();
return $this->redirectToRoute('to_do_list');
}
从列表中删除对象时,会产生此错误
如何解决这个问题。
添加
您很可能需要安装 SensioFrameworkExtraBundle
,然后 Symfony 会自动将您的标识符转换为实体。
这是文档https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html
试试这个方法,准确指定您希望删除的内容
/**
* @Route("/delete/{id}", name="delete_task")
* @param $id
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function delete($id)
{
$tasks = $this->getDoctrine()->getRepository(Task::class)->find($id);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($tasks);
$entityManager->flush();
return $this->redirectToRoute('to_do_list');
}
也试试这个方法,具体说明要删除的内容。我在我的项目中使用这个
if($request->get("id_product_remove"))
{
$id = intval($request->get("id_product_remove"));
$Product= $this->getDoctrine()->getRepository(Product::class)->find($id);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($Product);
$entityManager->flush();
}