public function getHasManyReference($nsTable, $key)
{
$candidates = $columnCandidates = [];
$targets = $this->structure->getHasManyReference($nsTable);
$table = preg_replace('#^(.*\\.)?(.*)$#', '$2', $nsTable);
foreach ($targets as $targetNsTable => $targetColumns) {
$targetTable = preg_replace('#^(.*\\.)?(.*)$#', '$2', $targetNsTable);
if (stripos($targetNsTable, $key) === FALSE) {
continue;
}
foreach ($targetColumns as $targetColumn) {
if (stripos($targetColumn, $table) !== FALSE) {
$columnCandidates[] = $candidate = [$targetNsTable, $targetColumn];
if (strcmp($targetTable, $key) === 0 || strcmp($targetNsTable, $key) === 0) {
return $candidate;
}
}
$candidates[] = [$targetTable, [$targetNsTable, $targetColumn]];
}
}
if (count($columnCandidates) === 1) {
return $columnCandidates[0];
} elseif (count($candidates) === 1) {
return $candidates[0][1];
}
foreach ($candidates as $candidate) {
if (strtolower($candidate[0]) === strtolower($key)) {
return $candidate[1];
}
}
if (!empty($candidates)) {
throw new AmbiguousReferenceKeyException('Ambiguous joining column in related call.');
}
if ($this->structure->isRebuilt()) {
return NULL;
}
$this->structure->rebuild();
return $this->getHasManyReference($nsTable, $key);
}