EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.08k stars 1.02k forks source link

[NEED HELP] Default value CollectionField #4738

Closed jnkconsult closed 3 years ago

jnkconsult commented 3 years ago

Hello, How can I update a value after inserting a "CollectionField"? I have a record (order) and a CollectionField of Items. The user can choice in the ItemsType form the Item and the quantity.

When a user insert a Item, I want to field the Items record with the Item (come from the form), the quantity (come from the form) but with the price of the item to. My idea is to filled this data after the submit of the Order "admin" form.

I try to used the "createEntity" function to do that but I don't find the way to have a impact to a CollectionField.

Thank you

parijke commented 3 years ago

I do not know exactly if this is what you mean. I have a Invoice with InvoiceItem as the collection. In the InvoiceItem-collection I can select a product. A small js script is making an API call to a route that returns the price and fills that in the Invoice Item. Every time I choose another Product, the API call is done and the actual price is retrieved from database.

Does that sounds like your use case?

jnkconsult commented 3 years ago

@parijke Thank you....you solution is a solution like a other...why not. I will try it.

jnkconsult commented 3 years ago

I have find a other way to do. I used the persistEntity function of the AbstractCrudController

    public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void
    {

        foreach ($entityInstance->getOrderItems() as $item)
        {
            $itemOption = new ItemTools($entityManager);
            $prices = $itemOption->getPriceItem($item->getItem(),$entityInstance->getTeam());
            $item->setPriceWithVat($prices['final_price_with_vat']);
            $item->setPriceWithoutVat($prices['final_price_without_vat']);
            $item->setVatType($item->getItem()->getVatType());
        }

        $entityManager->persist($entityInstance);
        $entityManager->flush();
    }

With this solution, with we save a order (create or update), I can fill the prices.

parijke commented 3 years ago

Yes. I had to make sure that a price can be overwritten, so it had to be editable