Doctrine\ODM\MongoDB\Mapping\Types\Type::getType PHP Method

getType() public static method

Get a Type instance.
public static getType ( string $type ) : Doctrine\ODM\MongoDB\Mapping\Types\Type
$type string The type name.
return Doctrine\ODM\MongoDB\Mapping\Types\Type $type
    public static function getType($type)
    {
        if ( ! isset(self::$typesMap[$type])) {
            throw new \InvalidArgumentException(sprintf('Invalid type specified "%s".', $type));
        }
        if ( ! isset(self::$types[$type])) {
            $className = self::$typesMap[$type];
            self::$types[$type] = new $className;
        }
        return self::$types[$type];
    }

Usage Example

 /**
  * Makes additional translation of $document $field into $locale
  * using $value
  *
  * @param object $document
  * @param string $field
  * @param string $locale
  * @param mixed $value
  * @return TranslationRepository
  */
 public function translate($document, $field, $locale, $value)
 {
     $meta = $this->dm->getClassMetadata(get_class($document));
     $listener = $this->getTranslatableListener();
     $config = $listener->getConfiguration($this->dm, $meta->name);
     if (!isset($config['fields']) || !in_array($field, $config['fields'])) {
         throw new \Gedmo\Exception\InvalidArgumentException("Document: {$meta->name} does not translate field - {$field}");
     }
     $modRecordValue = !$listener->getPersistDefaultLocaleTranslation() && $locale === $listener->getDefaultLocale() || $listener->getTranslatableLocale($document, $meta) === $locale;
     if ($modRecordValue) {
         $meta->getReflectionProperty($field)->setValue($document, $value);
         $this->dm->persist($document);
     } else {
         if (isset($config['translationClass'])) {
             $class = $config['translationClass'];
         } else {
             $ea = new TranslatableAdapterODM();
             $class = $listener->getTranslationClass($ea, $config['useObjectClass']);
         }
         $foreignKey = $meta->getReflectionProperty($meta->identifier)->getValue($document);
         $objectClass = $config['useObjectClass'];
         $transMeta = $this->dm->getClassMetadata($class);
         $trans = $this->findOneBy(compact('locale', 'field', 'objectClass', 'foreignKey'));
         if (!$trans) {
             $trans = $transMeta->newInstance();
             $transMeta->getReflectionProperty('foreignKey')->setValue($trans, $foreignKey);
             $transMeta->getReflectionProperty('objectClass')->setValue($trans, $objectClass);
             $transMeta->getReflectionProperty('field')->setValue($trans, $field);
             $transMeta->getReflectionProperty('locale')->setValue($trans, $locale);
         }
         $mapping = $meta->getFieldMapping($field);
         $type = Type::getType($mapping['type']);
         $transformed = $type->convertToDatabaseValue($value);
         $transMeta->getReflectionProperty('content')->setValue($trans, $transformed);
         if ($this->dm->getUnitOfWork()->isInIdentityMap($document)) {
             $this->dm->persist($trans);
         } else {
             $oid = spl_object_hash($document);
             $listener->addPendingTranslationInsert($oid, $trans);
         }
     }
     return $this;
 }
All Usage Examples Of Doctrine\ODM\MongoDB\Mapping\Types\Type::getType