Development

Changeset 10901

You must first sign up to be able to contribute.

Changeset 10901

Show
Ignore:
Timestamp:
08/14/08 21:07:49 (2 years ago)
Author:
francois
Message:

DbFinderPlugin Added support for sfDoctrineFinder::join() (WIP)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/DbFinderPlugin/lib/sfDoctrineFinder.php

    r10845 r10901  
    1616    $class         = null, 
    1717    $alias         = null, 
     18    $aliases       = array(), 
    1819    $object        = null, 
    1920    $reinit        = true, 
     
    2526    $argNumber     = 0, 
    2627    $queryListener = null, 
     28    $relatedTables = array(), 
    2729    $withClasses   = array(), 
    2830    $withColumns   = array(), 
     
    3941  public function setClass($class, $alias = '') 
    4042  { 
    41     $this->addRelation($class, $alias); 
     43    $this->addAlias($class, $alias); 
    4244    $this->class = $class; 
    4345    $this->alias = $alias; 
    4446    $this->object = new $class(); 
     47    $this->relatedTables = array($this->object->getTable()); 
    4548    $this->initialize($this->connection, $class, $alias); 
    4649     
     
    9295  { 
    9396    return $this->getQuery(); 
     97  } 
     98   
     99  protected function addAlias($class, $alias = '') 
     100  { 
     101    if(!$alias) list($class, $alias) = $this->getClassAndAlias($class); 
     102    $this->aliases[$class] = $alias; 
     103  } 
     104   
     105  /** 
     106   * $aliases property is a $class => $alias hash 
     107   */ 
     108  protected function getAlias($class) 
     109  { 
     110    if(array_key_exists($class, $this->aliases)) 
     111    { 
     112      return $this->aliases[$class]; 
     113    } 
     114     
     115    return $class; 
     116  } 
     117   
     118  protected function isAlias($alias) 
     119  { 
     120    return in_array($alias, array_values($this->aliases)); 
     121  } 
     122   
     123  protected function getClassFromAlias($alias) 
     124  { 
     125    return array_search($alias, $this->aliases); 
    94126  } 
    95127   
     
    241273     
    242274    return $this; 
    243   } 
    244    
    245   protected function addRelation($class, $alias = '') 
    246   { 
    247     if(!$alias) list($class, $alias) = $this->getClassAndAlias($class); 
    248     $this->relations[$alias] = $class; 
    249275  } 
    250276   
     
    9791005  public function join() 
    9801006  { 
    981     throw new Exception('This method is not yet implemented'); 
     1007    $args = func_get_args(); 
     1008    switch(count($args)) 
     1009    { 
     1010      case 0: 
     1011        throw new Exception('sfDoctrineFinder::join() expects at least one argument'); 
     1012        break; 
     1013      case 1: 
     1014      case 2: 
     1015        // $articleFinder->join('Comment') 
     1016        // $articleFinder->join('Category', 'RIGHT JOIN') 
     1017        list($relatedClass, $alias, $isTrueAlias) = $this->getClassAndAlias($args[0]); 
     1018        $relation = $this->findRelation($relatedClass); 
     1019        $operator = isset($args[1]) ? trim(str_replace('join', '', strtolower($args[1]))) : 'inner'; 
     1020        break; 
     1021      case 3: 
     1022        // $articleFinder->join('Article.CategoryId', 'Category.Id', 'RIGHT JOIN') 
     1023        $operator = trim(str_replace('join', '', strtolower($args[2]))); 
     1024        break; 
     1025    } 
     1026    $method = $operator . 'Join'; 
     1027    $startClass = $this->getAlias($relation->offsetGet('localTable')->getClassnameToReturn()); 
     1028    if($isTrueAlias) 
     1029    { 
     1030      $this->addAlias($startClass.'.'.$relation->getAlias(), $alias); 
     1031    } 
     1032    else 
     1033    { 
     1034      $this->addAlias($relation->getClass(), $startClass.'.'.$relation->getAlias()); 
     1035    } 
    9821036     
    9831037    return $this; 
     
    10001054  } 
    10011055   
    1002   protected function hasRelation($peerName
    1003   { 
    1004     throw new Exception('This method is not yet implemented'); 
    1005   } 
    1006    
    1007   /** 
    1008    * Guess the relation to another class 
    1009    * 
    1010    * @param string $phpName Doctrine_Record Class name (e.g. 'Article') 
     1056  protected function hasRelation($class
     1057  { 
     1058    return array_key_exists($class, $this->relations); 
     1059  } 
     1060   
     1061  /** 
     1062   * Finds a relation between two classes by introspection 
     1063   * 
     1064   * @param string $class Doctrine_Record Class name (e.g. 'Article') 
    10111065   *  
    1012    * @return array A list with the two columns member of the relationship 
    1013    */ 
    1014   public function getRelation($phpName) 
    1015   { 
    1016     throw new Exception('This method is not yet implemented'); 
    1017   } 
    1018    
    1019   /** 
    1020    * Finds a relation between two classes by introspection 
    1021    */ 
    1022   protected function findRelation($phpName, $peerClass) 
    1023   { 
    1024     throw new Exception('This method is not yet implemented'); 
     1066   * @return Relation A Doctrine relation object 
     1067   */ 
     1068  public function findRelation($class) 
     1069  { 
     1070    if($this->hasRelation($class)) 
     1071    { 
     1072      return $this->relation[$class]; 
     1073    } 
     1074    foreach($this->relatedTables as $table) 
     1075    { 
     1076      foreach ($table->getRelations() as $key => $relation) 
     1077      { 
     1078        if ($relation->getClass() == $class) 
     1079        { 
     1080          $this->relatedTables[]= Doctrine::getTable($class); 
     1081          $this->relations[$class] = $relation; 
     1082           
     1083          return $relation; 
     1084        } 
     1085      } 
     1086    } 
     1087    // You never know... try the relation alias rather than die 
     1088    foreach($this->relatedTables as $table) 
     1089    { 
     1090      if($table->hasRelation($class)) 
     1091      { 
     1092        $relation = $table->getRelation($class); 
     1093        $this->relatedTables[]= $relation->getTable(); 
     1094        $this->relations[$class] = $relation; 
     1095         
     1096        return $relation; 
     1097      } 
     1098    } 
     1099     
     1100    throw new Exception(sprintf('Cannot determine relation to class %s', $class)); 
    10251101  } 
    10261102   
     
    10481124  { 
    10491125    throw new Exception('This method is not yet implemented'); 
     1126  } 
     1127   
     1128  protected function getClassAndAlias($class) 
     1129  { 
     1130    if(strpos($class, ' ') !== false) 
     1131    { 
     1132      // true alias given by user 
     1133      list($class, $alias) = explode(' ', $class); 
     1134      return array($class, $alias, true); 
     1135    } 
     1136    else 
     1137    { 
     1138      // no alias, computing one 
     1139      $alias = strtolower(substr($class, 0, 1)); 
     1140      while(in_array($alias, $this->aliases)) 
     1141      { 
     1142        $alias .= '1'; 
     1143      } 
     1144      return array($class, $alias, false); 
     1145    } 
    10501146  } 
    10511147   
     
    10731169      } 
    10741170    } 
    1075     if(!array_key_exists($class, $this->relations)) 
    1076     { 
    1077       $relations = array_flip($this->relations); 
    1078       $class = $relations[$class]; 
    1079     } 
    1080      
    1081     return $class . '.' . self::underscore($phpName); 
     1171     
     1172    // class may be an alias 
     1173    if($this->isAlias($class)) 
     1174    { 
     1175      $alias = $class; 
     1176      $class = $this->getClassFromAlias($alias); 
     1177    } 
     1178    else 
     1179    { 
     1180      // class may have an alias 
     1181      $alias = $this->getAlias($class); 
     1182    } 
     1183     
     1184    // Auto guess join 
     1185    if($class != $this->class && !$this->hasRelation($class) && $autoAddJoin) 
     1186    { 
     1187      $this->join($class); 
     1188      $alias = $this->getAlias($class); 
     1189    } 
     1190     
     1191    return $alias . '.' . self::underscore($phpName); 
    10821192  } 
    10831193} 
  • plugins/DbFinderPlugin/lib/sfModelFinder.php

    r10845 r10901  
    188188  } 
    189189   
    190   protected function getClassAndAlias($class) 
    191   { 
    192     if(strpos($class, ' ') !== false) 
    193     { 
    194       list($class, $alias) = explode(' ', $class); 
    195     } 
    196     else 
    197     { 
    198       $alias = strtolower(substr($class, 0, 1)); 
    199       while(isset($this->relations[$alias])) 
    200       { 
    201         $alias .= '1'; 
    202       } 
    203     } 
    204     return array($class, $alias); 
    205   } 
    206    
    207190  protected static function getValueAndComparisonFromArguments($arguments = array()) 
    208191  { 
  • plugins/DbFinderPlugin/lib/sfPropelFinder.php

    r10845 r10901  
    14571457  } 
    14581458   
     1459  protected function getClassAndAlias($class) 
     1460  { 
     1461    if(strpos($class, ' ') !== false) 
     1462    { 
     1463      list($class, $alias) = explode(' ', $class); 
     1464    } 
     1465    else 
     1466    { 
     1467      $alias = strtolower(substr($class, 0, 1)); 
     1468      while(isset($this->relations[$alias])) 
     1469      { 
     1470        $alias .= '1'; 
     1471      } 
     1472    } 
     1473    return array($class, $alias); 
     1474  } 
     1475   
    14591476  protected function getColName($phpName, $peerClass = null, $withPeerClass = false, $autoAddJoin = true) 
    14601477  { 
  • plugins/DbFinderPlugin/test/unit/sfDoctrineFinderInternalsTest.php

    r10845 r10901  
    7171  } 
    7272} 
    73 $finder = new myFinder('Article'); 
    74 $t->is($finder->getColName('Title'), 'a.title', 'getColName() recognizes [column phpName]'); 
    75 $t->is($finder->getColName('Article_Title'), 'a.title', 'getColName() recognizes [table phpName]_[column phpName]'); 
    76 $t->is($finder->getColName('Article.Title'), 'a.title', 'getColName() recognizes [table phpName].[column phpName]'); 
    77 $t->is($finder->getColName('Article.CategoryId'), 'a.category_id', 'getColName() expects column names in CamelCase'); 
    78 $t->is($finder->getColName('Article.categoryId'), 'a.category_id', 'getColName() is tolerant over the first letter capitalization'); 
    79 $t->is($finder->getColName('a.Title'), 'a.title', 'getColName() recognizes [table alias].[column phpName]'); 
     73$finder = new myFinder('DArticle'); 
     74$t->is($finder->getColName('Title'), 'd.title', 'getColName() recognizes [column phpName]'); 
     75$t->is($finder->getColName('DArticle_Title'), 'd.title', 'getColName() recognizes [table phpName]_[column phpName]'); 
     76$t->is($finder->getColName('DArticle.Title'), 'd.title', 'getColName() recognizes [table phpName].[column phpName]'); 
     77$t->is($finder->getColName('DArticle.CategoryId'), 'd.category_id', 'getColName() expects column names in CamelCase'); 
     78$t->is($finder->getColName('DArticle.categoryId'), 'd.category_id', 'getColName() is tolerant over the first letter capitalization'); 
     79$t->is($finder->getColName('d.Title'), 'd.title', 'getColName() recognizes [table alias].[column phpName]'); 
    8080 
    81 $finder = new myFinder('Article b'); 
     81$finder = new myFinder('DArticle b'); 
    8282$t->is($finder->getColName('b.Title'), 'b.title', 'getColName() recognizes [table alias].[column phpName]'); 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting.
Sensio Labs also supports several large Open-Source projects.