Changeset 10342
- Timestamp:
- 07/17/08 16:17:07 (4 months ago)
- Files:
-
- plugins/sfPropelFinderPlugin/README (modified) (2 diffs)
- plugins/sfPropelFinderPlugin/lib/DbFinder.php (modified) (2 diffs)
- plugins/sfPropelFinderPlugin/lib/sfDoctrineFinder.php (modified) (4 diffs)
- plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php (modified) (7 diffs)
- plugins/sfPropelFinderPlugin/test/unit/sfDoctrineFinderInternalsTest.php (modified) (2 diffs)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderInternalsTest.php (modified) (2 diffs)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderRelationsTest.php (modified) (1 diff)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelFinderPlugin/README
r10341 r10342 550 550 }}} 551 551 552 === Using Class Shortcuts === 553 554 {{{ 555 $article = sfPropelFinder::from('Article a')-> 556 where('a.Title', 'foo')-> 557 findOne(); 558 // same as 559 $article = sfPropelFinder::from('Article')-> 560 where('Article.Title', 'foo')-> 561 findOne(); 562 }}} 563 552 564 === Hacking the finder === 553 565 … … 591 603 === 2008-07-17 | Trunk === 592 604 605 * francois: Added preliminary support for table aliases (`from('Article a')`) in Doctrine and Propel finders 593 606 * francois: Implemented `sfDoctrineFinder::findOne()`, `findFirst()`, `findLast()` and `orderBy()` 594 607 * francois: Initialized `DbFinder` and `sfDoctrineFinder` (WIP) plugins/sfPropelFinderPlugin/lib/DbFinder.php
r10213 r10342 14 14 const 15 15 DOCTRINE = "Doctrine", 16 PROPEL = "Propel"; 16 PROPEL = "Propel"; 17 18 protected $relations = array(); 17 19 18 20 // Finder Initializers … … 171 173 } 172 174 175 protected function getClassAndAlias($class) 176 { 177 if(strpos($class, ' ') !== false) 178 { 179 list($class, $alias) = explode(' ', $class); 180 } 181 else 182 { 183 $alias = strtolower(substr($class, 0, 1)); 184 while(isset($this->relations[$alias])) 185 { 186 $alias .= '1'; 187 } 188 } 189 return array($class, $alias); 190 } 173 191 } plugins/sfPropelFinderPlugin/lib/sfDoctrineFinder.php
r10341 r10342 14 14 protected $class = null; 15 15 protected $object = null; 16 protected $relations = null;17 16 protected $latestQuery = ''; 18 17 protected $withClasses = array(); … … 22 21 public function __construct($class = '') 23 22 { 24 $this->query = Doctrine_Query::create()->from($class); 23 list($class, $alias) = $this->getClassAndAlias($class); 24 $this->query = Doctrine_Query::create()->from($class . ' ' . $alias); 25 25 if($class) 26 26 { 27 $this-> class = $class;27 $this->setClass($class, $alias); 28 28 $this->object = new $class(); 29 29 } … … 35 35 } 36 36 37 public function setClass($class )38 { 39 $this-> relations[]= $class;37 public function setClass($class, $alias = '') 38 { 39 $this->addRelation($class, $alias); 40 40 $this->class = $class; 41 41 42 42 return $this; 43 } 44 45 protected function addRelation($class, $alias = '') 46 { 47 if(!$alias) list($class, $alias) = $this->getClassAndAlias($class); 48 $this->relations[$alias] = $class; 43 49 } 44 50 … … 445 451 } 446 452 } 447 448 return self::underscore($phpName); 453 if(!array_key_exists($class, $this->relations)) 454 { 455 $relations = array_flip($this->relations); 456 $class = $relations[$class]; 457 } 458 459 return $class . '.' . self::underscore($phpName); 449 460 } 450 461 plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php
r10167 r10342 10 10 */ 11 11 12 class sfPropelFinder 12 class sfPropelFinder extends DbFinder 13 13 { 14 14 protected $class = null; … … 16 16 protected $databaseMap = null; 17 17 protected $criteria = null; 18 protected $relations = null;19 18 protected $latestQuery = ''; 20 19 protected $criterions = array(); … … 28 27 if($class) 29 28 { 29 list($class, $alias) = $this->getClassAndAlias($class); 30 30 $this->class = $class; 31 31 $tmp = new $class; 32 $this->setPeerClass(get_class($tmp->getPeer()) );32 $this->setPeerClass(get_class($tmp->getPeer()), $alias); 33 33 } 34 34 if($this->peerClass) … … 50 50 } 51 51 52 public function setPeerClass($peerClass )53 { 54 $this-> relations[]= $peerClass;52 public function setPeerClass($peerClass, $alias = '') 53 { 54 $this->addRelation($peerClass, $alias); 55 55 $this->peerClass = $peerClass; 56 56 57 57 return $this; 58 } 59 60 protected function addRelation($peerClass, $alias = '') 61 { 62 if(!$alias) list($class, $alias) = $this->getClassAndAlias($peerClass); 63 $this->relations[$alias] = $peerClass; 58 64 } 59 65 … … 998 1004 // $articleFinder->join('Comment') 999 1005 // $articleFinder->join('Category', 'RIGHT JOIN') 1000 $relatedClass = $args[0];1006 list($relatedClass, $alias) = $this->getClassAndAlias($args[0]); 1001 1007 list($column1, $column2) = $this->getRelation($relatedClass); 1002 $this-> relations[]= sfPropelFinderUtils::getPeerClassFromClass($relatedClass);1008 $this->addRelation(sfPropelFinderUtils::getPeerClassFromClass($relatedClass), $alias); 1003 1009 $operator = isset($args[1]) ? $args[1] : null; 1004 1010 break; … … 1009 1015 if($peerClass1 != $this->peerClass && !$this->hasRelation($peerClass1)) 1010 1016 { 1011 $this->relations []= $peerClass1; 1017 list($peerClass1, $alias) = $this->getClassAndAlias($peerClass1); 1018 $this->addRelation($peerClass1, $alias); 1012 1019 } 1013 1020 list($peerClass2, $column2) = $this->getColName($column2, $peerClass = null, $withPeerClass = true, $autoAddJoin = false); 1014 1021 if($peerClass2 != $this->peerClass && !$this->hasRelation($peerClass2)) 1015 1022 { 1016 $this->relations []= $peerClass2; 1023 list($peerClass2, $alias) = $this->getClassAndAlias($peerClass2); 1024 $this->addRelation($peerClass2, $alias); 1017 1025 } 1018 1026 break; … … 1119 1127 // Table.Column 1120 1128 list($class, $phpName) = explode('.', $phpName); 1121 $peerClass = sfPropelFinderUtils::getPeerClassFromClass($class); 1129 if(array_key_exists($class, $this->relations)) 1130 { 1131 $peerClass = $this->relations[$class]; 1132 } 1133 else 1134 { 1135 $peerClass = sfPropelFinderUtils::getPeerClassFromClass($class); 1136 } 1122 1137 } 1123 1138 else if(strpos($phpName, '_') !== false) plugins/sfPropelFinderPlugin/test/unit/sfDoctrineFinderInternalsTest.php
r10341 r10342 60 60 $databaseManager->initialize(); 61 61 62 $t = new lime_test( 3, new lime_output_color());62 $t = new lime_test(5, new lime_output_color()); 63 63 64 64 $t->diag('getColName()'); … … 72 72 } 73 73 $finder = new myFinder('Article'); 74 $t->is($finder->getColName('Title'), 'title', 'getColName() recognizes [column phpName]'); 75 $t->is($finder->getColName('Article_Title'), 'title', 'getColName() recognizes [table phpName]_[column phpName]'); 76 $t->is($finder->getColName('Article.Title'), 'title', 'getColName() recognizes [table phpName].[column phpName]'); 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('a.Title'), 'a.title', 'getColName() recognizes [table alias].[column phpName]'); 78 79 $finder = new myFinder('Article b'); 80 $t->is($finder->getColName('b.Title'), 'b.title', 'getColName() recognizes [table alias].[column phpName]'); plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderInternalsTest.php
r10121 r10342 65 65 ArticlePeer::doDeleteAll(); 66 66 67 $t = new lime_test( 3, new lime_output_color());67 $t = new lime_test(5, new lime_output_color()); 68 68 69 69 $t->diag('getColName()'); … … 80 80 $t->is($finder->getColName('Article_Title'), 'article.TITLE', 'getColName() recognizes [table phpName]_[column phpName]'); 81 81 $t->is($finder->getColName('Article.Title'), 'article.TITLE', 'getColName() recognizes [table phpName].[column phpName]'); 82 83 $t->is($finder->getColName('a.Title'), 'article.TITLE', 'getColName() recognizes [table alias].[column phpName]'); 84 85 $finder = new myFinder('Article b'); 86 $t->is($finder->getColName('b.Title'), 'article.TITLE', 'getColName() recognizes [table alias].[column phpName]'); plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderRelationsTest.php
r10167 r10342 101 101 } 102 102 103 104 103 ClubPeer::doDeleteAll(); 105 104 PersonPeer::doDeleteAll(); plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php
r10166 r10342 65 65 ArticlePeer::doDeleteAll(); 66 66 67 $t = new lime_test(11 3, new lime_output_color());67 $t = new lime_test(115, new lime_output_color()); 68 68 69 69 $t->diag('find()'); … … 399 399 $article = sfPropelFinder::from('Article')->where('Article_Title', 'abc')->findOne(); 400 400 $t->is($article->getId(), $article1->getId(), 'where() accepts a complete column name ClassName_ColumnName'); 401 $article = sfPropelFinder::from('Article')->where('a.Title', 'abc')->findOne(); 402 $t->is($article->getId(), $article1->getId(), 'where() accepts a complete column name ClassShortcut.ColumnName'); 403 $article = sfPropelFinder::from('Article b')->where('b.Title', 'abc')->findOne(); 404 $t->is($article->getId(), $article1->getId(), 'where() accepts a complete column name ClassAlias.ColumnName'); 401 405 $article = sfPropelFinder::from('Article')->where('Title', 'def')->findOne(); 402 406 $t->is($article->getId(), $article2->getId(), 'where() adds a WHERE condition on the column given as first argument');