[icinga-checkins] icinga.org: icinga-web/jmosshammer/default: * Extended Doctrine to support n-m relations without being forced to use the primary key

git at icinga.org git at icinga.org
Tue Jul 5 17:00:49 CEST 2011


Module: icinga-web
Branch: jmosshammer/default
Commit: 458841f1c52248e0cadb64110530a75827e80bd5
URL:    https://git.icinga.org/?p=icinga-web.git;a=commit;h=458841f1c52248e0cadb64110530a75827e80bd5

Author: Jannis Mosshammer <jannis.mosshammer at netways.de>
Date:   Tue Jul  5 17:00:03 2011 +0200

* Extended Doctrine to support n-m relations without being forced to use the primary key

---

 lib/doctrine/lib/Doctrine/Query.php    |   22 ++++++++++++++--------
 lib/doctrine/lib/Doctrine/Relation.php |   10 +++++++---
 2 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/lib/doctrine/lib/Doctrine/Query.php b/lib/doctrine/lib/Doctrine/Query.php
index 43934e0..ac7526f 100644
--- a/lib/doctrine/lib/Doctrine/Query.php
+++ b/lib/doctrine/lib/Doctrine/Query.php
@@ -610,7 +610,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
 
         foreach ($refs as $reference) {
             $reference = trim($reference);
-
+            
             if (empty($reference)) {
                 continue;
             }
@@ -665,6 +665,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
 
                 $this->_pendingFields[$componentAlias][] = $field;
             }
+            
         }
     }
 
@@ -1011,7 +1012,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
     protected function _buildSqlFromPart($ignorePending = false)
     {
         $q = '';
-
         foreach ($this->_sqlParts['from'] as $k => $part) {
             $e = explode(' ', $part);
 
@@ -1047,16 +1047,18 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
             if ( ! preg_match('/\bJOIN\b/i', $part) && ! isset($this->_pendingJoinConditions[$k])) {
                 $q .= ', ' . $part;
             } else {
+
                 if (substr($part, 0, 9) === 'LEFT JOIN') {
                     $aliases = array_merge($this->_subqueryAliases,
                                 array_keys($this->_neededTables));
-
+                    
                     if ( ! in_array($e[3], $aliases) && ! in_array($e[2], $aliases) && ! empty($this->_pendingFields)) {
                         continue;
                     }
 
                 }
 
+
                 if ( ! $ignorePending && isset($this->_pendingJoinConditions[$k])) {
                     if (strpos($part, ' ON ') !== false) {
                         $part .= ' AND ';
@@ -1065,11 +1067,13 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
                     }
 
                     $part .= $this->_processPendingJoinConditions($k);
+                
+
                 }
 
                 $componentAlias = $this->getComponentAlias($e[3]);
                 $string = $this->getInheritanceCondition($componentAlias);
-
+                
                 if ($string) {
                     $part = $part . ' AND ' . $string;
                 }
@@ -1141,6 +1145,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
      */
     public function buildSqlQuery($limitSubquery = true)
     {
+
+
         // reset the state
         if ( ! $this->isSubquery()) {
             $this->_queryComponents = array();
@@ -1789,10 +1795,10 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
                             . $this->_conn->quoteIdentifier($assocTableName)
                             . ' '
                             . $this->_conn->quoteIdentifier($assocAlias);
-
+                    $ref = $relation->__getForeignKey();
                     $queryPart .= ' ON (' . $this->_conn->quoteIdentifier($localAlias
                                 . '.'
-                                . $localTable->getColumnName($localTable->getIdentifier())) // what about composite keys?
+                                . $localTable->getColumnName($ref ? $ref : $localTable->getIdentifier())) // what about composite keys?
                                 . ' = '
                                 . $this->_conn->quoteIdentifier($assocAlias . '.' . $relation->getLocalRefColumnName());
 
@@ -1807,7 +1813,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
                     }
 
                     $queryPart .= ')';
-
+                    
                     $this->_sqlParts['from'][] = $queryPart;
 
                     $queryPart = $join . $foreignSql;
@@ -2227,4 +2233,4 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
         $this->_parsers = array();
         $this->_dqlParts = array();
     }
-}
\ No newline at end of file
+}
diff --git a/lib/doctrine/lib/Doctrine/Relation.php b/lib/doctrine/lib/Doctrine/Relation.php
index e139a76..0d7ccac 100644
--- a/lib/doctrine/lib/Doctrine/Relation.php
+++ b/lib/doctrine/lib/Doctrine/Relation.php
@@ -71,8 +71,9 @@ abstract class Doctrine_Relation implements ArrayAccess
                                   'owningSide'  => false, // whether this is the owning side
                                   'refClassRelationAlias' => null,
                                   'foreignKeyName' => null,
-                                  'orderBy' => null
-                                  );
+                                  'orderBy' => null,
+                                  'foreignKey' => null 
+                                    );
 
     protected $_isRefClass = null;
 
@@ -326,6 +327,9 @@ abstract class Doctrine_Relation implements ArrayAccess
        return $this->definition['table']->getColumnName($this->definition['foreign']);
     }
 
+    final public function __getForeignKey() {
+        return $this->definition["foreignKey"];
+    }
     /**
      * isOneToOne
      * returns whether or not this relation is a one-to-one relation
@@ -453,4 +457,4 @@ abstract class Doctrine_Relation implements ArrayAccess
         $r[] = "</pre>";
         return implode("\n", $r);
     }
-}
\ No newline at end of file
+}





More information about the icinga-checkins mailing list