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')); }
/** * {@inheritdoc} */ public function loadMetadataForClass($className, ClassMetadataInfo $metadata) { $reflClass = $metadata->getReflectionClass(); $classAnnotations = $this->reader->getClassAnnotations($reflClass); // Compatibility with Doctrine Common 3.x if ($classAnnotations && is_int(key($classAnnotations))) { foreach ($classAnnotations as $annot) { $classAnnotations[get_class($annot)] = $annot; } } // Evaluate XmlEntity Annotations if (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlEntity'])) { $entityAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlEntity']; } elseif (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlRootEntity'])) { $entityAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlRootEntity']; $metadata->isRoot = true; } elseif (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlMappedSuperclass'])) { $entityAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlMappedSuperclass']; $metadata->isMappedSuperclass = true; } else { throw MappingException::classIsNotAValidXmlEntity($className); } $metadata->setName($reflClass->getName()); if (isset($entityAnnot->xml)) { $metadata->setXmlName($entityAnnot->xml); } else { $metadata->setXmlName(Inflector::xmlize($reflClass->getShortName())); } if (isset($entityAnnot->repositoryClass)) { $metadata->setCustomRepositoryClass($entityAnnot->repositoryClass); } // Evaluate XmlChangeTrackingPolicy annotation if (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlChangeTrackingPolicy'])) { $changeTrackingAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlChangeTrackingPolicy']; $metadata->setChangeTrackingPolicy(constant('Doctrine\\OXM\\Mapping\\ClassMetadata::CHANGETRACKING_' . $changeTrackingAnnot->value)); } // Check for XmlNamespace/XmlNamespaces annotations $xmlNamespaces = array(); if (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlNamespace'])) { $xmlNamespaceAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlNamespace']; $xmlNamespaces[] = array('url' => $xmlNamespaceAnnot->url, 'prefix' => $xmlNamespaceAnnot->prefix); } else { if (isset($classAnnotations['Doctrine\\OXM\\Mapping\\XmlNamespaces'])) { $xmlNamespaceAnnot = $classAnnotations['Doctrine\\OXM\\Mapping\\XmlNamespaces']; foreach ($xmlNamespaceAnnot->value as $xmlNamespace) { $xmlNamespaces[] = array('url' => $xmlNamespace->url, 'prefix' => $xmlNamespace->prefix); } } } $metadata->setXmlNamespaces($xmlNamespaces); foreach ($reflClass->getProperties() as $property) { if ($metadata->isMappedSuperclass && !$property->isPrivate() || $metadata->isInheritedField($property->name)) { continue; } $mapping = array(); $mapping['fieldName'] = $property->getName(); if ($idAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\OXM\\Mapping\\XmlId')) { $mapping['id'] = true; } if ($generatedValueAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\OXM\\Mapping\\XmlGeneratedValue')) { $metadata->setIdGeneratorType(constant('Doctrine\\OXM\\Mapping\\ClassMetadata::GENERATOR_TYPE_' . $generatedValueAnnot->strategy)); } $referenceAnnot = $this->reader->getPropertyAnnotation($property, 'Doctrine\\OXM\\Mapping\\XmlReferences'); if (isset($referenceAnnot->entityName)) { $mapping['references'] = $referenceAnnot->entityName; } // todo add Id Generator strategy support foreach ($this->reader->getPropertyAnnotations($property) as $fieldAnnot) { if ($fieldAnnot instanceof \Doctrine\OXM\Mapping\XmlField) { if ($fieldAnnot->type == null) { throw MappingException::propertyTypeIsRequired($className, $property->getName()); } $mapping = array_merge($mapping, (array) $fieldAnnot); $metadata->mapField($mapping); } } } // Evaluate @HasLifecycleCallbacks annotations if (isset($classAnnotations['Doctrine\\OXM\\Mapping\\HasLifecycleCallbacks'])) { foreach ($reflClass->getMethods() as $method) { // filter for the declaring class only, callbacks from parents will already be registered. if ($method->isPublic() && $method->getDeclaringClass()->getName() == $reflClass->name) { $annotations = $this->reader->getMethodAnnotations($method); // Compatibility with Doctrine Common 3.x if ($annotations && is_int(key($annotations))) { foreach ($annotations as $annot) { $annotations[get_class($annot)] = $annot; } } if (isset($annotations['Doctrine\\OXM\\Mapping\\PreMarshal'])) { $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::preMarshal); } if (isset($annotations['Doctrine\\OXM\\Mapping\\PostMarshal'])) { $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postMarshal); } if (isset($annotations['Doctrine\\OXM\\Mapping\\PreUnmarshal'])) { $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::preUnmarshal); } if (isset($annotations['Doctrine\\OXM\\Mapping\\PostUnmarshal'])) { $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postUnmarshal); } if (isset($annotations['Doctrine\\OXM\\Mapping\\PrePersist'])) { $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::prePersist); } if (isset($annotations['Doctrine\\OXM\\Mapping\\PostPersist'])) { $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postPersist); } if (isset($annotations['Doctrine\\OXM\\Mapping\\PreUpdate'])) { $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::preUpdate); } if (isset($annotations['Doctrine\\OXM\\Mapping\\PostUpdate'])) { $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postUpdate); } if (isset($annotations['Doctrine\\OXM\\Mapping\\PreRemove'])) { $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::preRemove); } if (isset($annotations['Doctrine\\OXM\\Mapping\\PostRemove'])) { $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postRemove); } if (isset($annotations['Doctrine\\OXM\\Mapping\\PreLoad'])) { $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::preLoad); } if (isset($annotations['Doctrine\\OXM\\Mapping\\PostLoad'])) { $metadata->addLifecycleCallback($method->getName(), \Doctrine\OXM\Events::postLoad); } } } } }
/** * {@inheritdoc} */ public function loadMetadataForClass($className, ClassMetadataInfo $metadata) { $xmlRoot = $this->getElement($className); if ($xmlRoot->getName() == 'entity') { if (isset($xmlRoot['root']) && $xmlRoot['root'] == "true") { $metadata->isRoot = true; $metadata->setCustomRepositoryClass(isset($xmlRoot['repository-class']) ? (string) $xmlRoot['repository-class'] : null); } } else { if ($xmlRoot->getName() == 'mapped-superclass') { $metadata->isMappedSuperclass = true; } else { throw MappingException::classIsNotAValidXmlEntityOrXmlMappedSuperClass($className); } } $metadata->setName($className); // Evalute xml-name attribute if (isset($xmlRoot['xml-name'])) { $metadata->setXmlName((string) $xmlRoot['xml-name']); } else { $metadata->setXmlName(Inflector::xmlize($className)); } // Evaluate change-tracking-policy attribute if (isset($xmlRoot['change-tracking-policy'])) { $metadata->setChangeTrackingPolicy(constant('Doctrine\\OXM\\Mapping\\ClassMetadata::CHANGETRACKING_' . strtoupper((string) $xmlRoot['change-tracking-policy']))); } // Evaluate <namespaces...> if (isset($xmlRoot->namespaces)) { $namespaces = array(); foreach ($xmlRoot->namespaces->namespace as $namespace) { $namespaces[] = array('url' => (string) $namespace['url'], 'prefix' => (string) $namespace['prefix']); } $metadata->setXmlNamespaces($namespaces); } // Evaluate <field ...> mappings if (isset($xmlRoot->field)) { foreach ($xmlRoot->field as $fieldMapping) { $mapping = array('fieldName' => (string) $fieldMapping['name'], 'type' => (string) $fieldMapping['type'], 'node' => constant('Doctrine\\OXM\\Mapping\\ClassMetadata::XML_' . strtoupper((string) $fieldMapping['node']))); if (isset($fieldMapping['xml-name'])) { $mapping['name'] = (string) $fieldMapping['xml-name']; } if (isset($fieldMapping['identifier'])) { $mapping['id'] = (bool) $fieldMapping['identifier']; } if (isset($fieldMapping['direct'])) { $mapping['direct'] = (bool) $fieldMapping['direct']; } if (isset($fieldMapping['nulable'])) { $mapping['nullable'] = (bool) $fieldMapping['nullable']; } if (isset($fieldMapping['required'])) { $mapping['required'] = (bool) $fieldMapping['required']; } if (isset($fieldMapping['collection'])) { $mapping['collection'] = (bool) $fieldMapping['collection']; } if (isset($fieldMapping['get-method'])) { $mapping['getMethod'] = (string) $fieldMapping['get-method']; } if (isset($fieldMapping['set-method'])) { $mapping['setMethod'] = (string) $fieldMapping['set-method']; } if (isset($fieldMapping['prefix'])) { $mapping['prefix'] = (string) $fieldMapping['prefix']; } if (isset($fieldMapping['wrapper'])) { $mapping['wrapper'] = (string) $fieldMapping['wrapper']; } $metadata->mapField($mapping); } } // Evaluate <lifecycle-callbacks...> if (isset($xmlRoot->{'lifecycle-callbacks'})) { foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) { $metadata->addLifecycleCallback((string) $lifecycleCallback['method'], constant('Doctrine\\OXM\\Events::' . (string) $lifecycleCallback['type'])); } } }