我是 Symfony 的初学者。我陷入困境,因为我无法调试我的电子商务应用程序。我实在不明白问题出在哪里。我正在尝试显示数据库中的数据,但无论我尝试什么,它都会给我一条错误消息。
当我执行转储并死亡时,它输出产品表中的字段,但不输出数据。 当我删除转储并死掉时,我输入了这个 url /product/Rossi (数据库中包含的名称),它给了我一条错误消息:“Symfony\ Bridge\Doctrine\ 未找到“App\Entity\Produit”对象ArgumentResolver\EntityValueResolver”。 当我尝试将 /details 代替 /{name} 并将此 url /product/details 放入搜索栏中时,我收到以下错误消息: 变量“产品”不存在。
谢谢您的帮助!!
控制器
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Produit;
#[Route('/produit', name: 'produit_')]
class ProduitController extends AbstractController
{
#[Route('/', name: 'index')]
public function index(): Response
{
return $this->render('produit/index.html.twig'
);
}
#[Route('/{nom}', name: 'details')]
public function details(Produit $produit): Response
{
// dd($produit->getNom());
return $this->render('produit/details.html.twig'
);
}
}
实体
<?php
namespace App\Entity;
use App\Repository\ProduitRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: ProduitRepository::class)]
class Produit
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 100)]
private ?string $nom = null;
#[ORM\Column(type: Types::TEXT)]
private ?string $description = null;
#[ORM\Column]
private ?int $prix = null;
#[ORM\ManyToOne(inversedBy: 'produits')]
private ?Categorie $id_categorie = null;
#[ORM\OneToMany(mappedBy: 'produits', targetEntity: Images::class)]
private Collection $images;
#[ORM\OneToMany(mappedBy: 'produit', targetEntity: CommandeDetails::class)]
private Collection $commandeDetails;
public function __construct()
{
$this->commandeDetails = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(string $nom): static
{
$this->nom = $nom;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): static
{
$this->description = $description;
return $this;
}
public function getPrix(): ?int
{
return $this->prix;
}
public function setPrix(int $prix): static
{
$this->prix = $prix;
return $this;
}
public function getIdCategorie(): ?Categorie
{
return $this->id_categorie;
}
public function setIdCategorie(?Categorie $id_categorie): static
{
$this->id_categorie = $id_categorie;
return $this;
}
/**
* @return Collection<int, Images>
*/
public function getImages(): Collection
{
return $this->images;
}
public function addImage(Images $image): static
{
if (!$this->images->contains($image)) {
$this->images->add($image);
$image->setProduits($this);
}
return $this;
}
public function removeImage(Images $image): static
{
if ($this->images->removeElement($image)) {
// set the owning side to null (unless already changed)
if ($image->getProduits() === $this) {
$image->setProduits(null);
}
}
return $this;
}
/**
* @return Collection<int, CommandeDetails>
*/
public function getCommandeDetails(): Collection
{
return $this->commandeDetails;
}
public function addCommandeDetail(CommandeDetails $commandeDetail): static
{
if (!$this->commandeDetails->contains($commandeDetail)) {
$this->commandeDetails->add($commandeDetail);
$commandeDetail->setProduit($this);
}
return $this;
}
public function removeCommandeDetail(CommandeDetails $commandeDetail): static
{
if ($this->commandeDetails->removeElement($commandeDetail)) {
// set the owning side to null (unless already changed)
if ($commandeDetail->getProduit() === $this) {
$commandeDetail->setProduit(null);
}
}
return $this;
}
}
树枝
{% extends 'base.html.twig' %}
{% block title %}Détails des produits{% endblock %}
{% block body %}
<h1>Details {{ produit.nom }} </h1>
{% endblock %}
此答案适用于“变量‘产品’不存在”消息
您需要将对象传递到您的视图。
你可以这样做:
#[Route('/{nom}', name: 'details')]
public function details(Produit $produit): Response
{
return $this->render('produit/details.html.twig', [
'produit' => $produit
]);
}