public function testColumnWithMissingTypeDefaultsToString()
 {
     $cm = new ClassMetadata(ColumnWithoutType::class);
     $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService());
     $annotationDriver = $this->_loadDriver();
     $annotationDriver->loadMetadataForClass('Doctrine\\Tests\\ORM\\Mapping\\InvalidColumn', $cm);
     $this->assertEquals('string', $cm->fieldMappings['id']['type']);
 }
 /**
  * @param ClassMetadata $targetClass
  * @param array         $assoc
  * @param mixed         $actualValue
  *
  * @return self
  */
 public static function invalidAssociation(ClassMetadata $targetClass, $assoc, $actualValue)
 {
     $expectedType = 'Doctrine\\Common\\Collections\\Collection|array';
     if (($assoc['association'] & ClassMetadata::TO_ONE) > 0) {
         $expectedType = $targetClass->getName();
     }
     return new self(sprintf('Expected value of type "%s" for association field "%s#$%s", got "%s" instead.', $expectedType, $assoc['sourceDoc'], $assoc['fieldName'], is_object($actualValue) ? get_class($actualValue) : gettype($actualValue)));
 }
 /**
  * Lookup the document class to find methods that match to event lifecycle names
  *
  * @param ClassMetadata $metadata  The document metadata.
  * @param string        $className The listener class name.
  *
  * @throws MappingException           When the listener class not found.
  */
 public static function bindDocumentListener(ClassMetadata $metadata, $className)
 {
     $class = $metadata->fullyQualifiedClassName($className);
     if (!class_exists($class)) {
         throw MappingException::documentListenerClassNotFound($class, $className);
     }
     foreach (get_class_methods($class) as $method) {
         if (!isset(self::$events[$method])) {
             continue;
         }
         $metadata->addDocumentListener($method, $class, $method);
     }
 }
Exemplo n.º 4
0
 /**
  * Generates a closure capable of initializing a proxy
  *
  * @param ClassMetadata       $class
  * @param DocumentPersister   $documentPersister
  * @param \ReflectionProperty $reflectionId
  *
  * @return callable
  */
 private function createInitializer(ClassMetadata $class, DocumentPersister $documentPersister, \ReflectionProperty $reflectionId)
 {
     if ($class->getReflectionClass()->hasMethod('__wakeup')) {
         return function (BaseProxy $proxy) use($reflectionId, $documentPersister) {
             $proxy->__setInitializer(null);
             $proxy->__setCloner(null);
             if ($proxy->__isInitialized()) {
                 return;
             }
             $properties = $proxy->__getLazyProperties();
             foreach ($properties as $propertyName => $property) {
                 if (!isset($proxy->{$propertyName})) {
                     $proxy->{$propertyName} = $properties[$propertyName];
                 }
             }
             $proxy->__setInitialized(true);
             $proxy->__wakeup();
             $rid = $reflectionId->getValue($proxy);
             if ($documentPersister->load($rid, '*:0', $proxy) === null) {
                 throw DocumentNotFoundException::documentNotFound(get_class($proxy), $rid);
             }
         };
     }
     return function (BaseProxy $proxy) use($reflectionId, $documentPersister) {
         $proxy->__setInitializer(null);
         $proxy->__setCloner(null);
         if ($proxy->__isInitialized()) {
             return;
         }
         $properties = $proxy->__getLazyProperties();
         foreach ($properties as $propertyName => $property) {
             if (!isset($proxy->{$propertyName})) {
                 $proxy->{$propertyName} = $properties[$propertyName];
             }
         }
         $proxy->__setInitialized(true);
         $rid = $reflectionId->getValue($proxy);
         if ($documentPersister->load($rid, '*:0', $proxy) === null) {
             throw DocumentNotFoundException::documentNotFound(get_class($proxy), $rid);
         }
     };
 }
Exemplo n.º 5
0
 /**
  * Detects instances of ClassMetadata that don't need to be processed in the SchemaTool context.
  *
  * @param ClassMetadata $class
  * @param array         $processedClasses
  *
  * @return bool
  */
 private function processingNotRequired($class, array $processedClasses)
 {
     return isset($processedClasses[$class->name]) || $class->isMappedSuperclass();
 }
Exemplo n.º 6
0
 /**
  * @param string            $className
  * @param ClassMetadata     $metadata
  * @param DocumentListeners $annot
  *
  * @throws MappingException
  */
 private function mapDocumentListeners($className, ClassMetadata $metadata, DocumentListeners $annot)
 {
     foreach ($annot->value as $item) {
         $listenerClassName = $metadata->fullyQualifiedClassName($item);
         if (!class_exists($listenerClassName)) {
             throw MappingException::documentListenerClassNotFound($listenerClassName, $className);
         }
         $hasMapping = false;
         $listenerClass = new \ReflectionClass($listenerClassName);
         /* @var $method \ReflectionMethod */
         foreach ($listenerClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
             // find method callbacks.
             $callbacks = $this->getMethodCallbacks($method);
             $hasMapping = $hasMapping ?: !empty($callbacks);
             foreach ($callbacks as $value) {
                 $metadata->addDocumentListener($value[1], $listenerClassName, $value[0]);
             }
         }
         // Evaluate the listener using naming convention.
         if (!$hasMapping) {
             DocumentListenerBuilder::bindDocumentListener($metadata, $listenerClassName);
         }
     }
 }
 /**
  * @depends can_set_isMappedSuperclass
  * @test
  */
 function can_clear_isMappedSuperclass()
 {
     $this->metadata->clearIsMappedSuperclass();
     $this->assertFalse($this->metadata->isMappedSuperclass());
     $this->assertEquals(0, $this->metadata->attributes & ClassMetadata::CA_TYPE_MAPPED_SUPERCLASS);
 }
 /**
  * Prepares the array that is ready to be inserted to mongodb for a given object document.
  *
  * @param ClassMetadata $class
  * @param UnitOfWork    $uow
  * @param object        $document
  *
  * @return \stdClass $insertData
  * @throws ODMOrientDbException
  */
 public function prepareData(ClassMetadata $class, UnitOfWork $uow, $document)
 {
     $insertData = new \stdClass();
     if ($class->isEmbeddedDocument()) {
         $insertData->{'@type'} = 'd';
         $insertData->{'@class'} = $class->getOrientClass();
         $cs = $uow->getDocumentActualData($document);
     } else {
         $cs = $uow->getDocumentChangeSet($document);
         array_Walk($cs, function (&$val) {
             $val = $val[1];
         });
     }
     $mappings =& $class->fieldMappings;
     foreach ($cs as $name => $new) {
         $mapping = isset($mappings[$name]) ? $mappings[$name] : null;
         if ($mapping === null) {
             // don't store arbitrary values for now
             continue;
         }
         // Don't store null values unless nullable === true
         if ($new === null && $mapping['nullable'] === false) {
             continue;
         }
         $value = null;
         if ($new !== null) {
             switch (true) {
                 // @Property
                 case !isset($mapping['association']):
                     $value = Type::getType($mapping['type'])->convertToDatabaseValue($new);
                     break;
                 case $mapping['association'] & ClassMetadata::LINK:
                     $value = $this->getDocReference($new);
                     break;
                 case $mapping['association'] & ClassMetadata::LINK_MANY:
                     // initialize the link collection
                     if ($mapping['association'] & ClassMetadata::LINK_MAP) {
                         $value = new \stdClass();
                     } else {
                         $value = [];
                     }
                     break;
                 case $mapping['association'] & ClassMetadata::EMBED:
                     /** @var ClassMetadata $rmd */
                     $rmd = $this->metadataFactory->getMetadataFor(get_class($new));
                     $value = $this->prepareData($rmd, $uow, $new);
                     break;
                 case $mapping['association'] & ClassMetadata::EMBED_MANY:
                     $value = [];
                     if ($mapping['association'] & ClassMetadata::EMBED_MAP) {
                         foreach ($new as $k => $item) {
                             /** @var ClassMetadata $rmd */
                             $rmd = $this->metadataFactory->getMetadataFor(get_class($item));
                             $value[$k] = $this->prepareData($rmd, $uow, $item);
                         }
                     } else {
                         foreach ($new as $k => $item) {
                             /** @var ClassMetadata $rmd */
                             $rmd = $this->metadataFactory->getMetadataFor(get_class($item));
                             $value[] = $this->prepareData($rmd, $uow, $item);
                         }
                     }
                     break;
             }
         }
         $insertData->{$mapping['name']} = $value;
     }
     return $insertData;
 }
 /**
  * Adds inherited fields to the subclass mapping.
  *
  * @param ClassMetadata $subClass
  * @param ClassMetadata $parentClass
  */
 private function addInheritedFields(ClassMetadata $subClass, ClassMetadata $parentClass)
 {
     foreach ($parentClass->fieldMappings as $fieldName => $mapping) {
         if (!isset($mapping['inherited']) && !$parentClass->isMappedSuperclass()) {
             $mapping['inherited'] = $parentClass->name;
         }
         if (!isset($mapping['declared'])) {
             $mapping['declared'] = $parentClass->name;
         }
         $subClass->addInheritedFieldMapping($mapping);
     }
     foreach ($parentClass->reflFields as $name => $field) {
         $subClass->reflFields[$name] = $field;
     }
 }
Exemplo n.º 10
0
 /**
  * @param object $document
  *
  * @return bool
  */
 public function exists($document)
 {
     $rid = $this->class->getIdentifierValue($document);
     return $this->binding->documentExists($rid);
 }
Exemplo n.º 11
0
 /**
  * Returns the POPO class associated with this repository.
  *
  * @return string
  */
 public function getClassName()
 {
     return $this->metadata->getName();
 }
 /**
  * @depends load_edge_document
  * @test
  *
  * @param ClassMetadata $md
  *
  * @return ClassMetadata
  */
 public function is_edge_document(ClassMetadata $md)
 {
     $this->assertTrue($md->isEdge());
     return $md;
 }
Exemplo n.º 13
0
 private function addFieldMapping(ClassMetadata $metadata, &$mapping)
 {
     $metadata->mapField($mapping);
 }