/**
  * Phase association persistence method.
  *
  * @param ResourceEvent $event
  */
 public function persistPhaseAssociation(ResourceEvent $event)
 {
     $om = $event['objectManager'];
     $subject = $event->getSubject();
     // If subject doesn't have phases, don't attempt.
     if (!method_exists($subject, 'getPhases')) {
         return;
     }
     $om->transactional(function ($om) use($subject) {
         $phases = $subject->getPhases()->toArray();
         // Remove all phases BEFORE persisting subject.
         foreach ($phases as $phase) {
             $subject->removePhase($phase);
         }
         $om->persist($subject);
         $om->flush();
         // Add phases back into subject.
         foreach ($phases as $phase) {
             $subject->addPhase($phase);
             $om->persist($phase);
         }
         $om->persist($subject);
         $om->flush();
     });
 }
 /**
  * Pre delete diagnosis.
  *
  * Stops a diagnosis from being deleted if it contains second level data.
  *
  * @param ResourceEvent $event
  */
 public function preDeleteDiagnosis(ResourceEvent $event)
 {
     $diagnosis = $event->getSubject();
     $hasActivities = (bool) count($diagnosis->getActivities());
     $hasRegimens = (bool) count($diagnosis->getRegimens());
     if ($hasActivities || $hasRegimens) {
         $entities = array();
         $hasActivities && ($entities[] = 'activities');
         $hasRegimens && ($entities[] = 'regimens');
         $string = $this->createEntityString($entities);
         $event->stop('accard.flashes.no_delete', 'warning', array('%entities%' => $string));
     }
 }