原则部分合并实体

时间:2018-11-17 18:34:25

标签: doctrine

我在教义方面遇到一个小问题。

当用户更新条目时,我将创建一个与现有ID具有相同ID的新对象,并合并它们以更新后者。它有效,但事实是,当我创建一个新对象时,在构造函数中,我将“ date_created”的值设置为当前时间戳。

因此,当我合并新实体时,它还会更新“ date_created”字段,该字段应保持不变。有没有一种方法可以在忽略“ date_created”字段的同时进行合并,还是应该只进行常规更新查询?

先谢谢您

1 个答案:

答案 0 :(得分:0)

这是一些代码:

控制器

// Validation of user inputs

// New object with the ID of the existing one
         $newFetcher = new Fetcher(
                    $request->input('id'),
                    $request->input('name'),
                    $request->input('vendorOID'),
                    $request->input('productOID'),
                    $request->input('versionOID'),
                    $request->input('updateOID')
          );
// Calling repository to merge entities
$updatedFetcher = $this->q->update($newFetcher);

存储库

public function update(Fetcher $fetcher)
    {
        $this->_em->merge($fetcher);
        $this->_em->flush();
        return $fetcher;
    }

实体类的示例

public function __construct($id = 1, $name = "", $vendor_oid = null, $product_oid = null, $version_oid = null, $update_oid = null)
    {
        $this->id = $id;
        $this->name = $name;
        $this->vendor_oid = $vendor_oid;
        $this->product_oid = $product_oid;
        $this->version_oid = $version_oid;
        $this->update_oid = $update_oid;
        parent::setDateCreated();
        parent::setTimestamp();
    }

注意:我的所有实体都从超类扩展为我不想为每个表声明的通用字段,例如ID,时间戳,date_created。

setTimestamp和setdatecreated将最后更新日期和创建日期设置为当前时间戳:

超类

public function setTimestamp()
    {
        $this->timestamp = $this->getDateTime();
    }
public function setDateCreated()
    {
        $this->date_created = $this->getDateTime();
    }

public function getDateTime($tz = TIMEZONE)
    {
        return new DateTime("now", new DateTimeZone($tz));
    }
相关问题