0
votes

I'm creating an extbase extension to handle very simple product orders. The model is:

Order --1:n--> OrderItem --1:1--> Product

To order a selection of products, the customer goes to checkout page and uses a Fluid based Order form. All selected products are available in the fluid template as {products}.

The OrderController->createAction processes new Orders by creating OrderItems from the given Products.

Writing the code I'd like to have would look like this:

class OrderController
{
    public function create(Order $order, array $products)
    {
        foreach ($products as $product) {
            $orderItem = new OrderItem()
                ->setProduct($product)
                ->setPrice($product->getPrice());
            $order->addOrderItem($orderItem);
        }
        $this->orderRepository->add($order);
    }
}
  1. How to assign products to a Fluid form fields, in order to receive them as ControllerAction array argument?
  2. How to trigger extbase to automagically provide an array of product objects to the ControllerAction?
  3. I wonder if it is even possible to simplify the Controller createAction like shown below and assemble objects elsewhere:
public function create(Order $order) {
    $this->orderRepository->add($order);
}
1

1 Answers

0
votes

My suggestion for the fluid template would be:

// basic form with the order as main object
<f:form action="create" object="{order}">
    // each order item with a product, using index to have no array with an empty index (Extbase does not like that)
    <f:for each="{products}" key="index" as="product">
        // Here you can set the product
        <f:form.hidden prpoerty="orderItems.{index}.product" value="{product} />
    </f:for>
</f:form>

This should be enough to use your single-line create action. You can change the field type from select to whatever you want, but the __identity is mandatory to say Extbase which record it has to link.

When you want the orderItems to be new created, you need to remove the __identity field.