Mon problème est le suivant :
J'ai crée deux entités
Si quelqu'un peut éclairer, ca m'aiderais grandement .
Merci d'avance.
Entourez votre code en utilisant "```" pour bien le mettre en forme. (ne copiez pas trop de code)
Ce que je veux
Décrivez ce que vous cherchez à obtenir.
Ce que j'obtiens
Décrivez vos éventuelles erreurs ou ce que vous obtenez à la place de ce que vous attendez :(
Bonsoir, désolé pour ce retour tardif mais boulot oblige !
voici mes deux entités
ENTITE CATEGORY
<?php
namespace App\Entity;
use App\Repository\CategoryRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
@ORM\Entity(repositoryClass=CategoryRepository::class)
*/
class Category
{
/**
/**
/**
/**
public function __construct()
{
$this->products = new ArrayCollection();
$this->subcategories = new ArrayCollection();
}
public function __toString() {
return $this->getName();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
public function addProduct(Product $product): self
{
if (!$this->products->contains($product)) {
$this->products[] = $product;
$product->setCategory($this);
}
return $this;
}
public function removeProduct(Product $product): self
{
if ($this->products->removeElement($product)) {
// set the owning side to null (unless already changed)
if ($product->getCategory() === $this) {
$product->setCategory(null);
}
}
return $this;
}
public function getSubcategory(): ?string
{
return $this->subcategory;
}
public function setSubcategory(string $subcategory): self
{
$this->subcategory = $subcategory;
return $this;
}
}
ENTITE PRODUCT
<?php
namespace App\Entity;
use App\Repository\ProductRepository;
use Doctrine\ORM\Mapping as ORM;
/**
@ORM\Entity(repositoryClass=ProductRepository::class)
*/
class Product
{
/**
/**
/**
/**
/**
/**
/**
/**
/**
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getIllustration(): ?string
{
return $this->illustration;
}
public function setIllustration(string $illustration): self
{
$this->illustration = $illustration;
return $this;
}
public function getSubtitle(): ?string
{
return $this->subtitle;
}
public function setSubtitle(string $subtitle): self
{
$this->subtitle = $subtitle;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(float $price): self
{
$this->price = $price;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
public function getIsBest(): ?bool
{
return $this->isBest;
}
public function setIsBest(bool $isBest): self
{
$this->isBest = $isBest;
return $this;
}
}
CONTROLLER
<?php
namespace App\Controller;
use App\Classe\Search;
use App\Entity\Product;
use App\Form\SearchType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class ProductController extends AbstractController
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @Route("/nos-produits", name="products")
*/
public function index(Request $request)
{
$products = $this->entityManager->getRepository(Product::class)->find($id);
}
return $this->render('product/index.html.twig', [
'products' => $products
]);
}
et voici ma requete dans le repository
public function findProductWithCategory($category_id)
{
$qb = $this->createQueryBuilder('p')
->select('c', 'p')
->join('p.category', 'c')
->andWhere('c.id = :category_id)')
->setParameter('category_id', $category_id);
return $qb->getQuery()->getResult();
}