add foreign (field) option for ormMetadata for 'foreign' type

This commit is contained in:
Taras Machyshyn
2013-12-19 17:55:42 +02:00
parent 2f455cc654
commit edbf579e47
6 changed files with 49 additions and 6 deletions
@@ -15,7 +15,7 @@ class Links
{
$this->metadata = $metadata;
$this->relations = new Relations();
$this->relations = new Relations($this->metadata);
}
protected function getMetadata()
@@ -29,6 +29,10 @@ class Orm
protected $fieldAccordances = array(
'type' => 'type',
'maxLength' => 'len',
'autoincrement' => 'autoincrement',
'notStorable' => 'notStorable',
'link' => 'relation',
'field' => 'foreign', //todo change "foreign" to "field"
'default' => array(
'condition' => '^javascript:',
'conditionEquals' => false,
@@ -8,6 +8,19 @@ use Espo\Core\Utils\Util,
class Relations
{
private $metadata;
public function __construct(\Espo\Core\Utils\Metadata $metadata)
{
$this->metadata = $metadata;
}
protected function getMetadata()
{
return $this->metadata;
}
protected function getSortEntities($entity1, $entity2)
{
$entities = array(
@@ -20,6 +33,7 @@ class Relations
return $entities;
}
protected function getJoinTable($tableName1, $tableName2)
{
$tables = $this->getSortEntities($tableName1, $tableName2);
@@ -28,6 +42,25 @@ class Relations
}
protected function getForeignField($name, $entityName)
{
$foreignField = $this->getMetadata()->get('entityDefs.'.$entityName.'.fields.'.$name);
if ($foreignField['type'] != Entity::VARCHAR) {
$fieldDefs = $this->getMetadata()->get('fields.'.$foreignField['type']);
$naming = isset($fieldDefs['naming']) ? $fieldDefs['naming'] : 'postfix';
if (isset($fieldDefs['actualFields']) && is_array($fieldDefs['actualFields'])) {
$foreignFieldArray = array();
foreach($fieldDefs['actualFields'] as $fieldName) {
$foreignFieldArray[] = Util::getNaming($name, $fieldName, $naming);
}
return $foreignFieldArray;
}
}
return $name;
}
//todo sedine in foreign fieldDefs a key for current
@@ -96,6 +129,7 @@ class Relations
'type' => Entity::FOREIGN,
'relation' => $params['link']['name'],
'notStorable' => true,
'foreign' => $this->getForeignField('name', $foreignParams['entityName']),
),
$params['link']['name'].'Id' => array(
'type' => Entity::FOREIGN_ID,
@@ -112,9 +146,9 @@ class Relations
),
);
if (isset($params['link']['params']['foreign'])) { //???
/*if (isset($params['link']['params']['foreign'])) { //???
$relation[$params['entityName']] ['fields'] [$params['link']['name'].'Name'] ['foreign'] = $params['link']['params']['foreign'];
}
} */
return $relation;
}
+1 -1
View File
@@ -207,7 +207,7 @@ class Metadata
$espoMetadataFile = Util::concatPath($this->getMetaConfig()->cachePath, 'ormMetadata.php');
if (!file_exists($espoMetadataFile)) {
if (!file_exists($espoMetadataFile) || !$this->getConfig()->get('useCache')) {
$this->getConverter()->process();
}
@@ -0,0 +1,5 @@
{
"database":{
"notStorable":true
}
}
@@ -6,10 +6,10 @@
],
"fields":{
"salutation":{
"type":"varchar"
"type":"enum"
},
"first":{
"type":"enum"
"type":"varchar"
},
"last":{
"type":"varchar"