예제 #1
0
 /**
  * Checks if file is uploaded.
  *
  * @param mixed  $object
  * @param string $field
  *
  * @return bool|string
  */
 public function is_uploaded($object, $field)
 {
     if ($object === null) {
         return false;
     }
     try {
         $this->em->initializeObject($object);
         return $this->uploader->asset($object, $field);
     } catch (InvalidArgumentException $e) {
         return false;
     }
 }
 /**
  * @param \Scribe\Doctrine\ORM\Mapping\Entity|mixed $entity
  * @param mixed                                     $index
  *
  * @throws \Exception
  *
  * @return $this
  */
 protected function loadAndMergeEntity($index, $entity)
 {
     try {
         $entityMetadata = $this->getClassMetadata($this->getEntityFQCN());
         $this->identLog[] = $identity = $entityMetadata->getIdentifierValues($entity);
         if (count($identity) > 0) {
             $identity = [key($identity) => current($identity)];
         } elseif (!$entity->hasIdentity()) {
             OutBuffer::stat('+y/b-preload +y/i-[warns]+w/- import could not begin for "%s:%d"', basename($this->metadata->getName()), $index);
             OutBuffer::stat('+y/b-preload +y/i-[warns]+w/- import strategy "merge" unavailable due to failed identifier map resolution');
         }
         $repository = $this->manager->getRepository($this->getEntityFQCN());
         $entitySearched = $repository->findOneBy($identity);
         $this->manager->initializeObject($entitySearched);
         if ($entitySearched && !$entity->isEqualTo($entitySearched)) {
             $mapper = new HydratorManager(new HydratorMapping(true));
             $entity = $mapper->getMappedObject($entity, $entitySearched);
             $this->manager->remove($entitySearched);
             $this->manager->merge($entity);
             $this->manager->persist($entity);
             $this->manager->flush();
             $this->manager->clear();
             ++$this->countUpdate;
             return $this;
         } elseif ($entitySearched && $entity->isEqualTo($entitySearched)) {
             $entity = $entitySearched;
             ++$this->countSkip;
             return $this;
         }
         $this->loadAndPersistEntity($entity);
     } catch (\Exception $e) {
         throw $e;
     }
     return $this;
 }
 /**
  * {@inheritdoc}
  */
 public function validate($entity, Constraint $constraint)
 {
     $parent = $entity->getParent();
     $this->entity = $entity;
     $this->parent = $parent;
     if ($parent) {
         $criticalError = false;
         try {
             $this->entityManager->initializeObject($parent);
         } catch (EntityNotFoundException $exception) {
             $this->addViolationAt($parent->getPrimaryKey(), $this->entity->getPrimaryKey(), null, 'Parent #%s of object #%s does not exist');
             $criticalError = true;
         }
         if ($criticalError === false) {
             $this->validateParent($entity, $parent);
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function validate($entity, Constraint $constraint)
 {
     if (!$constraint instanceof UniqueEntity) {
         throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\UniqueEntity');
     }
     $criticalError = false;
     $class = $this->entityManager->getClassMetadata(get_class($entity));
     foreach ($constraint->fields as $fieldName) {
         $object = $class->reflFields[$fieldName]->getValue($entity);
         try {
             $this->entityManager->initializeObject($object);
         } catch (EntityNotFoundException $exception) {
             $this->context->addViolationAt($fieldName, $fieldName . ' does not exist');
             $criticalError = true;
         }
     }
     if ($criticalError === false) {
         parent::validate($entity, $constraint);
     }
 }
예제 #5
0
 /**
  * {@inheritDoc}
  *
  * @static 
  */
 public static function initializeObject($obj)
 {
     return \Doctrine\ORM\EntityManager::initializeObject($obj);
 }
 /**
  * (StartDate1 <= EndDate2) and (StartDate2 <= EndDate1)
  * 
  * @param object $entity
  * @param \Symfony\Component\Validator\Constraint $constraint
  */
 public function validate($entity, Constraint $constraint)
 {
     if (!$constraint instanceof DateTimeIntersect) {
         throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\DateTimeIntersect');
     }
     $class = $this->em->getClassMetadata(get_class($entity));
     /*@var $class \Doctrine\Common\Persistence\Mapping\ClassMetadata*/
     $errorPath = $this->getErrorPaths($constraint);
     foreach ($errorPath as $fieldName) {
         if (!$class->hasField($fieldName) && !is_null($fieldName)) {
             throw new ConstraintDefinitionException(sprintf("The field '%s' is not mapped by Doctrine.", $fieldName));
         }
     }
     $fields = $this->getFields($constraint);
     switch (false) {
         case $class->hasField($constraint->startDateField):
             throw new ConstraintDefinitionException(sprintf('There isn`t field %s in entity %s', $constraint->startDateField, get_class($entity)));
         case $class->hasField($constraint->endDateField):
             throw new ConstraintDefinitionException(sprintf('There isn`t field %s in entity %s', $constraint->endDateField, get_class($entity)));
     }
     $startDate = $class->getFieldValue($entity, $constraint->startDateField);
     $endDate = $class->getFieldValue($entity, $constraint->endDateField);
     if (null === $startDate || '' === $startDate || null === $endDate || '' === $endDate) {
         return;
     }
     if (!$startDate instanceof \DateTime) {
         throw new UnexpectedTypeException($entity->getStartDate(), '\\DateTime');
     }
     if (!$startDate instanceof \DateTime) {
         throw new UnexpectedTypeException($entity->getStartDate(), '\\DateTime');
     }
     if (!$endDate instanceof \DateTime) {
         throw new UnexpectedTypeException($endDate, '\\DateTime');
     }
     $criteria = array();
     foreach ($fields as $fieldName) {
         if (!$class->hasField($fieldName) && !$class->hasAssociation($fieldName)) {
             throw new ConstraintDefinitionException(sprintf("The field '%s' is not mapped by Doctrine.", $fieldName));
         }
         $criteria[$fieldName] = $class->reflFields[$fieldName]->getValue($entity);
         if (!is_null($criteria[$fieldName]) && $class->hasAssociation($fieldName)) {
             $this->em->initializeObject($criteria[$fieldName]);
             $relatedClass = $this->em->getClassMetadata($class->getAssociationTargetClass($fieldName));
             $relatedId = $relatedClass->getIdentifierValues($criteria[$fieldName]);
             if (count($relatedId) > 1) {
                 throw new ConstraintDefinitionException("Associated entities are not allowed to have more than one identifier");
             }
             $criteria[$fieldName] = array_pop($relatedId);
         }
     }
     $queryBuilder = $this->em->getRepository(get_class($entity))->createQueryBuilder('dti')->where("dti.{$constraint->startDateField} <= :startDate AND dti.{$constraint->endDateField} >= :startDate")->orWhere("dti.{$constraint->startDateField} <= :endDate AND dti.{$constraint->endDateField} >= :endDate")->setParameters(array('startDate' => $class->getFieldValue($entity, $constraint->startDateField), 'endDate' => $class->getFieldValue($entity, $constraint->endDateField)));
     foreach ($criteria as $field => $value) {
         $queryBuilder->andWhere("dti.{$field} = :{$field}")->setParameter($field, $value);
     }
     $result = $queryBuilder->getQuery()->getResult();
     $result instanceof \Iterator ? $result->rewind() : reset($result);
     if (count($result) === 0 || count($result) === 1 && $entity === ($result instanceof \Iterator ? $result->current() : current($result))) {
         return;
     }
     $names = array();
     foreach ($result as $range) {
         if ($range !== $entity) {
             $names[] = "{$range->getStartDate()->format('Y-m-d')} - {$range->getEndDate()->format('Y-m-d')}";
         }
     }
     if (!empty($names)) {
         foreach ($errorPath as $errorPathItem) {
             $this->buildViolation(sprintf($constraint->message))->setParameter('{{ intersects }}', implode(', ', $names))->atPath($errorPathItem)->setInvalidValue($criteria[$fields[0]])->addViolation();
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function initializeObject($obj)
 {
     return $this->wrapped->initializeObject($obj);
 }