Merge branch 'master' of ssh://172.20.0.1/var/git/espo/backend

This commit is contained in:
Yuri Kuznetsov
2013-12-26 15:19:24 +02:00
21 changed files with 175 additions and 51 deletions
@@ -67,7 +67,7 @@ class Converter
*/
public function process()
{
$GLOBALS['log']->add('Debug', 'Converter:process() - Start: converting metadata to orm format and database schema');
$GLOBALS['log']->add('Debug', 'Converters\Orm - Start: orm convertation');
$entityDefs = $this->getMetadata()->get('entityDefs');
@@ -87,8 +87,7 @@ class Converter
//save database meta to a file espoMetadata.php
$result = $this->getMetadata()->setOrmMetadata($databaseMeta);
$GLOBALS['log']->add('Debug', 'Converter:process() - End: converting metadata to orm format and database schema, result=['.$result.']');
$GLOBALS['log']->add('Debug', 'Converters\Orm - End: orm convertation, result=['.$result.']');
return $result;
}
@@ -63,16 +63,16 @@ class Links
$params['targetEntity'] = $foreignParams['entityName'];
$foreignParams['targetEntity'] = $params['entityName'];
//hardcode for Teams
//hasMany With Relation Name
if (isset($link['params'])) {
switch ($link['params']['type']) {
case 'hasMany':
if ($params['targetEntity'] == 'Team') {
$method = 'teamRelation';
if (isset($link['params']['relationName'])) {
$method = 'hasManyWithName';
}
break;
}
} //END: hardcode
} //END: hasMany With Relation Name
if (method_exists($this, $method)) {
return $this->$method($params, $foreignParams);
@@ -28,11 +28,15 @@ class Orm
*/
protected $fieldAccordances = array(
'type' => 'type',
'dbType' => 'dbType',
'maxLength' => 'len',
'len' => 'len',
'notnull' => 'notnull',
'autoincrement' => 'autoincrement',
'notStorable' => 'notStorable',
'link' => 'relation',
'field' => 'foreign', //todo change "foreign" to "field"
'unique' => 'unique',
'default' => array(
'condition' => '^javascript:',
'conditionEquals' => false,
@@ -107,13 +111,15 @@ class Orm
$fieldParams['len'] = $this->defaultLength['varchar'];
break;
case 'datetime':
$fieldParams['notnull'] = false;
break;
case 'bool':
$fieldParams['default'] = isset($fieldParams['default']) ? (bool) $fieldParams['default'] : $this->defaultValue['bool'];
break;
case 'personName':
$fieldParams['type'] = Entity::VARCHAR;
$typeDefs = $this->getLinks()->process('typePersonName', $entityName, array('name' => $fieldName));
$fieldParams = Util::merge($fieldParams, $typeDefs[$entityName]['fields'][$fieldName]);
break;
}
}
}
@@ -141,7 +147,7 @@ class Orm
'dbType' => 'varchar',
),
'name' => array(
'type' => Entity::VARCHAR,
'type' => isset($entityMeta['fields']['name']['type']) ? $entityMeta['fields']['name']['type'] : Entity::VARCHAR,
'notStorable' => true,
),
);
@@ -150,22 +156,21 @@ class Orm
//check if "fields" option exists in $fieldMeta
$fieldParams['type'] = isset($fieldParams['type']) ? $fieldParams['type'] : '';
$fieldTypeMeta = $this->getMetadata()->get('fields.'.$fieldParams['type']);
if (isset($fieldTypeMeta['fields']) && is_array($fieldTypeMeta['fields'])) {
$namingType = isset($fieldTypeMeta['naming']) ? $fieldTypeMeta['naming'] : $this->defaultNaming;
foreach($fieldTypeMeta['fields'] as $subFieldName => $subFieldParams) {
foreach($fieldTypeMeta['actualFields'] as $subFieldName) {
//$subFieldNameNaming = Util::fromCamelCase( Util::getNaming($fieldName, $subFieldName, $namingType, '_'), '_' );
$subFieldNameNaming = Util::getNaming($fieldName, $subFieldName, $namingType);
if (!isset($entityMeta['fields'][$subFieldNameNaming])) {
$subFieldDefs = $this->convertField($entityName, $subFieldName, $subFieldParams);
$subField = $this->convertActualFields($entityName, $fieldName, $fieldParams, $subFieldName, $fieldTypeMeta);
//if (!isset($entityMeta['fields'][ $subField['naming'] ])) {
if (!isset($outputMeta[ $subField['naming'] ])) {
$subFieldDefs = $this->convertField($entityName, $subField['name'], $subField['params']);
if ($subFieldDefs !== false) {
$outputMeta[$subFieldNameNaming] = $subFieldDefs; //push fieldDefs to the main array
$outputMeta[ $subField['naming'] ] = $subFieldDefs; //push fieldDefs to the main array
}
}
}
} else {
@@ -214,13 +219,25 @@ class Orm
$fieldParams['type'] = $this->defaultFieldType;
} //END: set default type if exists
$fieldTypeMeta = $this->getMetadata()->get('fields.'.$fieldParams['type']);
if (isset($fieldParams['dbType'])) {
$fieldTypeMeta = $this->getMetadata()->get('fields.'.$fieldParams['dbType']);
} else {
$fieldTypeMeta = $this->getMetadata()->get('fields.'.$fieldParams['type']);
}
//check if need to skip this field into database metadata
if (isset($fieldTypeMeta['database']['skip']) && $fieldTypeMeta['database']['skip'] === true) {
return false;
}
//merge database options from field definition
if (isset($fieldTypeMeta['database'])) {
$fieldParams = Util::merge($fieldParams, $fieldTypeMeta['database']);
}
//if (isset($fieldTypeMeta['database']['notnull']))
$fieldDefs = $this->getInitValues($fieldParams);
//check if field need to be saved in database
@@ -229,10 +246,10 @@ class Orm
$fieldDefs['notStorable'] = true;
} //END: check if field need to be saved in database
//merge database options from field definition
if (isset($fieldTypeMeta['database'])) {
//merge database options from field definition
/*if (isset($fieldTypeMeta['database'])) {
$fieldDefs = Util::merge($fieldDefs, $fieldTypeMeta['database']);
}
}*/
//check and set a field length
if (!isset($fieldDefs['len']) && in_array($fieldDefs['type'], array_keys($this->defaultLength))) {
@@ -242,6 +259,50 @@ class Orm
return $fieldDefs;
}
protected function convertActualFields($entityName, $fieldName, $fieldParams, $subFieldName, $fieldTypeMeta)
{
$subField = array();
$subField['params'] = $this->getInitValues($fieldParams);
//if empty field name, then use the main field
if (trim($subFieldName) == '') {
if (!isset($fieldTypeMeta['database'])) {
$GLOBALS['log']->add('EXCEPTION', 'Empty field defs for ['.$entityName.':'.$fieldName.'] using "actualFields". Main field ['.$fieldName.']');
}
$subField['name'] = $fieldName;
$subField['naming'] = $fieldName;
if (isset($fieldTypeMeta['database'])) {
$subField['params'] = Util::merge($subField['params'], $fieldTypeMeta['database']);
}
} else {
if (!isset($fieldTypeMeta['fields'][$subFieldName])) {
$GLOBALS['log']->add('EXCEPTION', 'Empty field defs for ['.$entityName.':'.$subFieldName.'] using "actualFields". Main field ['.$fieldName.']');
}
$namingType = isset($fieldTypeMeta['naming']) ? $fieldTypeMeta['naming'] : $this->defaultNaming;
$subField['name'] = $subFieldName;
$subField['naming'] = Util::getNaming($fieldName, $subFieldName, $namingType);
if (isset($fieldTypeMeta['fields'][$subFieldName])) {
$subField['params'] = Util::merge($subField['params'], $fieldTypeMeta['fields'][$subFieldName]);
}
}
/*
name = $subFieldName
naming = $subFieldNameNaming
params = $subFieldParams
*/
return $subField;
}
protected function convertLinks($entityName, $entityMeta, array $entityDefs)
@@ -252,12 +313,8 @@ class Orm
$relationships = array();
foreach($entityMeta['links'] as $linkName => $linkParams) {
//echo $linkName.'<br />';
//print_r($linkParams);
$linkEntityName = $this->getLinks()->getLinkEntityName($entityName, $linkParams);
//print_r($entityDefs[$linkEntityName]['links']);
//print_r($convertedMeta[$linkEntityName]);
$currentType = $linkParams['type'];
$parentType = '';
@@ -346,7 +403,7 @@ class Orm
$values = array();
foreach($this->fieldAccordances as $espoType => $doctrineType) {
if (isset($fieldParams[$espoType]) && !empty($fieldParams[$espoType])) {
if (isset($fieldParams[$espoType])) {
if (is_array($doctrineType)) {
@@ -216,20 +216,61 @@ class Relations
}
public function teamRelation($params, $foreignParams)
public function typePersonName($params, $foreignParams)
{
$foreignField = $this->getForeignField($params['link']['name'], $params['entityName']);
$tableName = Util::toUnderScore($params['entityName']);
$fullList = array(); //contains empty string (" ") like delimiter
$fieldList = array(); //doesn't contain empty string (" ") like delimiter
$like = array();
foreach($foreignField as $fieldName) {
$fieldNameTrimmed = trim($fieldName);
if (!empty($fieldNameTrimmed)) {
$columnName = $tableName.'.'.Util::toUnderScore($fieldNameTrimmed);
$fullList[] = $fieldList[] = $columnName;
$like[] = $columnName." LIKE '{text}'";
} else {
$fullList[] = "'".$fieldName."'";
}
}
return array(
$params['entityName'] => array (
'fields' => array(
$params['link']['name'] => array(
'select' => "TRIM(CONCAT(".implode(", ", $fullList)."))",
'where' => array(
'LIKE' => "(".implode(" OR ", $like)." OR CONCAT(".implode(", ", $fullList).") LIKE '{text}')",
),
'orderBy' => implode(", ", $fieldList),
),
),
),
);
}
//public function teamRelation($params, $foreignParams)
public function hasManyWithName($params, $foreignParams)
{
$relationKeys = explode('-', Util::fromCamelCase($params['link']['params']['relationName']));
$midKeys = array();
foreach($relationKeys as $key) {
$midKeys[] = $key.'Id';
}
return array(
$params['entityName'] => array(
'relations' => array(
$params['link']['name'] => array(
'type' => Entity::MANY_MANY,
'entity' => $params['targetEntity'],
'relationName' => 'EntityTeam',
'midKeys' => array(
'entityId',
'teamId',
),
'relationName' => $params['link']['params']['relationName'],
'midKeys' => $midKeys,
'conditions' => array('entityType' => $params['entityName']),
),
),
@@ -54,7 +54,9 @@ class Schema
//convertToSchema
public function process(array $databaseMeta, $entityDefs)
{
{
$GLOBALS['log']->add('Debug', 'Converters\Schema - Start: building schema');
$schema = $this->getSchema();
$tables = array();
@@ -145,6 +147,9 @@ class Schema
}
//END: check and create columns/tables for relations
$GLOBALS['log']->add('Debug', 'Converters\Schema - End: building schema');
return $schema;
}
@@ -107,7 +107,8 @@
},
"teams": {
"type": "hasMany",
"entity": "Team"
"entity": "Team",
"relationName": "EntityTeam"
},
"contacts": {
"type": "hasMany",
@@ -88,7 +88,8 @@
},
"teams": {
"type": "hasMany",
"entity": "Team"
"entity": "Team",
"relationName": "EntityTeam"
},
"users": {
"type": "hasMany",
@@ -70,7 +70,8 @@
},
"teams": {
"type": "hasMany",
"entity": "Team"
"entity": "Team",
"relationName": "EntityTeam"
},
"account": {
"type": "belongsTo",
@@ -107,7 +107,8 @@
},
"teams": {
"type": "hasMany",
"entity": "Team"
"entity": "Team",
"relationName": "EntityTeam"
},
"account": {
"type": "belongsTo",
@@ -76,7 +76,8 @@
},
"teams": {
"type": "hasMany",
"entity": "Team"
"entity": "Team",
"relationName": "EntityTeam"
},
"attachments": {
"type": "hasChildren",
@@ -83,7 +83,8 @@
},
"teams": {
"type": "hasMany",
"entity": "Team"
"entity": "Team",
"relationName": "EntityTeam"
},
"caseEmailTemplate": {
"type": "belongsTo",
@@ -132,7 +132,8 @@
},
"teams": {
"type": "hasMany",
"entity": "Team"
"entity": "Team",
"relationName": "EntityTeam"
},
"opportunities": {
"type": "hasMany",
@@ -83,7 +83,8 @@
},
"teams": {
"type": "hasMany",
"entity": "Team"
"entity": "Team",
"relationName": "EntityTeam"
},
"users": {
"type": "hasMany",
@@ -75,7 +75,8 @@
},
"teams": {
"type": "hasMany",
"entity": "Team"
"entity": "Team",
"relationName": "EntityTeam"
},
"account": {
"type": "belongsTo",
@@ -108,7 +108,8 @@
},
"teams": {
"type": "hasMany",
"entity": "Team"
"entity": "Team",
"relationName": "EntityTeam"
}
},
"collection": {
@@ -72,7 +72,8 @@
},
"teams": {
"type": "hasMany",
"entity": "Team"
"entity": "Team",
"relationName": "EntityTeam"
},
"parent": {
"type": "belongsToParent",
@@ -50,7 +50,8 @@
},
"teams": {
"type": "hasMany",
"entity": "Team"
"entity": "Team",
"relationName": "EntityTeam"
},
"assignedUser": {
"type": "belongsTo",
@@ -15,7 +15,8 @@
},
"teams": {
"type": "hasMany",
"entity": "Team"
"entity": "Team",
"relationName": "EntityTeam"
}
},
"collection": {
@@ -27,5 +27,8 @@
"search":{
"basic":false,
"advanced":true
},
"database":{
"type":"float"
}
}
@@ -21,5 +21,8 @@
"search":{
"basic":false,
"advanced":true
},
"database":{
"notnull":false
}
}
@@ -21,5 +21,8 @@
"search":{
"basic":false,
"advanced":false
},
"database":{
"notnull":false
}
}