Development

Changeset 10065

You must first sign up to be able to contribute.

Changeset 10065

Show
Ignore:
Timestamp:
07/02/08 14:57:02 (2 months ago)
Author:
francois
Message:

sfPropelFinderPlugin Added i18n capabilities

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfPropelFinderPlugin/README

    r10047 r10065  
    271271}}} 
    272272 
     273The `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 
     290sfContext::getInstance()->getUser()->setCulture('en'); 
     291$article = sfPropelFinder::from('Article')->with('I18n')->findOne(); 
     292echo $article->getContent();   // english content 
     293sfContext::getInstance()->getUser()->setCulture('fr'); 
     294$article = sfPropelFinder::from('Article')->with('I18n')->findOne(); 
     295echo $article->getContent();   // contenu français 
     296}}} 
     297 
     298Note: 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 
    273300=== Adding columns === 
    274301 
     
    419446== Changelog == 
    420447 
    421 === 2008-07-01 | Trunk === 
    422  
     448=== 2008-07-02 | Trunk === 
     449 
     450 * francois: Added `sfPropelFinder::withI18n()` method 
    423451 * francois: Added `sfPropelFinderPager` class and `sfPropelFinder::paginate()` method 
    424452 * francois: Added `sfPropelFinder::groupBy()` method 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r10047 r10065  
    2121  protected $withClasses = array(); 
    2222  protected $withColumns = array(); 
     23  protected $culture = null; 
    2324 
    2425  public function __construct($class = '') 
     
    391392        $obj = new $cls(); 
    392393        $startCol = $obj->hydrate($resultSet, 1); 
     394        if($this->culture) 
     395        { 
     396          $obj->setCulture($this->culture); 
     397        } 
    393398        // Then the related classes added by way of 'with' 
    394399        $objectsInJoin = array($obj); 
     
    520525    foreach($classes as $class) 
    521526    { 
    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; 
    524549     
    525550    return $this; 
  • plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php

    r9948 r10065  
    1818        title:       varchar(255) 
    1919        category_id: ~ 
     20      article_i18n: 
     21        content:     varchar(255) 
    2022      category: 
    2123        id:          ~ 
     
    6365ArticlePeer::doDeleteAll(); 
    6466 
    65 $t = new lime_test(126, new lime_output_color()); 
     67$t = new lime_test(132, new lime_output_color()); 
    6668 
    6769$t->diag('find()'); 
     
    635637$comment = new Comment(); 
    636638$comment->setContent('foo'); 
    637 $comment->setArticle($article2); 
     639$comment->setArticleId($article2->getId()); 
    638640$comment->save(); 
    639641$nbArticles = sfPropelFinder::from('Article')->join('Comment')->where('Comment_Content', 'foo')->count(); 
     
    654656$comment = new Comment(); 
    655657$comment->setContent('foo'); 
    656 $comment->setArticle($article1); 
     658$comment->setArticleId($article1->getId()); 
    657659$comment->setAuthor($author1); 
    658660$comment->save(); 
     
    699701$comment = new Comment(); 
    700702$comment->setContent('foo'); 
    701 $comment->setArticle($article1); 
     703$comment->setArticleId($article1->getId()); 
    702704$comment->setAuthor($author1); 
    703705$comment->save(); 
     
    717719$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'); 
    718720 
     721$t->diag('withI18n()'); 
     722 
     723ArticlePeer::doDeleteAll(); 
     724ArticleI18nPeer::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 
     733sfContext::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 
     740sfContext::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 
     747sfContext::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 
    719755$t->diag('withColumn()'); 
     756 
     757ArticlePeer::doDeleteAll(); 
     758CommentPeer::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 
    720773$comment = sfPropelFinder::from('Comment')-> 
    721774  join('Article')->