Changeset 10901
- Timestamp:
- 08/14/08 21:07:49 (2 years ago)
- Files:
-
- plugins/DbFinderPlugin/lib/sfDoctrineFinder.php (modified) (9 diffs)
- plugins/DbFinderPlugin/lib/sfModelFinder.php (modified) (1 diff)
- plugins/DbFinderPlugin/lib/sfPropelFinder.php (modified) (1 diff)
- plugins/DbFinderPlugin/test/unit/sfDoctrineFinderInternalsTest.php (modified) (1 diff)
- plugins/DbFinderPlugin/test/unit/sfDoctrineFinderRelationsTest.php (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/DbFinderPlugin/lib/sfDoctrineFinder.php
r10845 r10901 16 16 $class = null, 17 17 $alias = null, 18 $aliases = array(), 18 19 $object = null, 19 20 $reinit = true, … … 25 26 $argNumber = 0, 26 27 $queryListener = null, 28 $relatedTables = array(), 27 29 $withClasses = array(), 28 30 $withColumns = array(), … … 39 41 public function setClass($class, $alias = '') 40 42 { 41 $this->add Relation($class, $alias);43 $this->addAlias($class, $alias); 42 44 $this->class = $class; 43 45 $this->alias = $alias; 44 46 $this->object = new $class(); 47 $this->relatedTables = array($this->object->getTable()); 45 48 $this->initialize($this->connection, $class, $alias); 46 49 … … 92 95 { 93 96 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); 94 126 } 95 127 … … 241 273 242 274 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;249 275 } 250 276 … … 979 1005 public function join() 980 1006 { 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 } 982 1036 983 1037 return $this; … … 1000 1054 } 1001 1055 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 class1009 * 1010 * @param string $ phpNameDoctrine_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') 1011 1065 * 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)); 1025 1101 } 1026 1102 … … 1048 1124 { 1049 1125 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 } 1050 1146 } 1051 1147 … … 1073 1169 } 1074 1170 } 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); 1082 1192 } 1083 1193 } plugins/DbFinderPlugin/lib/sfModelFinder.php
r10845 r10901 188 188 } 189 189 190 protected function getClassAndAlias($class)191 {192 if(strpos($class, ' ') !== false)193 {194 list($class, $alias) = explode(' ', $class);195 }196 else197 {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 207 190 protected static function getValueAndComparisonFromArguments($arguments = array()) 208 191 { plugins/DbFinderPlugin/lib/sfPropelFinder.php
r10845 r10901 1457 1457 } 1458 1458 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 1459 1476 protected function getColName($phpName, $peerClass = null, $withPeerClass = false, $autoAddJoin = true) 1460 1477 { plugins/DbFinderPlugin/test/unit/sfDoctrineFinderInternalsTest.php
r10845 r10901 71 71 } 72 72 } 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]'); 80 80 81 $finder = new myFinder(' Article b');81 $finder = new myFinder('DArticle b'); 82 82 $t->is($finder->getColName('b.Title'), 'b.title', 'getColName() recognizes [table alias].[column phpName]');

