Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NTDB joining tables on different key as primary key #114

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Database/Conventions/DiscoveredConventions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public function getPrimary($table)
{
return $this->structure->getPrimaryKey($table);
}

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


public function getHasManyReference($nsTable, $key)
Expand Down Expand Up @@ -86,7 +91,7 @@ public function getBelongsToReference($table, $key)
$tableColumns = $this->structure->getBelongsToReference($table);

foreach ($tableColumns as $column => $targetTable) {
if (stripos($column, $key) !== FALSE) {
if (stripos($column, $key) !== FALSE || stripos($targetTable, $key) !== FALSE) { //also check if any reference exists
return [$targetTable, $column];
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/Database/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,22 @@ public function getPrimaryKey($table)

return $this->structure['primary'][$table];
}


//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)
{
$this->needStructure();
Expand Down
14 changes: 10 additions & 4 deletions src/Database/Table/Selection.php
Original file line number Diff line number Diff line change
Expand Up @@ -869,13 +869,19 @@ public function getReferencedTable(ActiveRow $row, $table, $column = NULL)

if ($cacheKeys) {
$selection = $this->createSelectionInstance($table);
$selection->where($selection->getPrimary(), array_keys($cacheKeys));

//search for foreign key column name which is referenced from activeRow parent table to table from which is selection made
$foreign = $this->conventions->getForeign($row->getTable()->getName(), $column);
$primary = $foreign == NULL ? $selection->getPrimary() : $foreign;

$selection->where($primary, array_keys($cacheKeys));
} else {
$selection = [];
$selection = NULL;
}
}

return isset($selection[$checkPrimaryKey]) ? $selection[$checkPrimaryKey] : NULL;

return $selection;
//return isset($selection[$checkPrimaryKey]) ? $selection[$checkPrimaryKey] : NULL;
}


Expand Down
3 changes: 2 additions & 1 deletion src/Database/Table/SqlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,8 @@ public function parseJoinsCb(& $joins, $match)
throw new Nette\InvalidArgumentException("No reference found for \${$parent}->{$keyMatch['key']}.");
}
list($table, $column) = $belongsTo;
$primary = $this->conventions->getPrimary($table);
$foreign = $this->conventions->getForeign($parent, $column); //check for foreign key instead primary key in referenced table
$primary = $foreign == NULL ? $this->conventions->getPrimary($table) : $foreign;
}

$tableAlias = $keyMatch['key'] ?: preg_replace('#^(.*\.)?(.*)$#', '$2', $table);
Expand Down