douliang1900 2016-09-28 08:40
浏览 28

在以一对一关系验证Symfony表单后检测更改

I use an Symfony product form. That can be changed by an user. After the user saves the form I want to know if there are any changes made in the product.

The form uses the entity of the product. This product has an one-to-one relation with the entity price. As there are multiple one-to-one relation with price for discounts and so on. The price entity has an set of data like priceType en currency and value.

It is an one-to-one one direction, only from product to price. The product entity has the following annotation:

 /**
 * @ORM\OneToOne(targetEntity="Price")
 * @ORM\JoinColumn(name="price_sales_id", referencedColumnName="id")
 */
private $priceSales;

After the form is saved and validated I use the following code to compare:

$uow = $em->getUnitOfWork();
$uow->computeChangeSets();
$changeSet = $uow->getEntityChangeSet($product);

The $changeSet object gives back the changes made in the product but not the one made in the one-to-one relation. Is there a way to also detect changed in the related entities?

  • 写回答

1条回答 默认 最新

  • dougu7546 2016-09-28 09:34
    关注

    First of all, never use computeChangeSets() as you are going to alter UnitOfWork and this may cause big troubles.

    Second, I'm pretty sure that Product is the inversedSide of your relationship with Price (you can verify it checking annotation: if you have mappedBy into Product's price annotation, than you are into inversedSide).

    Doctrine looks only for changes at owningSide (inversedBy annotation) of a relationship.

    Once you have switched these sides you can computeChangeSets (which I really recommend to do not) or do whatever you need to do.

    The best strategy to me (that not involves switching the sides of relation, even if you should stop and rethink if the sides are correct) is to implement a Doctrine entity listener and to listen on preUpdate event

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入