| 1 | <?php |
||
| 9 | class FlushSubscriber implements EventSubscriber |
||
| 10 | { |
||
| 11 | public $flushed; |
||
| 12 | public $inRequest = false; |
||
| 13 | |||
| 14 | public function preFlush(PreFlushEventArgs $args) |
||
| 15 | { |
||
| 16 | if (!$this->inRequest) { |
||
| 17 | // let console commands handle flushes anyway they want |
||
| 18 | return; |
||
| 19 | } |
||
| 20 | |||
| 21 | $em = $args->getEntityManager(); |
||
| 22 | if ($em->getConnection()->isTransactionActive()) { |
||
| 23 | // the transaction is managed manually and was already started |
||
| 24 | // probably it won't be handled since it is the end of response |
||
| 25 | // but anyways, it won't cause trouble |
||
| 26 | return; |
||
| 27 | } |
||
| 28 | |||
| 29 | if ($this->flushed) { |
||
| 30 | throw new \BadMethodCallException("The flush can be run only once and is run automatically in the end of each request to prevent data inconsistencies and bad design."); |
||
| 31 | } |
||
| 32 | |||
| 33 | $this->flushed = true; |
||
| 34 | } |
||
| 35 | |||
| 36 | public function getSubscribedEvents() |
||
| 40 | } |
||
| 41 |