fix relation handling when source table equals foreign table
authorMichael Wallner <mike@php.net>
Fri, 8 May 2015 10:10:49 +0000 (12:10 +0200)
committerMichael Wallner <mike@php.net>
Fri, 8 May 2015 10:10:49 +0000 (12:10 +0200)
lib/pq/Gateway/Table.php

index e365e3772f836ce19a58bc8f2e7a788b07a4f4ee..2789963a35f2ee0ac38620820004a84d0979d35e 100644 (file)
@@ -405,14 +405,26 @@ class Table implements \SplSubject
                        if (!($relation instanceof Table\Reference)) {
                                $relation = static::resolve($relation)->getRelation($this->getName());
                        }
-                       $query->write("JOIN", $relation->foreignTable)->write("ON");
-                       foreach ($relation as $key => $ref) {
-                               $query->criteria(
-                                       array(
-                                               "{$relation->referencedTable}.{$ref}=" => 
-                                                       new QueryExpr("{$relation->foreignTable}.{$key}")
-                                       )
-                               );
+                       if ($this->getName() === $relation->foreignTable) {
+                               $query->write("JOIN", $relation->referencedTable)->write("ON");
+                               foreach ($relation as $key => $ref) {
+                                       $query->criteria(
+                                               array(
+                                                       "{$relation->referencedTable}.{$ref}=" => 
+                                                               new QueryExpr("{$relation->foreignTable}.{$key}")
+                                               )
+                                       );
+                               }
+                       } else {
+                               $query->write("JOIN", $relation->foreignTable)->write("ON");
+                               foreach ($relation as $key => $ref) {
+                                       $query->criteria(
+                                               array(
+                                                       "{$relation->referencedTable}.{$ref}=" => 
+                                                               new QueryExpr("{$relation->foreignTable}.{$key}")
+                                               )
+                                       );
+                               }
                        }
                }
                if ($where) {