I keep getting this error in symfony 6 and I dont know what I'm doing wrong, my code seems correct to me. Symfony's argument resolver system is unable to find an instance of the "Products" entity I keep getting this error when I try to inject an instance of the entity class product into a controller parameters, but it cannot find it.
<?php
namespace App\Controller;
use App\Entity\Products;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/produits', name: 'products_')]
class ProductsController extends AbstractController
{
#[Route('/', name: 'index')]
public function index(): Response
{
return $this->render('products/index.html.twig');
}
#[Route('/{slug}', name: 'details')]
public function details(Products $user): Response
{
dd($user);
return $this->render('products/details.html.twig');
}
}
<?php
namespace App\Entity;
use App\Entity\trait\CreatedAtTrait;
use App\Entity\trait\SlugTrait;
use App\Repository\ProductsRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: ProductsRepository::class)]
class Products
{
use CreatedAtTrait;
use SlugTrait;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\Column(type: Types::TEXT)]
private ?string $description = null;
#[ORM\Column]
private ?int $price = null;
#[ORM\Column]
private ?int $stock = null;
#[ORM\ManyToOne(inversedBy: 'Products')]
#[ORM\JoinColumn(nullable: false)]
private ?Categories $categories = null;
#[ORM\OneToMany(mappedBy: 'Products', targetEntity: Images::class, orphanRemoval: true)]
private Collection $images;
#[ORM\OneToMany(mappedBy: 'Products', targetEntity: OrdersDetails::class)]
private Collection $ordersDetails;
public function __construct()
{
$this->images = new ArrayCollection();
$this->ordersDetails = new ArrayCollection();
$this->created_at = new \DateTimeImmutable();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): static
{
$this->description = $description;
return $this;
}
public function getPrice(): ?int
{
return $this->price;
}
public function setPrice(int $price): static
{
$this->price = $price;
return $this;
}
public function getStock(): ?int
{
return $this->stock;
}
public function setStock(int $stock): static
{
$this->stock = $stock;
return $this;
}
public function getCategories(): ?Categories
{
return $this->categories;
}
public function setCategories(?Categories $categories): static
{
$this->categories = $categories;
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->setProducts($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->getProducts() === $this) {
$image->setProducts(null);
}
}
return $this;
}
/**
* @return Collection<int, OrdersDetails>
*/
public function getOrdersDetails(): Collection
{
return $this->ordersDetails;
}
public function addOrdersDetail(OrdersDetails $ordersDetail): static
{
if (!$this->ordersDetails->contains($ordersDetail)) {
$this->ordersDetails->add($ordersDetail);
$ordersDetail->setProducts($this);
}
return $this;
}
public function removeOrdersDetail(OrdersDetails $ordersDetail): static
{
if ($this->ordersDetails->removeElement($ordersDetail)) {
// set the owning side to null (unless already changed)
if ($ordersDetail->getProducts() === $this) {
$ordersDetail->setProducts(null);
}
}
return $this;
}
}
The product $user is not working if it is not the primary key. If you don't want the use the primary key then you need to use MapEntity / ParamConverter for this.
This example is for Symfony 6.2+. Otherwise, check the ParamConverter from the SensioFrameworkBundle.
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
#[Route('/{slug}', name: 'details')]
public function details(
#[MapEntity(mapping: ['slug' => 'name'])] Products $product
): Response
{
dd($product);
return $this->render('products/details.html.twig');
}
I don't see "slug" in your entity. I used "name" as an example.