Skip to content
This repository was archived by the owner on Mar 1, 2018. It is now read-only.
This repository was archived by the owner on Mar 1, 2018. It is now read-only.

Fix: Check for local table when inserting the condition #8

Description

@joksnet

Suppose I have two tables File and Collection. A File can be included in N Collections. Both tables are loose-coupled with a Tags relation. So, when doing something like this:

Doctrine_Core::getTable('File')
  ->createQuery('f')
  ->innerJoin('f.collections c')
  ->leftJoin('f.Tags fto')
  ->leftJoin('c.Tags cto');

The SQL generated has in both cases, both relations.

Here is the patch I made:

--- a/LooseCoupling/lib/Doctrine/Template/Listener/LooselyCoupled.class.php
+++ b/LooseCoupling/lib/Doctrine/Template/Listener/LooselyCoupled.class.php
@@ -58,23 +58,17 @@ class LooselyCoupledListener extends Doctrine_Record_Listener
   public function preDqlSelect(Doctrine_Event $event)
   {
     $query = $event->getQuery();
-
-    $relations = array_keys($this->_options);
-
     $components = $this->_getDqlCallbackComponents($query);
-    $rootComponentName = false;
+   $rootComponentName = $event->getInvoker()->getTable()->getComponentName();
+
     foreach ($components as $alias => $component)
     {
-      if(!$rootComponentName)
-      {
-        $rootComponentName = $component['table']->getComponentName();
-      }
       if (isset($component['relation']))
       {
         foreach ($event->getInvoker()->getTable()->getRelations() as $relation)
         {
-          if ($component['table'] == $relation->getTable() && $relation->getTable()->hasTemplate('LooselyCoupleable'))
+          if ($component['relation']['localTable'] == $event->getInvoker()->getTable() && $component['table'] == $relation->getTable() && $relation->getTable()->hasTemplate('LooselyCoupleable'))
           {
             $query->addPendingJoinCondition($alias, $alias.'.obj_type = "'.$rootComponentName.'"');
             continue;
           }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions