1

I can't seem to update an entity when making a PUT request.

It works fine when I want to modify single elements belonging to the entity but not anymore as soon as I try to update an array or an object within it.

This works fine

{   
  "order_number": "TEST NAME"
}

As soon as an array is involved nothing seems to happen.

The quantities in the request are different from the one in the original entity.

{   
  "order_number": "TEST NAME",
  "items": [
    {
      "id": 248,
      "quantity": 0,
      "quantity_shipped": 252
    }
  ]        
}

EDIT:

Foo and Items have a OneToMany relationship as shown in the code below.

class Foo implements FooInterface
{
     /**
     * One Foo has Many Items.
     * @ORM\OneToMany(targetEntity="Item", mappedBy="foo", cascade={"persist","remove"})
     * @JMSSerializer\Groups({"list_orders", "order_details"})
     * @JMSSerializer\MaxDepth(1)
     * @JMSSerializer\Expose
     */
    private $items;

}

I also use a Form to validate the data in the request, and the Items are defined as a CollectionType as expected.

class FooType extends AbstractType  implements FormTypeInterface
{
/**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('items',CollectionType::class, array(
                'entry_type'=>ItemType::class,
                'entry_options' =>array('label'=>false),
                'allow_add' => true,
                'by_reference' => false,
            ))
    }

}

The same was done for the Items:

    class SalesOrderLineItemType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('quantity',TextType::class,array(
                'constraints' =>array(
                    new NotBlank())
            )),
            ->add('quantityShipped',TextType::class,array(
                'constraints' =>array(
                    new NotBlank())
            ))
            ->add('Foo',EntityType::class, array(
                'class'=>'App:Api\Foo',
                'constraints' =>array(
                    new Required(),
                ),
            ))
    }
}

Does anyone know why this is happening?

Do I need to use a forEach loop to check each element?

I'm sure there's a more direct and efficient way of doing this.

Seb
  • 363
  • 1
  • 18

2 Answers2

1

Doctrine has hard time to detect changes on object and array, you need to reset the child object in order to update it again.

You can check how to reset the child here: How to force Doctrine to update array type fields?

davidbonachera
  • 4,416
  • 1
  • 21
  • 29
1

If i understand you correctly you looking for cascade persist analog for updating. Doctrine hasn't such opportunities, so if you want update order item you need do this apart from the order, send PUT request to, for example, /api/order//items/ and then update your item.

Ihor Kostrov
  • 2,361
  • 14
  • 23
  • Since item is a collection of order, does that mean that I'll have to go through the collection and PUT each element individually? I was indeed hoping for a way to update the whole order, detecting differences in a collection. – Seb Aug 23 '19 at 03:16