我对easyadmin3有疑问。在我的管理面板中,我有一个productCrudController,在创建新产品时我希望能够设置的值之一就是价格。至于价格,我有一个单独的表,其中包含了我所有的价格和日期。我的想法是,一辆面包车的价格会随着时间的推移而变化,我的客户希望能够对每一种产品的价格历史进行概述。
因此,在我的productCrudController中,我使用一个associationField链接到我的价格实体。然而,我确实遇到了以下实际问题:我不想在priceCrudController中添加一个价格,这样我就可以在productCrudController中选择( associationField希望我这样做)。
我想要的是,我可以创建一个产品,并输入一个价格,然后将插入到我的价格表。
我的代码:
productCrudController ->
现在我有一个价格字段,我可以在下拉菜单中选择一个价格,但是我必须先用priceCrudController添加价格,这实际上是不切实际的。
class ProductsCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Products::class;
}
public function configureFields(string $pageName): iterable
{
$image = ImageField::new('image')->setBasePath('resources/images');
$imageFile = TextField::new('imageFile')->setFormType(VichImageType::class);
$fields = [
IdField::new('id', 'ID')->hideOnForm(),
TextField::new('name'),
TextEditorField::new('description'),
AssociationField::new('category'),
AssociationField::new('plants')->setTemplatePath('list.html.twig'),
NumberField::new('stock'),
AssociationField::new('prices', 'bruto price')->onlyOnIndex()->setTemplatePath('price.html.twig'),
];
if($pageName == Crud::PAGE_INDEX || $pageName == Crud::PAGE_DETAIL){
$fields[] = $image;
} else {
$fields[] = $imageFile;
}
return $fields;
}
我试着为“价格”制作一个numberField,看看是否可以输入一个值,然后将其持久化到数据库中,但是我得到了以下错误:
类Doctrine\ORM\PersistentCollection的
对象无法转换为字符串
这是我的“产品”实体中的“价格”属性和方法:
/**
* @ORM\OneToMany(targetEntity=Prices::class, mappedBy="product")
* @Groups({"products:read"})
*/
private $prices;
/**
* @return Collection|Prices[]
*/
public function getPrices(): Collection
{
return $this->prices;
}
public function addPrice(Prices $price): self
{
if (!$this->prices->contains($price)) {
$this->prices[] = $price;
$price->setProduct($this);
}
return $this;
}
public function removePrice(Prices $price): self
{
if ($this->prices->removeElement($price)) {
// set the owning side to null (unless already changed)
if ($price->getProduct() === $this) {
$price->setProduct(null);
}
}
return $this;
}
我有一种感觉,我可能需要和事件侦听器一起做些什么,但是我不知道如何去做,因为我以前从来没有和他们合作过。
我非常感谢你的帮助
发布于 2021-06-11 14:08:16
您可以为“价格”实体创建一个表单,然后在产品中使用它。
CollectionField::new('prices')
->hideOnIndex()
->setLabel('bruto price')
->setTemplatePath('price.html.twig')
->setFormTypeOptions([
'label' => false,
'delete_empty' => true,
'by_reference' => false,
])
->setEntryIsComplex(false)
->setCustomOptions([
'allowAdd' => true,
'allowDelete' => false,
'entryType' => PricesType::class, // Your price form class here
'showEntryLabel' => false,
])
;
https://stackoverflow.com/questions/67840798
复制相似问题