6

I have these entities on my code.

class Review extends BaseEntity {

    /** @ORM\OneToOne(targetEntity="Action", mappedBy="review") */
    protected $action;
}


class Action extends BaseEntity {

    /** @ORM\OneToOne(targetEntity="Review", inversedBy="action") */
    protected $review;
}

As you can see it's a OneToOne relationship between Action and Review. My problem is I use soft-delete for my entities as well, so when I delete an entity is not actually removed only a deletion date is set. Later in audit trail I need to show deleted reviews and I also need information from the parent action of course. My question is, do I need to make this relationship OneToMany? or is there a better approach?

richardalberto
  • 515
  • 2
  • 9
  • 23
  • I think i didn't quite get your question, what does having a relationship OneToMany or OneToOne has to do with soft-deleting? What's the actual issue between one or another? Defining the relationship type depends on what you want to do with those Entities and **NOT MUCH** with the behavior. – Azteca May 13 '15 at 16:26
  • actually it does. If you soft-delete a OneToOne relationship the owning side will still keep a reference to the deleted entity and unless you replace/remove/clear that entity you cannot be associated to a new one. But if you do, then you will loose reference to the owner entity and won't be able to log changes later. – richardalberto May 13 '15 at 17:43
  • Oh, i see, then for what i know (not much) you can do it the way **you** suggested, use a _OneToMany_ relationship and validate to keep only one alive and the rest soft-deleted, so you can have a history for any audition. I'll try to post an answer on the validation. – Azteca May 13 '15 at 22:30
  • Well nvm,my thoughts were with a `/** @preRemove */` event, and while verifying, throw an exception (this rollsback the transaction) but then again, you never want to get it delete... so... sorry mate, my bad :( – Azteca May 13 '15 at 22:48

1 Answers1

0

To be honest i'm not very found of the soft-delete behaviour. What you need to be aware is that soft-deleting an entity is a strong compromise in a relational database.

You may want to consider in this particular instance an event sourcing approach. I would recommend to store the information about the deletion and the (soft)deleted entity in a document based DB.

Any other approach (like OneToMany) is still fine but keep in mind that the risk here is degrading your relation by introducing a incoherent relationship.

That being said I'm fully aware that real life it's quite different than theory :) good luck.

Regards.

Francesco Panina
  • 314
  • 4
  • 16