Changeset 10065
- Timestamp:
- 07/02/08 14:57:02 (2 months ago)
- Files:
-
- plugins/sfPropelFinderPlugin/README (modified) (2 diffs)
- plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php (modified) (3 diffs)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelFinderPlugin/README
r10047 r10065 271 271 }}} 272 272 273 The `with()` method can also hydrate the related I18n objects, thus providing an equivalent to symfony's `doSelectWithI18n()` methods. 274 {{{ 275 #!php 276 <?php 277 // Consider the following schema 278 //article: 279 // title: varchar(255) 280 //article_i18n: 281 // content: varchar(255) 282 $article = new Article(); 283 $article->setTitle('Foo Bar'); 284 $article->setCulture('en'); 285 $article->setContent('english content'); 286 $article->setCulture('fr'); 287 $article->setContent('contenu français'); 288 $article->save(); 289 290 sfContext::getInstance()->getUser()->setCulture('en'); 291 $article = sfPropelFinder::from('Article')->with('I18n')->findOne(); 292 echo $article->getContent(); // english content 293 sfContext::getInstance()->getUser()->setCulture('fr'); 294 $article = sfPropelFinder::from('Article')->with('I18n')->findOne(); 295 echo $article->getContent(); // contenu français 296 }}} 297 298 Note: Since the `i18nTable` and the `ìs_culture` schema properties are lost after model generation, `with('I18n')` only works if the i18n table is named after the main table (e.g. 'Article' => 'ArticleI18n') and if the culture column name is `culture`. This is the default symfony behavior, so it should work if you didn't define special i18n table and column names. 299 273 300 === Adding columns === 274 301 … … 419 446 == Changelog == 420 447 421 === 2008-07-01 | Trunk === 422 448 === 2008-07-02 | Trunk === 449 450 * francois: Added `sfPropelFinder::withI18n()` method 423 451 * francois: Added `sfPropelFinderPager` class and `sfPropelFinder::paginate()` method 424 452 * francois: Added `sfPropelFinder::groupBy()` method plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php
r10047 r10065 21 21 protected $withClasses = array(); 22 22 protected $withColumns = array(); 23 protected $culture = null; 23 24 24 25 public function __construct($class = '') … … 391 392 $obj = new $cls(); 392 393 $startCol = $obj->hydrate($resultSet, 1); 394 if($this->culture) 395 { 396 $obj->setCulture($this->culture); 397 } 393 398 // Then the related classes added by way of 'with' 394 399 $objectsInJoin = array($obj); … … 520 525 foreach($classes as $class) 521 526 { 522 $this->addWithClass($class); 523 } 527 if(strtolower($class) == 'i18n') 528 { 529 $this->withI18n(); 530 } 531 else 532 { 533 $this->addWithClass($class); 534 } 535 } 536 537 return $this; 538 } 539 540 public function withI18n() 541 { 542 $i18nClass = $this->class.'I18n'; 543 $tmp = new $i18nClass(); 544 $i18nPeerClass = get_class($tmp->getPeer()); 545 $culture = sfContext::getInstance()->getUser()->getCulture(); 546 $this->addWithClass($this->class.'I18n'); 547 $this->criteria->add(constant($i18nPeerClass.'::CULTURE'), $culture); 548 $this->culture = $culture; 524 549 525 550 return $this; plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php
r9948 r10065 18 18 title: varchar(255) 19 19 category_id: ~ 20 article_i18n: 21 content: varchar(255) 20 22 category: 21 23 id: ~ … … 63 65 ArticlePeer::doDeleteAll(); 64 66 65 $t = new lime_test(1 26, new lime_output_color());67 $t = new lime_test(132, new lime_output_color()); 66 68 67 69 $t->diag('find()'); … … 635 637 $comment = new Comment(); 636 638 $comment->setContent('foo'); 637 $comment->setArticle ($article2);639 $comment->setArticleId($article2->getId()); 638 640 $comment->save(); 639 641 $nbArticles = sfPropelFinder::from('Article')->join('Comment')->where('Comment_Content', 'foo')->count(); … … 654 656 $comment = new Comment(); 655 657 $comment->setContent('foo'); 656 $comment->setArticle ($article1);658 $comment->setArticleId($article1->getId()); 657 659 $comment->setAuthor($author1); 658 660 $comment->save(); … … 699 701 $comment = new Comment(); 700 702 $comment->setContent('foo'); 701 $comment->setArticle ($article1);703 $comment->setArticleId($article1->getId()); 702 704 $comment->setAuthor($author1); 703 705 $comment->save(); … … 717 719 $t->is($finder->getLatestQuery(), 'SELECT comment.ID, comment.CONTENT, comment.ARTICLE_ID, comment.AUTHOR_ID, article.ID, article.VERSION, article.TITLE, article.CATEGORY_ID, category.ID, category.NAME FROM comment, article, category WHERE comment.ARTICLE_ID=article.ID AND article.CATEGORY_ID=category.ID LIMIT 1', 'with() accepts several arguments, so you don\'t need to call it several times'); 718 720 721 $t->diag('withI18n()'); 722 723 ArticlePeer::doDeleteAll(); 724 ArticleI18nPeer::doDeleteAll(); 725 $article1 = new Article(); 726 $article1->setTitle('aaa'); 727 $article1->setCulture('en'); 728 $article1->setContent('english content'); 729 $article1->setCulture('fr'); 730 $article1->setContent('contenu français'); 731 $article1->save(); 732 733 sfContext::getInstance()->getUser()->setCulture('en'); 734 $finder = sfPropelFinder::from('Article')->withI18n(); 735 $article = $finder->findOne(); 736 737 $t->is($finder->getLatestQuery(), 'SELECT article.ID, article.VERSION, article.TITLE, article.CATEGORY_ID, article_i18n.CONTENT, article_i18n.ID, article_i18n.CULTURE FROM article, article_i18n WHERE article_i18n.CULTURE=\'en\' AND article.ID=article_i18n.ID LIMIT 1', 'withI18n() hydrates the related I18n object with a culture taken from the user object'); 738 $t->is($article->getContent(), 'english content', 'withI18n() considers the current user culture for hydration'); 739 740 sfContext::getInstance()->getUser()->setCulture('fr'); 741 $finder = sfPropelFinder::from('Article')->withI18n(); 742 $article = $finder->findOne(); 743 744 $t->is($finder->getLatestQuery(), 'SELECT article.ID, article.VERSION, article.TITLE, article.CATEGORY_ID, article_i18n.CONTENT, article_i18n.ID, article_i18n.CULTURE FROM article, article_i18n WHERE article_i18n.CULTURE=\'fr\' AND article.ID=article_i18n.ID LIMIT 1', 'withI18n() hydrates the related I18n object with a culture taken from the user object'); 745 $t->is($article->getContent(), 'contenu français', 'withI18n() considers the current user culture for hydration'); 746 747 sfContext::getInstance()->getUser()->setCulture('en'); 748 $finder = sfPropelFinder::from('Article')->with('I18n'); 749 $article = $finder->findOne(); 750 $t->is($finder->getLatestQuery(), 'SELECT article.ID, article.VERSION, article.TITLE, article.CATEGORY_ID, article_i18n.CONTENT, article_i18n.ID, article_i18n.CULTURE FROM article, article_i18n WHERE article_i18n.CULTURE=\'en\' AND article.ID=article_i18n.ID LIMIT 1', 'with(\'I18n\') is a synonym for withI18n()'); 751 $finder = sfPropelFinder::from('Article')->with('i18n'); 752 $article = $finder->findOne(); 753 $t->is($finder->getLatestQuery(), 'SELECT article.ID, article.VERSION, article.TITLE, article.CATEGORY_ID, article_i18n.CONTENT, article_i18n.ID, article_i18n.CULTURE FROM article, article_i18n WHERE article_i18n.CULTURE=\'en\' AND article.ID=article_i18n.ID LIMIT 1', 'with(\'i18n\') is a synonym for withI18n()'); 754 719 755 $t->diag('withColumn()'); 756 757 ArticlePeer::doDeleteAll(); 758 CommentPeer::doDeleteAll(); 759 760 $article1 = new Article(); 761 $article1->setTitle('bbbbb'); 762 $article1->setCategory($category1); 763 $article1->save(); 764 $author1 = new Author(); 765 $author1->setName('John'); 766 $author1->save(); 767 $comment = new Comment(); 768 $comment->setContent('foo'); 769 $comment->setArticleId($article1->getId()); 770 $comment->setAuthor($author1); 771 $comment->save(); 772 720 773 $comment = sfPropelFinder::from('Comment')-> 721 774 join('Article')->