简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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