我很困惑。
我有一个管理所有产品的ProductRepository.php
,然后我有一一个category
属性,它应该连接到CategoryRepository.php
或类别实体。
这是我在ProductRepository.php
中的代码
namespace AppRepository;
use AppEntityProduct;
use AppEntityCategory;
use DoctrineBundleDoctrineBundleRepositoryServiceEntityRepository;
use SymfonyBridgeDoctrineRegistryInterface;
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()
];
}
我的问题是,我在声明Category类时出错了,你知道吗?我的目标是从产品的类别属性中获得category name
,即category model
的id
类别型号为:
namespace AppEntity;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass="AppRepositoryCategoryRepository")
*/
class Category
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(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;
}
}
我想我理解你的意图。
假设产品::类别是产品和类别类别之间的关系:
$categoryName = $product->getCategory()->getName();
您只需获取对象Category,然后获取其名称。