Doctrine flush() 导致 "unexpected EOF"

Doctrine flush() leads to "unexpected EOF"

我还是 Symfony 的新手,无法弄清楚这个错误。

基本上我尝试从 table wish 上的数据库中删除 byUser。执行 flush() 函数时出现错误 # unable to fetch the response from the backend: unexpected EOF

我的控制器:

private $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
    $this->entityManager = $entityManager;
}

#[Route('/wish/delete/{id}', name: 'delete_wish')]
public function deleteWish(Wish $wish): Response
{
   $user = $this->getUser();
        
   $user->removeWish($wish);
   $this->entityManager->persist($wish);
   $this->entityManager->flush();

   $this->addFlash("success", "Successfully removed the wish!");

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

来自user.php

public function removeWish(Wish $wish): self
{
    if ($this->wishes->removeElement($wish)) {
        // set the owning side to null (unless already changed)
        if ($wish->getByUser() === $this) {
            $wish->setByUser(null);
        }
    }

    return $this;
}

开发服务器日志:

[2021-05-07T15:16:45.680666+00:00] doctrine.DEBUG: "START TRANSACTION" [] []
[2021-05-07T15:16:45.684106+00:00] doctrine.DEBUG: UPDATE wish SET by_user_id = ? WHERE id = ? [null,3] []

我的代码之前可以正常工作,我没有故意更改任何内容。所以我很迷茫从哪里开始寻找。有人知道吗?

我不知道看起来怎么样 removeWish($wish);但你可以试试:

private $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
    $this->entityManager = $entityManager;
}

#[Route('/wish/delete/{id}', name: 'delete_wish')]
public function deleteWish(Wish $wish): Response
{
   $user = $this->getUser();
        
   $this->entityManager->remove($wish);
   $this->entityManager->flush();

   $this->addFlash("success", "Successfully removed the wish!");

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

尝试像这样编辑您的用户实体属性:主要是在 Delete="SET NULL"

class User
{
    /**
    * @ORM\ManyToOne(targetEntity="App\Entity\Wish")
    * @ORM\JoinColumn(name="wish_id", referencedColumnName="id", onDelete="SET NULL")
    */
    protected $wish;
}

那你就可以删除了

   $this->entityManager->remove($wish);
   $this->entityManager->flush();