Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
770 views
in Technique[技术] by (71.8m points)

php - How to force Doctrine to update array type fields?

I have a Doctrine entity with array type field:

/**
 * @ORMTable()
 */
class MyEntity
{
    (...)

    /**
     * @var array $items
     * 
     * @ORMColumn( type="array" ) 
     */
    private $items;

    /**
     * @param SomeItem $item 
     */
    public function addItem(SomeItem $item)
    {
        $this->items[] = $item;
    }

    (...)
}

If I add element to the array, this code works properly:

$myEntityObject->addItems(new SomeItem()); 
$EntityManager->persist($myEntityObject);
$EntityManager->flush();

$myEntityObject is saved to the database with correct data (array is serialized, and deserialized when querying database).

Unfortunately, when I change one of the object inside array without changing size of that array, Doctrine does nothing if I'm trying to save changes to the database.

$items = $myEntityObject->getItems();
$items[0]->setSomething(123);
$myEntityObject->setItems($items);
$EntityManager->persist($myEntityObject);
$EntityManager->flush();
print_r($myEntityObject);

Although, print_r in the last line of that code displays changed object's data, Doctrine doesn't know that something was changed inside the array if array size didn't changed. Is there any way to force Doctrine to save changes made in that field (or gently inform it about changes in that field that needs to be saved) ?


Just found in documentation a way to solve my issue:

http://docs.doctrine-project.org/en/latest/reference/change-tracking-policies.html

It requires a lot of changes in the code, but it works. Does someone know how to preserve default tracking policy for other fields and use NotifyPropertyChanged just for the field that stores array?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Doctrine uses identical operator (===) to compare changes between old and new values. The operator used on the same object (or array of objects) 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 (We need to clone only one object to persist the whole array)

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

Regarding the second question:

Does someone know how to preserve default tracking policy for other fields and use NotifyPropertyChanged just for the field that stores array?

You cannot set tracking policy just for a one field.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...