Пример #1
0
 public function getClassMap($className)
 {
     if ($className == '') {
         mtracestack();
     }
     //$className = strtolower(trim($className));
     $classIndex = strtolower(trim($className));
     if ($className[0] == '\\') {
         $className = substr($className, 1);
     }
     if ($className == '') {
         return;
     }
     if (isset($this->classMaps[$classIndex])) {
         return $this->classMaps[$classIndex];
     }
     $map = $this->getMap($className);
     //var_dump($map);
     $database = $map['database'];
     $classMap = new \Maestro\Persistence\Map\ClassMap($className, $database);
     $classMap->setDatabaseName($database);
     $classMap->setTableName($map['table']);
     if (isset($map['extends'])) {
         $classMap->setSuperClassName($map['extends']);
     }
     $config = $className::config();
     $attributes = $map['attributes'];
     foreach ($attributes as $attributeName => $attr) {
         $attributeMap = new \Maestro\Persistence\Map\AttributeMap($attributeName, $classMap);
         if (isset($attr['index'])) {
             $attributeMap->setIndex($attr['index']);
         }
         $type = isset($attr['type']) ? strtolower($attr['type']) : 'string';
         $attributeMap->setType($type);
         $plataformTypedAttributes = $classMap->getDb()->getPlatform()->getTypedAttributes();
         $attributeMap->setHandled(strpos($plataformTypedAttributes, $type) !== false);
         if ($config['converters'][$attributeName]) {
             $attributeMap->setConverter($config['converters'][$attributeName]);
         }
         $attributeMap->setColumnName($attr['column'] ?: $attributeName);
         $attributeMap->setAlias($attr['alias'] ?: $attributeName);
         $attributeMap->setKeyType($attr['key'] ?: 'none');
         $attributeMap->setIdGenerator($attr['idgenerator']);
         if ($attr['key'] == 'reference' && $classMap->getSuperClassMap() != NULL) {
             $referenceAttribute = $classMap->getSuperClassMap()->getAttributeMap($attributeName);
             if ($referenceAttribute) {
                 $attributeMap->setReference($referenceAttribute);
             }
         }
         $classMap->addAttributeMap($attributeMap);
     }
     $this->classMaps[$classIndex] = $classMap;
     if ($referenceAttribute) {
         // set superAssociationMap
         $attributeName = $referenceAttribute->getName();
         $superClassName = $classMap->getSuperClassMap()->getName();
         $superAssociationMap = new \Maestro\Persistence\Map\AssociationMap($classMap, $superClassName);
         $superAssociationMap->setToClassName($superClassName);
         $superAssociationMap->setToClassMap($classMap->getSuperClassMap());
         $superAssociationMap->setCardinality('oneToOne');
         $superAssociationMap->addKeys($attributeName, $attributeName);
         $superAssociationMap->setKeysAttributes();
         $classMap->setSuperAssociationMap($superAssociationMap);
     }
     $associations = $map['associations'];
     if (isset($associations)) {
         $fromClassMap = $classMap;
         foreach ($associations as $associationName => $association) {
             $toClass = $association['toClass'];
             $associationMap = new \Maestro\Persistence\Map\AssociationMap($classMap, $associationName);
             $associationMap->setToClassName($toClass);
             //$associationMap->setTargetAttributeName($associationName);
             $associationMap->setDeleteAutomatic($association['deleteAutomatic']);
             $associationMap->setSaveAutomatic($association['saveAutomatic']);
             $associationMap->setRetrieveAutomatic($association['retrieveAutomatic']);
             //$associationMap->setJoinAutomatic($association['joinAutomatic']);
             $autoAssociation = strtolower($className) == strtolower($toClass);
             if (!$autoAssociation) {
                 $autoAssociation = strtolower($className) == strtolower(substr($toClass, 1));
             }
             $associationMap->setAutoAssociation($autoAssociation);
             if (isset($association['index'])) {
                 $associationMap->setIndexAttribute($association['index']);
             }
             $associationMap->setCardinality($association['cardinality']);
             if ($association['cardinality'] == 'manyToMany') {
                 $associationMap->setAssociativeTable($association['associative']);
             } else {
                 $arrayKeys = explode(',', $association['keys']);
                 foreach ($arrayKeys as $keys) {
                     $key = explode(':', $keys);
                     $associationMap->addKeys($key[0], $key[1]);
                 }
             }
             if (isset($association['order'])) {
                 $order = array();
                 $orderAttributes = explode(',', $association['order']);
                 foreach ($orderAttributes as $orderAttr) {
                     $o = explode(' ', $orderAttr);
                     $ascend = substr($o[1], 0, 3) == 'asc';
                     $order[] = array($o[0], $ascend);
                 }
                 if (count($order)) {
                     $associationMap->setOrder($order);
                 }
             }
             $fromClassMap->putAssociationMap($associationMap);
         }
     }
     return $classMap;
 }