Symfony\Bundle\DoctrineBundle\Registry::getEntityManager PHP Method

getEntityManager() public method

Gets a named entity manager.
public getEntityManager ( string $name = null ) : EntityManager
$name string The entity manager name (null for the default one)
return EntityManager
    public function getEntityManager($name = null)
    {
        if (null === $name) {
            $name = $this->defaultEntityManager;
        }

        if (!isset($this->entityManagers[$name])) {
            throw new \InvalidArgumentException(sprintf('Doctrine EntityManager named "%s" does not exist.', $name));
        }

        return $this->container->get($this->entityManagers[$name]);
    }

Usage Example

Beispiel #1
0
 /**
  * @param object $entity
  * @param Constraint $constraint
  * @return bool
  */
 public function isValid($entity, Constraint $constraint)
 {
     if (!is_array($constraint->fields) && !is_string($constraint->fields)) {
         throw new UnexpectedTypeException($constraint->fields, 'array');
     }
     $fields = (array) $constraint->fields;
     if (count($constraint->fields) == 0) {
         throw new ConstraintDefinitionException("At least one field has to specified.");
     }
     $em = $this->registry->getEntityManager($constraint->em);
     $className = $this->context->getCurrentClass();
     $class = $em->getClassMetadata($className);
     $criteria = array();
     foreach ($fields as $fieldName) {
         if (!isset($class->reflFields[$fieldName])) {
             throw new ConstraintDefinitionException("Only field names mapped by Doctrine can be validated for uniqueness.");
         }
         $criteria[$fieldName] = $class->reflFields[$fieldName]->getValue($entity);
     }
     $repository = $em->getRepository($className);
     $result = $repository->findBy($criteria);
     if (count($result) > 0 && $result[0] !== $entity) {
         $oldPath = $this->context->getPropertyPath();
         $this->context->setPropertyPath(empty($oldPath) ? $fields[0] : $oldPath . "." . $fields[0]);
         $this->context->addViolation($constraint->message, array(), $criteria[$constraint->fields[0]]);
         $this->context->setPropertyPath($oldPath);
     }
     return true;
     // all true, we added the violation already!
 }
All Usage Examples Of Symfony\Bundle\DoctrineBundle\Registry::getEntityManager