Licitator 1.0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.8 KiB

5 years ago
  1. <?php
  2. namespace Zitec\RuleEngineBundle\DoctrineBehaviors;
  3. use Doctrine\Common\EventSubscriber;
  4. use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
  5. use Doctrine\ORM\Events;
  6. use Doctrine\ORM\Mapping\ClassMetadata;
  7. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  8. use Zitec\RuleEngineBundle\Entity\Rule;
  9. /**
  10. * Class RuleSubscriber
  11. * Doctrine event listener service that adds required fields to entities using RuleTrait.
  12. */
  13. class RuleSubscriber implements EventSubscriber
  14. {
  15. /**
  16. * @return array
  17. */
  18. public function getSubscribedEvents()
  19. {
  20. return [Events::loadClassMetadata];
  21. }
  22. /**
  23. * Adds a oneToOne association between the Rule entity and the entity using the RuleTrait.
  24. *
  25. * @param LoadClassMetadataEventArgs $eventArgs
  26. */
  27. public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
  28. {
  29. /* @var $classMetadata ClassMetadata */
  30. $classMetadata = $eventArgs->getClassMetadata();
  31. if (null === $classMetadata->reflClass) {
  32. return;
  33. }
  34. if (in_array(RuleTrait::class, $classMetadata->reflClass->getTraitNames())) {
  35. if (!$classMetadata->hasField('rule')) {
  36. $classMetadata->mapOneToOne(
  37. [
  38. 'targetEntity' => Rule::class,
  39. 'fetch' => ClassMetadataInfo::FETCH_EAGER,
  40. 'fieldName' => 'rule',
  41. 'cascade' => ['persist', 'remove'],
  42. 'orphanRemoval' => true,
  43. 'joinColumn' => [
  44. 'name' => 'rule_id',
  45. 'referencedColumnName' => 'id',
  46. ],
  47. ]
  48. );
  49. }
  50. }
  51. }
  52. }