Author: Fabien Potencier (fabien@symfony.com)
Inheritance: implements Symfony\Bridge\Doctrine\RegistryInterface
Example #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!
 }
Example #2
0
 public function testResetUnknownEntityManager()
 {
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
     $registry = new Registry($container, array(), array(), 'default', 'default');
     $this->setExpectedException('InvalidArgumentException', 'Doctrine EntityManager named "default" does not exist.');
     $registry->resetEntityManager('default');
 }
    /**
     * Process filters
     *
     * @return void
     */
    protected function processFilters()
    {
        if ($this->generator->filter->fields && is_array($this->generator->filter->fields)) {
            $counter = 0;
            foreach ($this->generator->filter->fields as $fieldName => $field) {
                if (isset($field['type'])) {
                    switch($field['type']) {

                        case 'daterange':

                            $dateFormaterEn = \IntlDateFormatter::create('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::NONE);
                            if (isset($this->query[$fieldName .'_from']) && $this->query[$fieldName .'_from'] != '') {
                                $counter++;
                                $this->queryBuilder->andWhere("e.{$fieldName} >= ?{$counter}");
                                $dateFrom = date('Y-m-d', $dateFormaterEn->parse($this->query[$fieldName .'_from'])) . ' 00:00:00';
                                $this->queryBuilder->setParameter($counter, $dateFrom);
                            }
                            if (isset($this->query[$fieldName .'_to']) && $this->query[$fieldName .'_to'] != '') {
                                $counter++;
                                $this->queryBuilder->andWhere("e.{$fieldName} <= ?{$counter}");
                                $dateTo = date('Y-m-d', $dateFormaterEn->parse($this->query[$fieldName .'_to'])) . ' 23:59:59';
                                $this->queryBuilder->setParameter($counter, $dateTo);
                            }
                            break;

                        case 'entity':
                            if (isset($this->query[$fieldName]) && $this->query[$fieldName] != '') {
                                try {
                                    $entity = $this->doctrineRegistry->getEntityManager()
                                            ->getRepository($field['options']['class'])
                                            ->find($this->query[$fieldName]);
                                } catch (Exception $exc) {
                                    $entity =  false;
                                }

                                if ($entity) {
                                    $counter++;
                                    $compare = $this->getCompare(isset($field['compare']) ? $field['compare'] : null);
                                    $this->queryBuilder->andWhere(
                                        $this->queryBuilder->expr()->$compare("e.{$fieldName} ", "?{$counter}")
                                    );
                                    $this->queryBuilder->setParameter($counter, $entity);
                                }

                            }
                            break;

                        case 'checkbox':
                            if (isset($this->query[$fieldName]) && $this->query[$fieldName] == true) {
                                $counter++;
                                $this->queryBuilder->andWhere(
                                    $this->queryBuilder->expr()->eq("e.{$fieldName} ", "?{$counter}")
                                );
                                $this->queryBuilder->setParameter($counter, true);
                            }
                            break;

                        case 'text':
                        default:
                            if (isset($this->query[$fieldName])) {
                                $counter++;
                                $compare = $this->getCompare(isset($field['compare']) ? $field['compare'] : null);
                                $this->queryBuilder->andWhere(
                                    $this->queryBuilder->expr()->$compare("e.{$fieldName} ", "?{$counter}")
                                );
                                if ($compare == 'like') {
                                    $this->query[$fieldName] = "%{$this->query[$fieldName]}%";
                                }
                                $this->queryBuilder->setParameter($counter, $this->query[$fieldName]);
                            }
                            break;
                    }
                }
            }
        }
    }
Example #4
0
 public function __construct(Registry $registry, DbalLogger $logger = null)
 {
     $this->connections = $registry->getConnectionNames();
     $this->managers = $registry->getEntityManagerNames();
     $this->logger = $logger;
 }
Example #5
0
 public function __construct(SecurityContext $context, Doctrine $doctrine)
 {
     $this->context = $context;
     $this->em = $doctrine->getEntityManager();
 }