From 229df5cb2117b12ddda1af88a4e5a6cb92c9ccac Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Tue, 24 Dec 2013 16:53:05 +0200 Subject: [PATCH] add fields for linkMultiple '..Ids', '..Names' --- .../Core/Utils/Database/Converters/Links.php | 75 ++++++++----------- .../Core/Utils/Database/Converters/Orm.php | 19 +++-- .../Utils/Database/Converters/Relations.php | 29 +++++-- .../Core/Utils/Database/Converters/Schema.php | 3 +- .../metadata/fields/linkMultiple.json | 3 - 5 files changed, 69 insertions(+), 60 deletions(-) diff --git a/application/Espo/Core/Utils/Database/Converters/Links.php b/application/Espo/Core/Utils/Database/Converters/Links.php index c0a0ba6dd5..e91a14d0e1 100644 --- a/application/Espo/Core/Utils/Database/Converters/Links.php +++ b/application/Espo/Core/Utils/Database/Converters/Links.php @@ -34,12 +34,19 @@ class Links if (isset($link['params'])) { return isset($link['params']['entity']) ? $link['params']['entity'] : $entityName; } - /*if (!isset($link['entity']) && isset($link['entities'])) { - return $link['entities']; - } */ + return isset($link['entity']) ? $link['entity'] : $entityName; } + public function isMethodExists($methodName) + { + if (method_exists($this, $methodName) || method_exists($this->getRelations(), $methodName)) { + return true; + } + + return false; + } + public function process($method, $entityName, $link, $foreignLink = array()) { @@ -65,78 +72,58 @@ class Links } break; } - } - //END: hardcode - + } //END: hardcode if (method_exists($this, $method)) { return $this->$method($params, $foreignParams); + } else if (method_exists($this->getRelations(), $method)) { + return $this->getRelations()->$method($params, $foreignParams); } return false; } - - protected function belongsTo($params, $foreignParams) - { - return $this->getRelations()->belongsTo($params, $foreignParams); - } - - //TODO: hook for teams - protected function hasMany($params, $foreignParams) - { - return $this->getRelations()->hasMany($params, $foreignParams); - } - - protected function hasManyHasMany($params, $foreignParams) { return $this->getRelations()->manyMany($params, $foreignParams); } + + /*protected function belongsTo($params, $foreignParams) + { + return $this->getRelations()->belongsTo($params, $foreignParams); + } + + protected function hasMany($params, $foreignParams) + { + return $this->getRelations()->hasMany($params, $foreignParams); + } + protected function hasChildren($params, $foreignParams) { return $this->getRelations()->hasChildren($params, $foreignParams); } - /*protected function belongsToParent($params, $foreignParams) - { - return $this->getRelations()->belongsToParent($params, $foreignParams); - }*/ - protected function linkParent($params, $foreignParams) { return $this->getRelations()->linkParent($params, $foreignParams); } + protected function linkMultiple($params, $foreignParams) + { + return $this->getRelations()->linkMultiple($params, $foreignParams); + } + protected function teamRelation($params, $foreignParams) { return $this->getRelations()->teamRelation($params, $foreignParams); - } + } */ - - - - /*protected function hasChildrenBelongsToParent() - { - return $this->getRelations()->hasChildren($params, $foreignParams); - }*/ - - /*protected function hasManyBelongsTo($params, $foreignParams) - { - $hasMany = $this->getRelations()->hasMany($params, $foreignParams); - $belongsTo = $this->getRelations()->belongsTo($foreignParams, $params); - //$belongsTo [Contact] [relations] [account] ['foreignKey'] = 'id'; ??? - - return Util::merge($hasMany, $belongsTo); - } */ - - - /* +/* [0] => belongsTo [1] => belongsToParent [2] => hasMany diff --git a/application/Espo/Core/Utils/Database/Converters/Orm.php b/application/Espo/Core/Utils/Database/Converters/Orm.php index 89e1cbbeb4..5e12b77437 100644 --- a/application/Espo/Core/Utils/Database/Converters/Orm.php +++ b/application/Espo/Core/Utils/Database/Converters/Orm.php @@ -181,8 +181,14 @@ class Orm switch ($fieldParams['type']) { case 'linkParent': - $relation = $this->getLinks()->process('linkParent', $entityName, array('name' => $fieldName)); - $outputMeta = Util::merge($outputMeta, $relation[$entityName]['fields']); + $linkData = $this->getLinks()->process('linkParent', $entityName, array('name' => $fieldName)); + $outputMeta = Util::merge($outputMeta, $linkData[$entityName]['fields']); + break; + + case 'linkMultiple': + $linkData = $this->getLinks()->process('linkMultiple', $entityName, array('name' => $fieldName)); + unset($outputMeta[$fieldName]); //no need "linkMultiple" field + $outputMeta = Util::merge($outputMeta, $linkData[$entityName]['fields']); break; } } @@ -278,9 +284,9 @@ class Orm } */ - if (method_exists($this->getLinks(), $method)) { //ex. hasManyHasMany + if ( $this->getLinks()->isMethodExists($method) ) { //ex. hasManyHasMany $convertedLink = $this->getLinks()->process($method, $entityName, array('name'=>$linkName, 'params'=>$linkParams), $foreignLink); - } else if (method_exists($this->getLinks(), $currentType)) { //ex. hasMany + } else { //ex. hasMany $convertedLink = $this->getLinks()->process($currentType, $entityName, array('name'=>$linkName, 'params'=>$linkParams), $foreignLink); } @@ -289,7 +295,10 @@ class Orm } */ //$relationships = Util::merge($relationships, $convertedLink); - $relationships = Util::merge($convertedLink, $relationships); + if ($convertedLink !== false) { + $relationships = Util::merge($convertedLink, $relationships); + } + //echo $method.' = '.$currentType.' - '.$foreignLink['type'].'
'; } diff --git a/application/Espo/Core/Utils/Database/Converters/Relations.php b/application/Espo/Core/Utils/Database/Converters/Relations.php index 2319fb716e..78b3eb0dd6 100644 --- a/application/Espo/Core/Utils/Database/Converters/Relations.php +++ b/application/Espo/Core/Utils/Database/Converters/Relations.php @@ -148,10 +148,6 @@ class Relations ), ); - /*if (isset($params['link']['params']['foreign'])) { //??? - $relation[$params['entityName']] ['fields'] [$params['link']['name'].'Name'] ['foreign'] = $params['link']['params']['foreign']; - } */ - return $relation; } @@ -167,15 +163,13 @@ class Relations 'foreignType' => $foreignParams['link']['name'].'Type', //???: 'foreignKey' => $params['link']['name'].'Id', ), ), - ), + ), ); return $relation; } - //uses for belongsToParent and linkParent - //public function belongsToParent($params, $foreignParams) public function linkParent($params, $foreignParams) { $relation = array(); @@ -202,6 +196,27 @@ class Relations return $relation; } + + public function linkMultiple($params, $foreignParams) + { + return array( + $params['entityName'] => array ( + 'fields' => array( + $params['link']['name'].'Ids' => array( + 'type' => Entity::VARCHAR, + 'notStorable' => true, + ), + $params['link']['name'].'Names' => array( + 'type' => Entity::VARCHAR, + 'notStorable' => true, + ), + ), + ), + ); + } + + + public function teamRelation($params, $foreignParams) { return array( diff --git a/application/Espo/Core/Utils/Database/Converters/Schema.php b/application/Espo/Core/Utils/Database/Converters/Schema.php index e9fbbd89c8..b4e9c38038 100644 --- a/application/Espo/Core/Utils/Database/Converters/Schema.php +++ b/application/Espo/Core/Utils/Database/Converters/Schema.php @@ -54,7 +54,7 @@ class Schema //convertToSchema public function process(array $databaseMeta, $entityDefs) - { + { $schema = $this->getSchema(); $tables = array(); @@ -125,6 +125,7 @@ class Schema $relationParams['midKeys'] = array_merge($relationParams['midKeys'], array_keys($relationParams['conditions'])); } $tables[$tableName] = $this->prepareManyMany($entityName, $relationParams, $tables, false); + //$table->addForeignKeyConstraint($tables['Team'], array($usMidKey), array($relationKey), array("onUpdate" => "CASCADE")); } else { $tables[$tableName] = $this->prepareManyMany($entityName, $relationParams, $tables); } diff --git a/application/Espo/Resources/metadata/fields/linkMultiple.json b/application/Espo/Resources/metadata/fields/linkMultiple.json index 46200574e2..3cb9606194 100644 --- a/application/Espo/Resources/metadata/fields/linkMultiple.json +++ b/application/Espo/Resources/metadata/fields/linkMultiple.json @@ -15,8 +15,5 @@ "search":{ "basic":false, "advanced":true - }, - "database":{ - "skip":true } }