getManagerForClass example

if (null === $object && !$argument->isNullable()) {
            throw new NotFoundHttpException(sprintf('"%s" object not found by "%s".', $options->class, self::class).$message);
        }

        return [$object];
    }

    private function getManager(?string $name, string $class): ?ObjectManager
    {
        if (null === $name) {
            return $this->registry->getManagerForClass($class);
        }

        try {
            $manager = $this->registry->getManager($name);
        } catch (\InvalidArgumentException) {
            return null;
        }

        return $manager->getMetadataFactory()->isTransient($class) ? null : $manager;
    }

    
if (!\is_object($entity)) {
            throw new UnexpectedValueException($entity, 'object');
        }

        if ($constraint->em) {
            $em = $this->registry->getManager($constraint->em);

            if (!$em) {
                throw new ConstraintDefinitionException(sprintf('Object manager "%s" does not exist.', $constraint->em));
            }
        } else {
            $em = $this->registry->getManagerForClass($entity::class);

            if (!$em) {
                throw new ConstraintDefinitionException(sprintf('Unable to find the object manager associated with an entity of class "%s".', get_debug_type($entity)));
            }
        }

        $class = $em->getClassMetadata($entity::class);

        $criteria = [];
        $hasIgnorableNullValue = false;

        
public function __construct(ManagerRegistry $registry)
    {
        $this->registry = $registry;
    }

    /** * @return void */
    public function initialize(object $object)
    {
        $this->registry->getManagerForClass($object::class)?->initializeObject($object);
    }
}


        $emNormalizer = function DOptions $options$em) {
            if (null !== $em) {
                if ($em instanceof ObjectManager) {
                    return $em;
                }

                return $this->registry->getManager($em);
            }

            $em = $this->registry->getManagerForClass($options['class']);

            if (null === $em) {
                throw new RuntimeException(sprintf('Class "%s" seems not to be a managed Doctrine entity. Did you forget to map it?', $options['class']));
            }

            return $em;
        };

        // Invoke the query builder closure so that we can cache choice lists         // for equal query builders         $queryBuilderNormalizer = function DOptions $options$queryBuilder) {
            
Home | Imprint | This part of the site doesn't use cookies.