src/Entity/Factory/EntityFactory.php
private function getNewInstance(string $entityFqn, $id): EntityInterface
    {
        if (isset(self::$created[$entityFqn][(string)$id])) {
            throw new \RuntimeException('Trying to get a new instance when one has already been created for this ID');
        }
        $reflection = $this->getDoctrineStaticMetaForEntityFqn($entityFqn)
                           ->getReflectionClass();
        $entity     = $reflection->newInstanceWithoutConstructor();

        $runInit = $reflection->getMethod(UsesPHPMetaDataInterface::METHOD_RUN_INIT);
        $runInit->setAccessible(true);
        $runInit->invoke($entity);

        $transactionProperty = $reflection->getProperty(AlwaysValidInterface::CREATION_TRANSACTION_RUNNING_PROPERTY);
        $transactionProperty->setAccessible(true);
        $transactionProperty->setValue($entity, true);

        $idSetter = $reflection->getMethod('set' . IdFieldInterface::PROP_ID);
        $idSetter->setAccessible(true);
        $idSetter->invoke($entity, $id);

        if ($entity instanceof EntityInterface) {
            $this->initialiseEntity($entity);

            $this->entityManager->persist($entity);

            return $entity;
        }
        throw new \LogicException('Failed to create an instance of EntityInterface');
    }
private function getNewInstance(string $entityFqn, $id): EntityInterface
    {
        if (isset(self::$created[$entityFqn][(string)$id])) {
            throw new \RuntimeException('Trying to get a new instance when one has already been created for this ID');
        }
        $reflection = $this->getDoctrineStaticMetaForEntityFqn($entityFqn)
                           ->getReflectionClass();
        $entity     = $reflection->newInstanceWithoutConstructor();

        $runInit = $reflection->getMethod(UsesPHPMetaDataInterface::METHOD_RUN_INIT);
        $runInit->setAccessible(true);
        $runInit->invoke($entity);

        $transactionProperty = $reflection->getProperty(AlwaysValidInterface::CREATION_TRANSACTION_RUNNING_PROPERTY);
        $transactionProperty->setAccessible(true);
        $transactionProperty->setValue($entity, true);

        $idSetter = $reflection->getMethod('set' . IdFieldInterface::PROP_ID);
        $idSetter->setAccessible(true);
        $idSetter->invoke($entity, $id);

        if ($entity instanceof EntityInterface) {
            $this->initialiseEntity($entity);

            $this->entityManager->persist($entity);

            return $entity;
        }
        throw new \LogicException('Failed to create an instance of EntityInterface');
    }
private function getNewInstance ( string $ entityFqn , $ id ) : EntityInterface { if ( isset ( self :: $ created [ $ entityFqn ] [ ( string ) $ id ] ) ) { throw new \ RuntimeException ( 'Trying to get a new instance when one has already been created for this ID' ) ; } $ reflection = $ this -> getDoctrineStaticMetaForEntityFqn ( $ entityFqn ) -> getReflectionClass ( ) ; $ entity = $ reflection -> newInstanceWithoutConstructor ( ) ; $ runInit = $ reflection -> getMethod ( UsesPHPMetaDataInterface :: METHOD_RUN_INIT ) ; $ runInit -> setAccessible ( true ) ; $ runInit -> invoke ( $ entity ) ; $ transactionProperty = $ reflection -> getProperty ( AlwaysValidInterface :: CREATION_TRANSACTION_RUNNING_PROPERTY ) ; $ transactionProperty -> setAccessible ( true ) ; $ transactionProperty -> setValue ( $ entity , true ) ; $ idSetter = $ reflection -> getMethod ( 'set' . IdFieldInterface :: PROP_ID ) ; $ idSetter -> setAccessible ( true ) ; $ idSetter -> invoke ( $ entity , $ id ) ; if ( $ entity instanceof EntityInterface ) { $ this -> initialiseEntity ( $ entity ) ; $ this -> entityManager -> persist ( $ entity ) ; return $ entity ; } throw new \ LogicException ( 'Failed to create an instance of EntityInterface' ) ; }
Build a new instance, bypassing PPP protections so that we can call private methods and set the private
transaction property

@param string $entityFqn
@param mixed  $id

@return EntityInterface
Build a new instance bypassing PPP protections so that we can call private methods and set the private transaction property