0

The scenario is quite simple to explain with an example:

public function setValue($value)
{
    if ($value == 0) {
          // DELETE ME
    }

    return $this;
}

I'm probably going to use a workaround (i.e.: deleting the object after the call to setValue, but I'd like to encapsulate this business logic within the method)

ᴀʀᴍᴀɴ
  • 4,443
  • 8
  • 37
  • 57
Muc
  • 1,464
  • 2
  • 13
  • 31

1 Answers1

2

To achieve this you'd probably require access to the EntityManager which you don't have within an entity (and for good reason, just as one of many examples see this example of how to get the Manager within an entity anway - it's VERY BAD PRACTISE).

So I suggest to follow your 'workaround' - to keep it away from other stuff I suggest to use the Doctrine event system - but it's depending on your personal flavor.

Community
  • 1
  • 1
LBA
  • 3,859
  • 2
  • 21
  • 60
  • Yeah... I wouldn't want to go the ugly way (the bad practice you mention)... It'd be nice to have somewhat of a "to be deleted" mark on the object itself which could be set from within though... – Muc Feb 18 '17 at 01:33
  • this can be easily achieved with a listener, check my link to the event system – LBA Feb 20 '17 at 09:57