public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) { $classMetadata = $eventArgs->getClassMetadata(); $reflectionClass = $classMetadata->getReflectionClass(); if (null === $reflectionClass) { return; } if (ClassUtil::classUsesTrait($reflectionClass->getName(), __NAMESPACE__ . '\\Translatable')) { if (!$classMetadata->hasAssociation('translations')) { $classMetadata->mapOneToMany(['fieldName' => 'translations', 'mappedBy' => 'translatable', 'indexBy' => 'locale', 'cascade' => ['persist', 'merge', 'remove'], 'orphanRemoval' => true, 'targetEntity' => $classMetadata->getName() . 'Translation']); } } if (ClassUtil::classUsesTrait($reflectionClass->getName(), __NAMESPACE__ . '\\Translation')) { if (!$classMetadata->hasAssociation('translatable')) { $classMetadata->mapManyToOne(['fieldName' => 'translatable', 'inversedBy' => 'translations', 'joinColumns' => [['name' => 'translatable_id', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE']], 'targetEntity' => substr($classMetadata->getName(), 0, -11)]); } if (!$classMetadata->hasField('locale')) { $classMetadata->mapField(['fieldName' => 'locale', 'type' => 'string']); } $translationConstraint = $classMetadata->getTableName() . '_unique_translation'; if (!ClassMetadataUtil::hasUniqueConstraint($classMetadata, $translationConstraint)) { $classMetadata->setPrimaryTable(['uniqueConstraints' => [['name' => $translationConstraint, 'columns' => ['translatable_id', 'locale']]]]); } } }
public function onFlush(OnFlushEventArgs $args) { $em = $args->getEntityManager(); $uow = $em->getUnitOfWork(); foreach ($uow->getScheduledEntityDeletions() as $entity) { $classMetadata = $em->getClassMetadata(get_class($entity)); $reflectionClass = $classMetadata->getReflectionClass(); if (ClassUtil::classUsesTrait($reflectionClass->getName(), __NAMESPACE__ . "\\SoftDeletable")) { $oldValue = $entity->getDeletedAt(); $entity->setDeletedAt(new \DateTime()); $em->persist($entity); $uow->propertyChanged($entity, 'deletedAt', $oldValue, $entity->getDeletedAt()); $uow->scheduleExtraUpdate($entity, ['deletedAt' => [$oldValue, $entity->getDeletedAt()]]); } } }
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) { $classMetadata = $eventArgs->getClassMetadata(); $reflectionClass = $classMetadata->getReflectionClass(); if (null === $reflectionClass) { return; } if (ClassUtil::classUsesTrait($reflectionClass->getName(), __NAMESPACE__ . "\\Timestampable")) { if (!$classMetadata->hasField('createdAt') && !$classMetadata->hasField('updatedAt')) { $classMetadata->mapField(['fieldName' => 'createdAt', 'type' => 'datetime_utc', 'nullable' => true]); $classMetadata->mapField(['fieldName' => 'updatedAt', 'type' => 'datetime_utc', 'nullable' => true]); $classMetadata->addLifecycleCallback('updateTimestampableFields', Events::prePersist); $classMetadata->addLifecycleCallback('updateTimestampableFields', Events::preUpdate); } } }