Doctrine\OXM\Mapping\ClassMetadataInfo::mapField PHP Метод

mapField() публичный Метод

public mapField ( array $mapping ) : void
$mapping array
Результат void
    public function mapField(array $mapping)
    {
        // Check mandatory fields
        if (!isset($mapping['fieldName']) || strlen($mapping['fieldName']) == 0) {
            throw MappingException::missingFieldName($this->name);
        }
        if (isset($this->fieldMappings[$mapping['fieldName']])) {
            $existingMapping = $this->fieldMappings[$mapping['fieldName']];
            // only complain if one exists for this class, and not any parents
            if (!isset($existingMapping['declared'])) {
                throw MappingException::duplicateFieldMapping($this->name, $mapping['fieldName']);
            } elseif ($existingMapping['declared'] == $this->rootXmlEntityName) {
                throw MappingException::duplicateFieldMapping($this->name, $mapping['fieldName']);
            }
        }
        if (!isset($mapping['type']) || strlen($mapping['type']) == 0) {
            throw MappingException::missingFieldType($this->name, $mapping['fieldName']);
        }
        if (!isset($mapping['name'])) {
            $mapping['name'] = Inflector::xmlize($mapping['fieldName']);
        } else {
            if ($mapping['name'][0] == '`') {
                $mapping['name'] = trim($mapping['name'], '`');
                $mapping['quoted'] = true;
            }
        }
        if (isset($this->xmlFieldMap[$mapping['name']])) {
            $existingMapping = $this->fieldMappings[$this->xmlFieldMap[$mapping['name']]];
            if (!isset($existingMapping['declared'])) {
                throw MappingException::duplicateXmlFieldName($this->name, $mapping['name']);
            } elseif ($existingMapping['declared'] == $this->rootXmlEntityName) {
                throw MappingException::duplicateXmlFieldName($this->name, $mapping['name']);
            }
        }
        if (!isset($mapping['node'])) {
            if (Type::hasType($mapping['type'])) {
                // Map object and array to "text", everything else to "attribute"
                if (in_array($mapping['type'], array(Type::OBJECT, Type::TARRAY))) {
                    $mapping['node'] = self::XML_TEXT;
                } else {
                    $mapping['node'] = self::XML_ATTRIBUTE;
                }
            } else {
                $mapping['node'] = self::XML_ELEMENT;
            }
        }
        if (!in_array($mapping['node'], self::getXmlNodeTypes())) {
            throw MappingException::xmlBindingTypeUnknown($mapping['fieldName'], $mapping['node']);
        }
        if (!isset($mapping['direct'])) {
            $mapping['direct'] = true;
        }
        if (!isset($mapping['nullable'])) {
            $mapping['nullable'] = false;
        }
        if (!isset($mapping['required'])) {
            $mapping['required'] = false;
        }
        if (!isset($mapping['container'])) {
            $mapping['container'] = false;
        }
        if (!isset($mapping['collection'])) {
            $mapping['collection'] = false;
        }
        if (!isset($mapping['getMethod'])) {
            $mapping['getMethod'] = $this->inferGetter($mapping['fieldName']);
        }
        if (!isset($mapping['setMethod'])) {
            $mapping['setMethod'] = $this->inferSetter($mapping['fieldName']);
        }
        if (!isset($mapping['id'])) {
            $mapping['id'] = false;
        } else {
            $this->identifier = $mapping['fieldName'];
        }
        $this->xmlFieldMap[$mapping['name']] = $mapping['fieldName'];
        $this->fieldMappings[$mapping['fieldName']] = $mapping;
        return $mapping;
    }

Usage Example

Пример #1
0
 public static function loadMetadata(ClassMetadataInfo $metadata)
 {
     $metadata->setXmlName('cms-user');
     $metadata->isRoot = true;
     $metadata->setXmlNamespaces(array(array('url' => 'http://www.schema.com/foo', 'prefix' => 'foo'), array('url' => 'http://www.schema.com/bar', 'prefix' => 'bar')));
     $metadata->addLifecycleCallback('doStuffOnPrePersist', 'prePersist');
     $metadata->addLifecycleCallback('doOtherStuffOnPrePersistToo', 'prePersist');
     $metadata->addLifecycleCallback('doStuffOnPostPersist', 'postPersist');
     $metadata->addLifecycleCallback('doStuffOnPreMarshal', 'preMarshal');
     $metadata->mapField(array('fieldName' => 'id', 'id' => true, 'type' => 'string', 'node' => 'attribute'));
     $metadata->mapField(array('fieldName' => 'name', 'type' => 'string', 'node' => 'text', 'required' => true, 'setMethod' => 'setUsername', 'getMethod' => 'getUsername'));
     $metadata->mapField(array('fieldName' => 'comments', 'type' => 'string', 'node' => 'text', 'collection' => true, 'wrapper' => 'comments', 'name' => 'comment'));
 }
All Usage Examples Of Doctrine\OXM\Mapping\ClassMetadataInfo::mapField