I got confused.
I am have an ProductRepository.php
that manages all the product, then I have a category
attribute which should connected to CategoryRepository.php
or Category Entity.
here is my code in ProductRepository.php
namespace App\Repository;
use App\Entity\Product;
use App\Entity\Category;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;
class ProductRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, Product::class);
}
public function transform(Product $product)
{
$category = Category($product->getCategory()); // error
$category = $category->getName();
return [
'id' => (int) $product->getId(),
'name' => (string) $product->getName(),
'category' => (int) $product->getCategory(),
'category_name' => (string) $category,
'SKU' => (string) $product->getSKU(),
'price' => (float) $product->getPrice(),
'quantity' => (int) $product->getQuantity()
];
}
My issue, I got an error on declaring the Category class, any idea about this?
my goal is to get the category name
from the product's category attribute which is the id
of the category model
category Model is:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
*/
class Category
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
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;
}
}