如何强制学说更新数组类型的字段? [英] How to force Doctrine to update array type fields?

查看:230
本文介绍了如何强制学说更新数组类型的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数组类型字段主义实体:

I have a Doctrine entity with array type field:

/**
 * @ORM\Table()
 */
class MyEntity
{
    (...)

    /**
     * @var array $items
     * 
     * @ORM\Column( type="array" ) 
     */
    private $items;

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

    (...)
}

如果我添加元素数组,这code正常工作:

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

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

$ myEntityObject 保存与正确数据的数据库(数组被序列化和查询数据库时反序列化)。

$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);

虽然的print_r 在改变对象的数据code显示器的最后一行,学说不知道什么东西在阵列内改变,如果数组的大小没'T改变。有没有什么办法,迫使学说保存在该领域所做的更改(或者轻轻通知它在这一领域需要被保存的更改)?

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) ?

推荐答案

教义使用相同的运算符(===)比较新旧值之间变化。相同的对象(或对象的阵列)与不同的数据所使用的操作者始终返回true。还有其他的方式来解决这个问题,您可以克隆需要改变的对象。

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);

// ...

或更改 setItems()方法(我们需要克隆只有一个对象坚持整个阵列)

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:

是否有人知道如何preserve默认跟踪政策等领域,并使用NotifyPropertyChanged只是存储阵列领域?

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.

这篇关于如何强制学说更新数组类型的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆