Changeset 10112
- Timestamp:
- 07/03/08 23:12:40 (5 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelFinderPlugin/README
r10101 r10112 95 95 find(); 96 96 97 // The where() method accepts simple or composed column names ('ClassName _ColumnName')98 $articles = $articleFinder->where('Article _Title', 'foo')->find();97 // The where() method accepts simple or composed column names ('ClassName.ColumnName') 98 $articles = $articleFinder->where('Article.Title', 'foo')->find(); 99 99 // You can also use the magic whereXXX() method, removing the column argument and concatenating it to the method name 100 100 $articles = $articleFinder->whereTitle('foo')->find(); … … 205 205 $article = sfPropelFinder::from('Article')-> 206 206 join('Comment')-> 207 where('Comment _Content', 'You rock!')->207 where('Comment.Content', 'You rock!')-> 208 208 findOne(); 209 209 // You can chain joins if you want to make it more complex … … 222 222 join('Comment')-> 223 223 join('Author')-> 224 where('Author _Name', 'John')->224 where('Author.Name', 'John')-> 225 225 findOne(); 226 226 // You can also use the magic joinXXX() method … … 235 235 $comment = sfPropelFinder::from('Comment')-> 236 236 join('Article')-> 237 where('Article _Title', 'Hello, world')->237 where('Article.Title', 'Hello, world')-> 238 238 findOne(); 239 239 $article = $comment->getArticle(); // Needs another database query … … 246 246 $comment = sfPropelFinder::from('Comment')->with('Article')-> 247 247 join('Article')-> 248 where('Article _Title', 'Hello, world')->248 where('Article.Title', 'Hello, world')-> 249 249 findOne(); 250 250 $article = $comment->getArticle(); // Same result, with no supplementary query … … 314 314 $article = sfPropelFinder::from('Article')-> 315 315 join('Category')-> 316 withColumn('Category _Name')->317 findOne(); 318 $categoryName = $article->getColumn('Category _Name'); // No supplementary query316 withColumn('Category.Name')-> 317 findOne(); 318 $categoryName = $article->getColumn('Category.Name'); // No supplementary query 319 319 320 320 // Beware that in this case, the related `Category` object is not hydrated, since `with()` was not used. … … 325 325 // Just like with(), withColumn() adds an internal join if you don't do it yourself 326 326 $article = sfPropelFinder::from('Article')-> 327 withColumn('Category _Name')->328 findOne(); 329 $categoryName = $article->getColumn('Category _Name'); // Works without a call to `join('Category')`327 withColumn('Category.Name')-> 328 findOne(); 329 $categoryName = $article->getColumn('Category.Name'); // Works without a call to `join('Category')` 330 330 331 331 // withColumn() can use a column alias as second argument. 332 332 $article = sfPropelFinder::from('Article')-> 333 333 join('Category')-> 334 withColumn('Category _Name', 'category')->334 withColumn('Category.Name', 'category')-> 335 335 findOne(); 336 336 $categoryName = $article->getColumn('category'); … … 347 347 $article = sfPropelFinder::from('Article')-> 348 348 join('Category')-> 349 withColumn('Category _CreatedAt', 'CategoryCreatedAt', 'Timestamp')->349 withColumn('Category.CreatedAt', 'CategoryCreatedAt', 'Timestamp')-> 350 350 findOne(); 351 351 $categoryName = $article->getColumn('CategoryCreatedAt'); … … 483 483 === 2008-07-03 | Trunk === 484 484 485 * francois: Preferring `ClassName.ColumnName` over `ClassName.ColumnName` for complete column names 485 486 * francois: Added Propel 1.3 compatibility 486 487 * francois: Added `sfPropelFinder::set()` method (based on a patch by jug) plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php
r10102 r10112 505 505 } 506 506 // Then the columns added one by one by way of 'withColumn' 507 foreach($this->getWithColumns() as $ name=> $column)507 foreach($this->getWithColumns() as $alias => $column) 508 508 { 509 509 // Additional columns are stored in the object, in a special 'namespace' 510 510 // see getColumn() for how to retrieve the value afterwards 511 $asName = 'WithColumn'.$name;512 511 // Using the third parameter of withColumn() as a type. defaults to $rs->get() (= $rs->getString()) 513 512 $typedGetter = 'get'.ucfirst($column['type']); 514 513 if($propelVersion == '1.2') 515 514 { 516 $ obj->$asName = $resultSet->$typedGetter($startCol);515 $this->setColumn($obj, $alias, $resultSet->$typedGetter($startCol)); 517 516 } 518 517 else 519 518 { 520 $ obj->$asName = $row[$startCol];519 $this->setColumn($obj, $alias, $row[$startCol]); 521 520 } 522 521 $startCol++; … … 564 563 } 565 564 // Then the columns added one by one by way of 'withColumn' 566 foreach($this->getWithColumns() as $ name=> $column)565 foreach($this->getWithColumns() as $alias => $column) 567 566 { 568 567 // if the column is on a related object property … … 575 574 $this->relations[]= $peerClass; 576 575 } 577 $c->addAsColumn( $name, $column['column']);576 $c->addAsColumn('\''.$alias.'\'', $column['column']); 578 577 } 579 578 … … 1006 1005 $tmp = new $class(); 1007 1006 return get_class($tmp->getPeer()); 1007 $tmp->__destroy(); 1008 1008 } 1009 1009 … … 1018 1018 * Requires symfony and sfMixer enabled 1019 1019 */ 1020 public function getColumn($object, $arguments) 1021 { 1022 $asName = 'WithColumn'.$arguments; 1023 1024 return $object->$asName; 1020 public function getColumn($object, $alias) 1021 { 1022 $alias = 'a'.md5($alias); 1023 return $object->$alias; 1024 } 1025 1026 public function setColumn($object, $alias, $value) 1027 { 1028 $alias = 'a'.md5($alias); 1029 $object->$alias = $value; 1025 1030 } 1026 1031 … … 1050 1055 return $phpName; 1051 1056 } 1052 if(strpos($phpName, '_') !== false) 1053 { 1054 list($class, $phpName) = split('_', $phpName); 1057 if(strpos($phpName, '.') !== false) 1058 { 1059 // Table.Column 1060 list($class, $phpName) = explode('.', $phpName); 1055 1061 $peerClass = $this->getPeerClassFromClass($class); 1056 1062 } 1063 else if(strpos($phpName, '_') !== false) 1064 { 1065 // Table_Column, or Table_Name_Column, so explode is not a solution here 1066 $limit = strrpos($phpName, '_'); 1067 $class = substr($phpName, 0, $limit); 1068 $phpName = substr($phpName, $limit + 1); 1069 $peerClass = $this->getPeerClassFromClass($class); 1070 } 1057 1071 if(!$peerClass) 1058 1072 { 1073 // Column 1059 1074 $peerClass = $this->peerClass; 1060 1075 } plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php
r10101 r10112 65 65 ArticlePeer::doDeleteAll(); 66 66 67 $t = new lime_test(14 1, new lime_output_color());67 $t = new lime_test(142, new lime_output_color()); 68 68 69 69 $t->diag('find()'); … … 358 358 $t->pass('where() throws an exception when the column is not found'); 359 359 } 360 $article = sfPropelFinder::from('Article')->where('Article.Title', 'abc')->findOne(); 361 $t->is($article->getId(), $article1->getId(), 'where() accepts a complete column name ClassName.ColumnName'); 360 362 $article = sfPropelFinder::from('Article')->where('Article_Title', 'abc')->findOne(); 361 363 $t->is($article->getId(), $article1->getId(), 'where() accepts a complete column name ClassName_ColumnName'); … … 464 466 $finder = sfPropelFinder::from('Article')-> 465 467 joinCategory()-> 466 where('Category _Name', 'foo')->467 _or('Category _Name', 'bar');468 where('Category.Name', 'foo')-> 469 _or('Category.Name', 'bar'); 468 470 $finder->find(); 469 471 $t->is( … … 476 478 joinArticle()-> 477 479 joinAuthor()-> 478 where('Article _Title', 'foo')->479 _or('Author _Name', 'bar');480 where('Article.Title', 'foo')-> 481 _or('Author.Name', 'bar'); 480 482 $finder->find(); 481 483 $t->is( … … 622 624 $article3->setCategory($category2); 623 625 $article3->save(); 624 $nbArticles = sfPropelFinder::from('Article')->join('Category')->where('Category _Name', 'cat1')->count();626 $nbArticles = sfPropelFinder::from('Article')->join('Category')->where('Category.Name', 'cat1')->count(); 625 627 $t->is($nbArticles, 2, 'join() allows to join to another table (many-to-one)'); 626 $nbArticles = sfPropelFinder::from('Article')->join('Category')->where('Category _Name', 'cat2')->count();628 $nbArticles = sfPropelFinder::from('Article')->join('Category')->where('Category.Name', 'cat2')->count(); 627 629 $t->is($nbArticles, 1, 'join() allows to join to another table (many-to-one)'); 628 $article = sfPropelFinder::from('Article')->join('Category')->where('Category _Name', 'cat2')->findOne();630 $article = sfPropelFinder::from('Article')->join('Category')->where('Category.Name', 'cat2')->findOne(); 629 631 $t->is($article->getTitle(), 'ccccc', 'join() allows to join to another table (many-to-one)'); 630 632 ArticlePeer::doDeleteAll(); … … 642 644 $comment->setArticleId($article2->getId()); 643 645 $comment->save(); 644 $nbArticles = sfPropelFinder::from('Article')->join('Comment')->where('Comment _Content', 'foo')->count();646 $nbArticles = sfPropelFinder::from('Article')->join('Comment')->where('Comment.Content', 'foo')->count(); 645 647 $t->is($nbArticles, 1, 'join() allows to join to another table (one-to-many)'); 646 $article = sfPropelFinder::from('Article')->join('Comment')->where('Comment _Content', 'foo')->findOne();648 $article = sfPropelFinder::from('Article')->join('Comment')->where('Comment.Content', 'foo')->findOne(); 647 649 $t->is($article->getTitle(), 'bbbbb', 'join() allows to join to another table (one-to-many)'); 648 650 … … 662 664 $comment->setAuthor($author1); 663 665 $comment->save(); 664 $article = sfPropelFinder::from('Article')->join('Comment')->join('Author')->where('Author _Name', 'John')->findOne();666 $article = sfPropelFinder::from('Article')->join('Comment')->join('Author')->where('Author.Name', 'John')->findOne(); 665 667 $t->is($article->getTitle(), 'aaaaa', 'you can chain several join() statements'); 666 $article = sfPropelFinder::from('Article')->joinComment()->joinAuthor()->where('Author _Name', 'John')->findOne();668 $article = sfPropelFinder::from('Article')->joinComment()->joinAuthor()->where('Author.Name', 'John')->findOne(); 667 669 $t->is($article->getTitle(), 'aaaaa', 'joinXXX() does a join according to the XXX column name'); 668 670 … … 782 784 $comment = sfPropelFinder::from('Comment')-> 783 785 join('Article')-> 784 withColumn('Article _Title')->786 withColumn('Article.Title')-> 785 787 findOne(); 786 $t->is($comment->getColumn('Article _Title'), 'bbbbb', 'Additional columns added with withColumn() are stored in the object and can be retrieved with getColumn()');788 $t->is($comment->getColumn('Article.Title'), 'bbbbb', 'Additional columns added with withColumn() are stored in the object and can be retrieved with getColumn()'); 787 789 788 790 $comment = sfPropelFinder::from('Comment')-> … … 791 793 try 792 794 { 793 $comment->getColumn('Article _Title');795 $comment->getColumn('Article.Title'); 794 796 $t->fail('getColumn() is not available as long as you don\'t add a column with withColumn()'); 795 797 } … … 800 802 801 803 $comment = sfPropelFinder::from('Comment')-> 802 withColumn('Article _Title')->804 withColumn('Article.Title')-> 803 805 findOne(); 804 $t->is($comment->getColumn('Article _Title'), 'bbbbb', 'If withColumn() is called on a related object column with no join on this class, the finder adds the join automatically');806 $t->is($comment->getColumn('Article.Title'), 'bbbbb', 'If withColumn() is called on a related object column with no join on this class, the finder adds the join automatically'); 805 807 806 808 $comment = sfPropelFinder::from('Comment')-> 807 809 join('Article')-> 808 withColumn('Article _Title', 'ArticleTitle')->810 withColumn('Article.Title', 'ArticleTitle')-> 809 811 findOne(); 810 812 $t->is($comment->getColumn('ArticleTitle'), 'bbbbb', 'withColumn() second parameter serves as a column alias'); … … 812 814 $comment = sfPropelFinder::from('Comment')-> 813 815 join('Article')-> 814 withColumn('Article _Title', 'ArticleTitle', 'int')->816 withColumn('Article.Title', 'ArticleTitle', 'int')-> 815 817 findOne(); 816 818 $t->is($comment->getColumn('ArticleTitle'), '0', 'withColumn() third parameter serves as a type caster'); … … 818 820 $comment = sfPropelFinder::from('Comment')-> 819 821 join('Article')->join('Author')-> 820 withColumn('Article _Title')->821 withColumn('Author _Name')->822 withColumn('Article.Title')-> 823 withColumn('Author.Name')-> 822 824 findOne(); 823 $t->is($comment->getColumn('Article _Title'), 'bbbbb', 'withColumn() can be called several times');824 $t->is($comment->getColumn('Author _Name'), 'John', 'withColumn() can be called several times');825 $t->is($comment->getColumn('Article.Title'), 'bbbbb', 'withColumn() can be called several times'); 826 $t->is($comment->getColumn('Author.Name'), 'John', 'withColumn() can be called several times'); 825 827 826 828 $comment = sfPropelFinder::from('Comment')-> 827 829 join('Article')->with('Author')-> 828 withColumn('Article _Title')->830 withColumn('Article.Title')-> 829 831 findOne(); 830 $t->is($comment->getColumn('Article _Title'), 'bbbbb', 'Columns added with withColumn() live together well with related objects added with with()');832 $t->is($comment->getColumn('Article.Title'), 'bbbbb', 'Columns added with withColumn() live together well with related objects added with with()'); 831 833 $t->is($comment->getAuthor()->getName(), 'John', 'Related objects added with with() live together well with columns added with withColumn()'); 832 834 833 835 $article = sfPropelFinder::from('Article')-> 834 836 join('Comment')-> 835 groupBy('Article _Id')->837 groupBy('Article.Id')-> 836 838 withColumn('COUNT(comment.ID)', 'NbComments')-> 837 839 findOne(); … … 840 842 $finder = sfPropelFinder::from('Article')-> 841 843 join('Comment')-> 842 groupBy('Article _Id')->844 groupBy('Article.Id')-> 843 845 withColumn('COUNT(comment.ID)', 'NbComments')-> 844 846 orderBy('NbComments'); 845 847 $article = $finder->findOne(); 846 $t->is($finder->getLatestQuery(), 'SELECT article.ID, article.VERSION, article.TITLE, article.CATEGORY_ID, COUNT(comment.ID) AS NbCommentsFROM article, comment WHERE article.ID=comment.ARTICLE_ID GROUP BY article.ID ORDER BY NbComments ASC LIMIT 1', 'Columns added with withColumn() can be used for sorting');848 $t->is($finder->getLatestQuery(), 'SELECT article.ID, article.VERSION, article.TITLE, article.CATEGORY_ID, COUNT(comment.ID) AS \'NbComments\' FROM article, comment WHERE article.ID=comment.ARTICLE_ID GROUP BY article.ID ORDER BY NbComments ASC LIMIT 1', 'Columns added with withColumn() can be used for sorting'); 847 849 848 850 $t->diag('set()');