Doctrine does not update a simple array type field

后端 未结 2 1977
失恋的感觉
失恋的感觉 2021-02-05 18:48

Short Story (Edit)

It is possible to store an array instead of a mapped association. In Symfony2, this is fairly easy using the collection Field Type. For example, usi

2条回答
  •  独厮守ぢ
    2021-02-05 19:17

    You can find the answer here:

    How to force Doctrine to update array type fields?

    Refs:

    Doctrine uses identical operator === to compare changes between old and new values. The operator used on the same object with different data always return true. There is the other way to solve this issue, you can clone an object that needs to be changed.

    $items = $myEntityObject->getItems(); 
    $items[0] = clone $items[0];
    $items[0]->setSomething(123); 
    $myEntityObject->setItems($items);
    

    Or change the setItems() method

    public function setItems($items) 
    {
        if (!empty($items) && $items === $this->items) {
            reset($items);
            $key = key($items);
            $items[$key] = clone $items[$key];
        }
        $this->items = $items; 
    }
    

提交回复
热议问题