Command/CleanAssetsCommand.php
protected function findAssociatedEntities(
        EntityManagerInterface $manager,
        $className
    ) {
        /** @var ClassMetadata[] $metadatas */
        $metadatas = $manager->getMetadataFactory()->getAllMetadata();

        $foundEntities = [];
        foreach ($metadatas as $metadata) {
            if ($metadata->getName() === $className) {
                // Collecting all resource entities with associations to other entities
                foreach ($metadata->getAssociationMappings() as $fieldName => $association) {
                    // Check association carried by the Resource side (clearly not recommended)
                    // @todo Please test this code or contact the author:
                    // We never had this case in our data set, we can't be sure it's going to behave like expected
                    $className = $association['sourceEntity'];
                    $metadata = $manager->getClassMetadata($className);
                    /** @var EntityRepository $repository */
                    $repository = $manager->getRepository($className);
                    $qb = $repository
                        ->createQueryBuilder('e')
                        ->select("e.{$metadata->getSingleIdentifierColumnName()} AS id")
                        ->where("e.{$association['fieldName']} IS NOT NULL");

                    foreach ($qb->getQuery()->getArrayResult() as $result) {
                        $value = $result['id'];
                        $foundEntities[$className][$value] = $value;
                    }
                }
            }

            // Collecting all resource entities associated to other entities
            foreach ($metadata->getAssociationsByTargetClass($className) as $fieldName => $association) {
                $className = $association['targetEntity'];
                $metadata = $manager->getClassMetadata($className);
                /** @var EntityRepository $repository */
                $repository = $manager->getRepository($association['sourceEntity']);
                $qb = $repository
                    ->createQueryBuilder('e')
                    ->select("r.{$metadata->getSingleIdentifierColumnName()} AS id")
                    ->innerJoin("e.{$association['fieldName']}", 'r');

                foreach ($qb->getQuery()->getArrayResult() as $result) {
                    $value = $result['id'];
                    $foundEntities[$className][$value] = $value;
                }
            }
        }

        return $foundEntities;
    }
protected function findAssociatedEntities(
        EntityManagerInterface $manager,
        $className
    ) {
        /** @var ClassMetadata[] $metadatas */
        $metadatas = $manager->getMetadataFactory()->getAllMetadata();

        $foundEntities = [];
        foreach ($metadatas as $metadata) {
            if ($metadata->getName() === $className) {
                // Collecting all resource entities with associations to other entities
                foreach ($metadata->getAssociationMappings() as $fieldName => $association) {
                    // Check association carried by the Resource side (clearly not recommended)
                    // @todo Please test this code or contact the author:
                    // We never had this case in our data set, we can't be sure it's going to behave like expected
                    $className = $association['sourceEntity'];
                    $metadata = $manager->getClassMetadata($className);
                    /** @var EntityRepository $repository */
                    $repository = $manager->getRepository($className);
                    $qb = $repository
                        ->createQueryBuilder('e')
                        ->select("e.{$metadata->getSingleIdentifierColumnName()} AS id")
                        ->where("e.{$association['fieldName']} IS NOT NULL");

                    foreach ($qb->getQuery()->getArrayResult() as $result) {
                        $value = $result['id'];
                        $foundEntities[$className][$value] = $value;
                    }
                }
            }

            // Collecting all resource entities associated to other entities
            foreach ($metadata->getAssociationsByTargetClass($className) as $fieldName => $association) {
                $className = $association['targetEntity'];
                $metadata = $manager->getClassMetadata($className);
                /** @var EntityRepository $repository */
                $repository = $manager->getRepository($association['sourceEntity']);
                $qb = $repository
                    ->createQueryBuilder('e')
                    ->select("r.{$metadata->getSingleIdentifierColumnName()} AS id")
                    ->innerJoin("e.{$association['fieldName']}", 'r');

                foreach ($qb->getQuery()->getArrayResult() as $result) {
                    $value = $result['id'];
                    $foundEntities[$className][$value] = $value;
                }
            }
        }

        return $foundEntities;
    }
@param EntityManagerInterface $manager
@param string                 $className

@throws \UnexpectedValueException
@throws \InvalidArgumentException
@throws MappingException
@throws InvalidArgumentException
@throws LogicException
@throws RuntimeException
@throws \BadMethodCallException
@throws \RuntimeException

@return array