easyadmin crud 控制器:为相关实体增加价值 [英] easyadmin crud controllers: adding value into related entity

查看:46
本文介绍了easyadmin crud 控制器:为相关实体增加价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于 easyadmin3 的问题.在我的管理面板中,我有一个 productCrudController,我希望在创建新产品时能够设置的值之一是价格.对于价格,我有一个单独的表格,其中包含我所有的价格和日期.我的想法是货车的价格会随着时间的推移而变化,我的客户希望能够大致了解每种产品的价格历史.

因此,在我的 productCrudController 中,我使用了一个关联字段来链接到我的价格实体.但是,我确实遇到了以下实际问题:我不想在 priceCrudController 中添加价格,然后我就可以在我的 productCrudController 中进行选择(associationField 希望我这样做).

我想要的是我可以创建一个产品并输入一个价格,然后将其插入到我的价格表中.

我的代码:

productCrudController ->

现在我有一个价格字段,我可以在下拉菜单中选择价格,但我必须先使用 priceCrudController 添加价格,这确实不实用.

class ProductsCrudController 扩展 AbstractCrudController{公共静态函数 getEntityFqcn(): 字符串{返回产品::类;}公共函数 configureFields(string $pageName): 可迭代{$image = ImageField::new('image')->setBasePath('resources/images');$imageFile = TextField::new('imageFile')->setFormType(VichImageType::class);$字段 = [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;} 别的 {$fields[] = $imageFile;}返回 $fields;}

我尝试只为价格"创建一个 numberField 以查看是否可以输入一个值然后将其保留在数据库中,但出现以下错误:

<块引用>

Doctrine\ORM\PersistentCollection 类的对象不能为转换为字符串

这是我的产品"实体和方法中的价格"属性:

/*** @ORM\OneToMany(targetEntity=Prices::class,mappedBy=product")* @Groups({"products:read"})*/私人$价格;/*** @return 收藏|价格[]*/公共函数 getPrices():集合{返回 $this->prices;}公共函数 addPrice(Prices $price): self{如果 (!$this->prices->contains($price)) {$this->prices[] = $price;$price->setProduct($this);}返回 $this;}公共函数 removePrice(Prices $price): self{如果 ($this->prices->removeElement($price)) {//将拥有方设置为 null(除非已经更改)if ($price->getProduct() === $this) {$price->setProduct(null);}}返回 $this;}

我感觉我可能需要对事件侦听器做一些事情,但我真的不知道如何去做,因为我之前没有真正与他们合作过.

非常感谢您的帮助

解决方案

您可以为 Prices 实体创建一个表单,然后在您的产品中使用它

CollectionField::new('prices')->hideOnIndex()->setLabel('布鲁托价格')->setTemplatePath('price.html.twig')->setFormTypeOptions(['标签' =>错误的,'delete_empty' =>真的,'by_reference' =>错误的,])-> setEntryIsComplex(false)->setCustomOptions(['允许添加' =>真的,'允许删除' =>错误的,'entryType' =>PricesType::class,//你的价格表格类在这里'showEntryLabel' =>错误的,]);

I have question concerning easyadmin3. In my admin panel I have a productCrudController and one of the values I want to be able to set when creating a new product is the price. For the price I have a separate table though which contains all my prices with a date. The idea being that the price of a product van change over time and my client wants to be able to have an overview of the price history for each product.

So in my productCrudController I work with an associationField to link to my prices entity. However I'm really stuck with the following practical issue: I don't want to have to add a price in a priceCrudController which I would then be able to select in my productCrudController (the way the associationField expects me to do).

What I want is that I can create a product and input a price which would then be inserted into my prices table.

My code:

productCrudController ->

Right now I have a field for prices where I can select a price in a dropdown menu, but so I have to add the price first with a priceCrudController, which really isn't practical.

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;
    }

I tried just making a numberField for 'prices' to see if I could just enter a value that would then be persisted in the database, but I get the following error:

Object of class Doctrine\ORM\PersistentCollection could not be converted to string

This is my 'prices' property in my 'products' entity and the methods:

   /**
     * @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;
    }

I have the feeling I might need to do something with event listeners, but I don't really know how to go about it as I haven't really worked with them before.

I'd be very grateful for any help

解决方案

You can create a form for the Prices entity and then use it in your product

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,
    ])
    ;

这篇关于easyadmin crud 控制器:为相关实体增加价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆