Skip to content

Commit

Permalink
NTDB join reference edit, after making JOIN or calling ref() on Activ…
Browse files Browse the repository at this point in the history
…eRow, it does lookup for foreign key column name in referenced table, not just primary key of referenced table
  • Loading branch information
apincik committed Dec 30, 2015
1 parent 6dd5dda commit ec9ef62
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 104 deletions.
16 changes: 11 additions & 5 deletions src/Database/Conventions/DiscoveredConventions.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@ public function getPrimary($table)
}


public function getForeign($table, $key)
{
return $this->structure->getForeignKey($table, $key);
}


public function getHasManyReference($nsTable, $key)
{
$candidates = $columnCandidates = [];
$candidates = $columnCandidates = array();
$targets = $this->structure->getHasManyReference($nsTable);
$table = preg_replace('#^(.*\.)?(.*)$#', '$2', $nsTable);

Expand All @@ -46,13 +52,13 @@ public function getHasManyReference($nsTable, $key)

foreach ($targetColumns as $targetColumn) {
if (stripos($targetColumn, $table) !== FALSE) {
$columnCandidates[] = $candidate = [$targetNsTable, $targetColumn];
$columnCandidates[] = $candidate = array($targetNsTable, $targetColumn);
if (strcmp($targetTable, $key) === 0 || strcmp($targetNsTable, $key) === 0) {
return $candidate;
}
}

$candidates[] = [$targetTable, [$targetNsTable, $targetColumn]];
$candidates[] = array($targetTable, array($targetNsTable, $targetColumn));
}
}

Expand Down Expand Up @@ -86,8 +92,8 @@ public function getBelongsToReference($table, $key)
$tableColumns = $this->structure->getBelongsToReference($table);

foreach ($tableColumns as $column => $targetTable) {
if (stripos($column, $key) !== FALSE) {
return [$targetTable, $column];
if (stripos($column, $key) !== FALSE || stripos($targetTable, $key) !== FALSE) { //@TODO not official code
return array($targetTable, $column);
}
}

Expand Down
23 changes: 18 additions & 5 deletions src/Database/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ public function getPrimaryKey($table)
return $this->structure['primary'][$table];
}

//@NOTE not official code
//Get foreign keys by table and return local column name in referenced table.
//@TODO cache getForeignKeys($table) call
public function getForeignKey($table, $column)
{
foreach($this->connection->getSupplementalDriver()->getForeignKeys($table) as $row) {
if($row["local"] == $column) {
return $row["foreign"];
}
}

return NULL;
}

public function getPrimaryKeySequence($table)
{
Expand Down Expand Up @@ -105,7 +118,7 @@ public function getHasManyReference($table, $targetTable = NULL)

} else {
if (!isset($this->structure['hasMany'][$table])) {
return [];
return array();
}
return $this->structure['hasMany'][$table];
}
Expand All @@ -126,7 +139,7 @@ public function getBelongsToReference($table, $column = NULL)

} else {
if (!isset($this->structure['belongsTo'][$table])) {
return [];
return array();
}
return $this->structure['belongsTo'][$table];
}
Expand All @@ -152,7 +165,7 @@ protected function needStructure()
return;
}

$this->structure = $this->cache->load('structure', [$this, 'loadStructure']);
$this->structure = $this->cache->load('structure', array($this, 'loadStructure'));
}


Expand All @@ -163,7 +176,7 @@ public function loadStructure()
{
$driver = $this->connection->getSupplementalDriver();

$structure = [];
$structure = array();
$structure['tables'] = $driver->getTables();

foreach ($structure['tables'] as $tablePair) {
Expand Down Expand Up @@ -198,7 +211,7 @@ public function loadStructure()

protected function analyzePrimaryKey(array $columns)
{
$primary = [];
$primary = array();
foreach ($columns as $column) {
if ($column['primary']) {
$primary[] = $column['name'];
Expand Down
Loading

0 comments on commit ec9ef62

Please sign in to comment.