0

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?

Tom
  • 1,547
  • 7
  • 27
  • 50

1 Answers1

0

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

Community
  • 1
  • 1
DonCallisto
  • 29,419
  • 9
  • 72
  • 100
  • It is only an one-to-one one direction, see my added info. Is there not an easier way as an listener as I want to know this in an controller? – Tom Sep 29 '16 at 11:37