summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--framework/Data/ActiveRecord/Relations/TActiveRecordRelation.php11
1 files changed, 7 insertions, 4 deletions
diff --git a/framework/Data/ActiveRecord/Relations/TActiveRecordRelation.php b/framework/Data/ActiveRecord/Relations/TActiveRecordRelation.php
index 4c238801..300dc26c 100644
--- a/framework/Data/ActiveRecord/Relations/TActiveRecordRelation.php
+++ b/framework/Data/ActiveRecord/Relations/TActiveRecordRelation.php
@@ -113,16 +113,19 @@ abstract class TActiveRecordRelation
$tableInfo=$from;
if($from instanceof TActiveRecord)
$tableInfo = $gateway->getRecordTableInfo($from);
+ //find first non-empty FK
foreach($tableInfo->getForeignKeys() as $fkeys)
{
if(strtolower($fkeys['table'])===strtolower($matchingTableName))
{
- if(!$loose && $this->getContext()->hasFkField())
- return $this->getFkFields($fkeys['keys']);
- else
- return $fkeys['keys'];
+ $hasFkField = !$loose && $this->getContext()->hasFkField();
+ $key = $hasFkField ? $this->getFkFields($fkeys['keys']) : $fkeys['keys'];
+ if(!empty($key))
+ return $key;
}
}
+
+ //none found
$matching = $gateway->getRecordTableInfo($matchesRecord)->getTableFullName();
throw new TActiveRecordException('ar_relations_missing_fk',
$tableInfo->getTableFullName(), $matching);