public function getClassMap($className) { $className = strtolower(trim($className)); if ($className[0] == '\\') { $className = substr($className, 1); } if ($className == '') { return; } if (isset($this->classMaps[$className])) { return $this->classMaps[$className]; } $map = $this->getMap($className); //var_dump($map); $database = $map['database']; $classMap = new ClassMap($className, $database, $this->manager); $classMap->setDatabaseName($database); //var_dump($className . '-' . $map['table']); $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 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[$className] = $classMap; if ($referenceAttribute) { // set superAssociationMap $attributeName = $referenceAttribute->getName(); $superClassName = $classMap->getSuperClassMap()->getName(); $superAssociationMap = new 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 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; }
public function getClassMap($className, $module = '', $class = '') { $className = str_replace('\\', '', $className); if (isset($this->classMaps[$className])) { return $this->classMaps[$className]; } if ($module == '') { $module = $this->location[$className]['module']; $class = $this->location[$className]['class']; } else { $this->location[$className]['module'] = $module; $this->location[$className]['class'] = $class; } $xml = $this->getMap($module, $class, $className); $database = (string) $xml->databaseName; $classMap = new ClassMap($className, $database); $classMap->setDatabaseName($database); $classMap->setTableName((string) $xml->tableName); if (isset($xml->extends)) { $classMap->setSuperClassName((string) $xml->extends); } //$config = $className::config(); $attributes = $this->getAsArray($xml->attribute); foreach ($attributes as $attr) { $attributeMap = new AttributeMap((string) $attr->attributeName, $classMap); // $converter = $this->getConverter($attr); if (isset($attr->attributeIndex)) { $attributeMap->setIndex($attr->attributeIndex); } $type = isset($attr->columnType) ? strtolower($attr->columnType) : 'string'; if (($converterName = strtolower($attr->converter->converterName)) != '') { if ($converterName == 'timestampconverter') { $type = 'timestamp'; } } $attributeMap->setType($type); $plataformTypedAttributes = $classMap->getDb()->getPlatform()->getTypedAttributes(); $attributeMap->setHandled(strpos($plataformTypedAttributes, $type)); $attributeMap->setColumnName($attr->columnName ?: $attributeName); $attributeMap->setAlias($attr->aliasName ?: $attributeName); $attributeMap->setKeyType($attr->key ?: 'none'); $attributeMap->setIdGenerator($attr->idgenerator); if (isset($attr->reference) && $classMap->getSuperClassMap() != NULL) { $referenceAttribute = $classMap->getSuperClassMap()->getAttributeMap($attributeName); if ($referenceAttribute) { $attributeMap->setReference($referenceAttribute); } } $classMap->addAttributeMap($attributeMap); } $this->classMaps[$className] = $classMap; if (isset($xml->association)) { $associations = $this->getAsArray($xml->association); $fromClassMap = $classMap; foreach ($associations as $association) { $associationName = (string) $association->target; $toClass = 'business' . $association->toClassModule . $association->toClassName; $this->location[$toClass]['module'] = $association->toClassModule; $this->location[$toClass]['class'] = $association->toClassName; $classPath = Manager::getAppPath("modules/" . $association->toClassModule . '/models/' . $association->toClassName . '.class.php'); Manager::addAutoloadClass($toClass, $classPath); $associationMap = new AssociationMap($classMap, $associationName); $associationMap->setToClassName($toClass); $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->indexAttribute)) { $associationMap->setIndexAttribute($association->indexAttribute->indexAttributeName); } $associationMap->setCardinality($association->cardinality); if ($association->cardinality == 'manyToMany') { $associativeClassName = 'business' . $association->associativeClassModule . $association->associativeClassName; $associativeXML = $this->getMap($association->associativeClassModule, $association->associativeClassName, $associativeClassName); $associationMap->setAssociativeTable((string) $associativeXML->tableName); } else { $entries = $this->getAsArray($association->entry); $inverse = $association->inverse == 'true'; foreach ($entries as $entry) { $fromAttribute = $inverse ? $entry->toAttribute : $entry->fromAttribute; $toAttribute = $inverse ? $entry->fromAttribute : $entry->toAttribute; $associationMap->addKeys($fromAttribute, $toAttribute); } } if (isset($association->orderAttribute)) { $order = array(); $orderAttributes = $this->getAsArray($association->orderAttribute); foreach ($orderAttributes as $orderAttr) { $ascend = $orderAttr->orderAttributeDirection == 'ascend'; $order[] = array($orderAttr->orderAttributeName, $ascend); } if (count($order)) { $associationMap->setOrder($order); } } $fromClassMap->putAssociationMap($associationMap); } } return $classMap; }
private function __deleteAssociationById(PersistentObject $object, AssociationMap $associationMap, $id, &$commands, ClassMap $classMap) { $toAttributeMap = $associationMap->getToAttributeMap(); $fromAttributeMap = $associationMap->getFromAttributeMap(); if (!is_array($id)) { $id = array($id); } if ($associationMap->getCardinality() == 'oneToOne') { if ($associationMap->isInverse()) { // obtem o objeto referenciado $refObject = $object->get($associationMap->getName()); $refObject->setAttributeValue($toAttributeMap, NULL); $this->_saveObject($refObject, $associationMap->getToClassMap(), $commands); } else { // se a associação é direta, atualiza o próprio objeto $object->setAttributeValue($fromAttributeMap, NULL); $this->_saveObject($object, $classMap, $commands); } } elseif ($associationMap->getCardinality() == 'oneToMany') { $refObject = $associationMap->getToClassMap()->getObject(); $commands[] = $associationMap->getUpdateStatementId($object, $id, NULL); } elseif ($associationMap->getCardinality() == 'manyToMany') { $commands[] = $associationMap->getDeleteStatementId($object, $id); } $associationMap->setKeysAttributes(); $this->__retrieveAssociation($object, $associationMap, $classMap); }