Skip to content

Commit

Permalink
fix mapping issue and subscriber event types
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaleta committed Nov 2, 2021
1 parent 10f3d94 commit b6781ab
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
22 changes: 7 additions & 15 deletions EventSubscriber/ReadOnlySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Doctrine\ORM\Event\OnClassMetadataNotFoundEventArgs;
use steevanb\DoctrineReadOnlyHydrator\Hydrator\SimpleObjectHydrator;
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Events;
use steevanb\DoctrineReadOnlyHydrator\Entity\ReadOnlyEntityInterface;
Expand All @@ -25,22 +24,14 @@ public function getSubscribedEvents()
];
}

/**
* @param LifecycleEventArgs $args
* @throws ReadOnlyEntityCantBePersistedException
*/
public function prePersist(LifecycleEventArgs $args)
public function prePersist($args)
{
if ($this->isReadOnlyEntity($args->getObject())) {
throw new ReadOnlyEntityCantBePersistedException($args->getObject());
}
}

/**
* @param PreFlushEventArgs $args
* @throws ReadOnlyEntityCantBeFlushedException
*/
public function preFlush(PreFlushEventArgs $args)
public function preFlush($args)
{
$unitOfWork = $args->getEntityManager()->getUnitOfWork();
$entities = array_merge(
Expand All @@ -55,10 +46,12 @@ public function preFlush(PreFlushEventArgs $args)
}
}

/** @param OnClassMetadataNotFoundEventArgs $eventArgs */
public function onClassMetadataNotFound(OnClassMetadataNotFoundEventArgs $eventArgs)
public function onClassMetadataNotFound($eventArgs)
{
try {
if(empty($eventArgs->getClassName())) {
return;
}
if (class_implements(
$eventArgs->getClassName(),
'steevanb\\DoctrineReadOnlyHydrator\\Entity\\ReadOnlyEntityInterface'
Expand All @@ -70,8 +63,7 @@ public function onClassMetadataNotFound(OnClassMetadataNotFoundEventArgs $eventA
} catch (\Exception $exception) {}
}

/** @param LifecycleEventArgs $eventArgs */
public function postLoad(LifecycleEventArgs $eventArgs)
public function postLoad($eventArgs)
{
if ($eventArgs->getObject() instanceof ReadOnlyEntityInterface) {
// add ReadOnlyProxy to classMetada list
Expand Down
15 changes: 7 additions & 8 deletions Hydrator/SimpleObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ protected function doHydrateRowData($className, array $data)
$classMetaData = $this->_em->getClassMetadata($className);
$mappings = $classMetaData->getAssociationMappings();
$entity = $this->createEntity($classMetaData, $data);
$reflection = new \ReflectionObject($entity);

foreach ($data as $name => $value) {
if (isset($mappings[$name]) && is_array($value)) {
Expand All @@ -95,15 +94,15 @@ protected function doHydrateRowData($className, array $data)

if (
$classMetaData->inheritanceType === ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE
|| $classMetaData->inheritanceType === ClassMetadata::INHERITANCE_TYPE_JOINED
|| $classMetaData->inheritanceType === ClassMetadata::INHERITANCE_TYPE_JOINED
) {
try {
$property = $reflection->getProperty($name);
} catch (\ReflectionException $e) {
continue;
}
try {
$property = $classMetaData->getReflectionClass()->getProperty($name);
} catch (\ReflectionException $e) {
continue;
}
} else {
$property = $reflection->getProperty($name);
$property = $classMetaData->getReflectionClass()->getProperty($name);
}

if ($property->isPublic()) {
Expand Down

0 comments on commit b6781ab

Please sign in to comment.