From 515beaea9b54627f33f58debfc8619be085f8d81 Mon Sep 17 00:00:00 2001 From: wei <> Date: Mon, 4 Feb 2008 11:09:46 +0000 Subject: Fix ActiveRecord: multiple FK referencing same table causes incorrect retrieval. --- .../Data/ActiveRecord/Relations/TActiveRecordRelation.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'framework/Data/ActiveRecord') 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); -- cgit v1.2.3