简体   繁体   English

在验证具有一对一关系的Symfony表单后检测更改

[英]Detecting an change after the validation of an Symfony form with an one-to-one relation

I use an Symfony product form. 我使用Symfony产品表格。 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 . price实体具有一组数据,例如priceType en currencyvalue

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. $changeSet对象返回在产品中所做的更改,但不返回在一对一关系中所做的更改。 Is there a way to also detect changed in the related entities? 有没有办法检测相关实体中的更改?

First of all, never use computeChangeSets() as you are going to alter UnitOfWork and this may cause big troubles. 首先, 切勿使用computeChangeSets()因为您将要更改UnitOfWork ,这可能会造成很大的麻烦。

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 ). 其次,我非常确定Product是您与Price关系的inversedSide (您可以通过检查批注来验证它:如果已将mappedBy inversedSideProductprice批注,则您已将其inversedSideinversedSide )。

Doctrine looks only for changes at owningSide ( inversedBy annotation) of a relationship. 原则看上去只有在变化owningSideinversedBy注释)的关系。

Once you have switched these sides you can computeChangeSets (which I really recommend to do not) or do whatever you need to do. 一旦切换了这些方面,您就可以计算computeChangeSets (我真的建议不要这样做)或做您需要做的任何事情。

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 对我而言,最好的策略(这不涉及切换关系的边,即使您应该停下来再考虑是否正确),也应该实现一个Doctrine实体侦听器并侦听preUpdate事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM