Reputation: 128
I am still new to Symfony and can't figure out this error.
Basically I try to remove the byUser
from my database on the table wish
. When the flush()
function is executed I get the error # unable to fetch the response from the backend: unexpected EOF
My controller:
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');
}
From 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;
}
Dev server logs:
[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] []
My code worked before and I did not change anything on purpose. So I am quite lost where to start looking. Would anyone have some idea?
Upvotes: 0
Views: 2061
Reputation: 159
Try edit your user entity atribute wish like this: The main is onDelete="SET NULL"
class User
{
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Wish")
* @ORM\JoinColumn(name="wish_id", referencedColumnName="id", onDelete="SET NULL")
*/
protected $wish;
}
Then you can delete
$this->entityManager->remove($wish);
$this->entityManager->flush();
Upvotes: 1
Reputation: 159
I dont know how looks removeWish($wish); but you can try:
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');
}
Upvotes: 1