【发布时间】:2021-02-10 08:07:39
【问题描述】:
所以情况是这样的: 我有一个实体 ProductWhisky,它是 ProductAbstract 的子类,我在其中引用 Product 实体(一对一)。 Product 与 ProductProducer 具有多对一关联。我正在使用 Easyadmin 3 来编辑这个 ProductWhisky。
所以这是我的 ProductWhiskyCrudController 的一部分:
public function configureFields(string $pageName): iterable
{
// more fields
yield AssociationField::new('productProducer', 'Producent');
// even more fields
}
这是我的 ProductWhiskyEntity:
class ProductWhisky extends ProductAbstract
{
use ProductTrait;
//props and other methods specific to ProductWhisky
}
产品特性
trait ProductTrait
{
/**
* @ORM\OneToOne(targetEntity="App\Entity\Product", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $product;
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(Product $product): self
{
$this->product = $product;
return $this;
}
/**
* @return ProductProducer|null
*/
public function getProductProducer(): ?ProductProducer
{
if ($this->getProduct()) {
return $this->getProduct()->getProducer();
}
return null;
}
产品实体:
class Product
{
// more props
/**
* @ORM\ManyToOne(targetEntity="App\Entity\ProductProducer", inversedBy="products", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
*/
private $producer;
// more props and methods
public function getProducer(): ?ProductProducer
{
return $this->producer;
}
public function setProducer(?ProductProducer $producer): self
{
$this->producer = $producer;
return $this;
}
所以,当我调用 ProductWhiskyCrudController 时,我总是收到消息 The "productProducer" field is not a Doctrine association, so it cannot be used as an association field.
在 EasyAdmin 2 中,我这样做了:
form:
fields:
- property: 'productProducer'
label: 'Producent'
type: entity
type_options: {class: 'App\Entity\ProductProducer', required: true}
知道如何解决这个问题吗?
一切顺利 蒂姆
【问题讨论】:
标签: symfony doctrine easyadmin