Development

Changeset 10112

You must first sign up to be able to contribute.

Changeset 10112

Show
Ignore:
Timestamp:
07/03/08 23:12:40 (5 months ago)
Author:
francois
Message:

sfPropelFinderPlugin Preferring ClassName.ColumnName over ClassName.ColumnName for complete column names

Files:

Legend:

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

    r10101 r10112  
    9595  find(); 
    9696 
    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(); 
    9999// You can also use the magic whereXXX() method, removing the column argument and concatenating it to the method name 
    100100$articles = $articleFinder->whereTitle('foo')->find(); 
     
    205205$article = sfPropelFinder::from('Article')-> 
    206206  join('Comment')-> 
    207   where('Comment_Content', 'You rock!')-> 
     207  where('Comment.Content', 'You rock!')-> 
    208208  findOne(); 
    209209// You can chain joins if you want to make it more complex 
     
    222222  join('Comment')-> 
    223223  join('Author')-> 
    224   where('Author_Name', 'John')-> 
     224  where('Author.Name', 'John')-> 
    225225  findOne(); 
    226226// You can also use the magic joinXXX() method 
     
    235235$comment = sfPropelFinder::from('Comment')-> 
    236236  join('Article')-> 
    237   where('Article_Title', 'Hello, world')-> 
     237  where('Article.Title', 'Hello, world')-> 
    238238  findOne(); 
    239239$article = $comment->getArticle();  // Needs another database query 
     
    246246$comment = sfPropelFinder::from('Comment')->with('Article')-> 
    247247  join('Article')-> 
    248   where('Article_Title', 'Hello, world')-> 
     248  where('Article.Title', 'Hello, world')-> 
    249249  findOne(); 
    250250$article = $comment->getArticle();  // Same result, with no supplementary query 
     
    314314$article = sfPropelFinder::from('Article')-> 
    315315  join('Category')-> 
    316   withColumn('Category_Name')-> 
    317   findOne(); 
    318 $categoryName = $article->getColumn('Category_Name');  // No supplementary query 
     316  withColumn('Category.Name')-> 
     317  findOne(); 
     318$categoryName = $article->getColumn('Category.Name');  // No supplementary query 
    319319 
    320320// Beware that in this case, the related `Category` object is not hydrated, since `with()` was not used. 
     
    325325// Just like with(), withColumn() adds an internal join if you don't do it yourself 
    326326$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')` 
    330330 
    331331// withColumn() can use a column alias as second argument. 
    332332$article = sfPropelFinder::from('Article')-> 
    333333  join('Category')-> 
    334   withColumn('Category_Name', 'category')-> 
     334  withColumn('Category.Name', 'category')-> 
    335335  findOne(); 
    336336$categoryName = $article->getColumn('category'); 
     
    347347$article = sfPropelFinder::from('Article')-> 
    348348  join('Category')-> 
    349   withColumn('Category_CreatedAt', 'CategoryCreatedAt', 'Timestamp')-> 
     349  withColumn('Category.CreatedAt', 'CategoryCreatedAt', 'Timestamp')-> 
    350350  findOne(); 
    351351$categoryName = $article->getColumn('CategoryCreatedAt'); 
     
    483483=== 2008-07-03 | Trunk === 
    484484 
     485 * francois: Preferring `ClassName.ColumnName` over `ClassName.ColumnName` for complete column names 
    485486 * francois: Added Propel 1.3 compatibility 
    486487 * francois: Added `sfPropelFinder::set()` method (based on a patch by jug) 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r10102 r10112  
    505505        } 
    506506        // 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) 
    508508        { 
    509509          // Additional columns are stored in the object, in a special 'namespace' 
    510510          // see getColumn() for how to retrieve the value afterwards 
    511           $asName = 'WithColumn'.$name; 
    512511          // Using the third parameter of withColumn() as a type. defaults to $rs->get() (= $rs->getString()) 
    513512          $typedGetter = 'get'.ucfirst($column['type']); 
    514513          if($propelVersion == '1.2') 
    515514          { 
    516             $obj->$asName = $resultSet->$typedGetter($startCol); 
     515            $this->setColumn($obj, $alias, $resultSet->$typedGetter($startCol)); 
    517516          } 
    518517          else 
    519518          { 
    520             $obj->$asName = $row[$startCol]
     519            $this->setColumn($obj, $alias, $row[$startCol])
    521520          } 
    522521          $startCol++; 
     
    564563    } 
    565564    // 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) 
    567566    { 
    568567      // if the column is on a related object property  
     
    575574        $this->relations[]= $peerClass; 
    576575      } 
    577       $c->addAsColumn($name, $column['column']); 
     576      $c->addAsColumn('\''.$alias.'\'', $column['column']); 
    578577    } 
    579578     
     
    10061005    $tmp = new $class(); 
    10071006    return get_class($tmp->getPeer()); 
     1007    $tmp->__destroy(); 
    10081008  } 
    10091009 
     
    10181018   * Requires symfony and sfMixer enabled 
    10191019   */ 
    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; 
    10251030  } 
    10261031   
     
    10501055      return $phpName; 
    10511056    } 
    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); 
    10551061      $peerClass = $this->getPeerClassFromClass($class); 
    10561062    } 
     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    } 
    10571071    if(!$peerClass) 
    10581072    { 
     1073      // Column 
    10591074      $peerClass = $this->peerClass; 
    10601075    } 
  • plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php

    r10101 r10112  
    6565ArticlePeer::doDeleteAll(); 
    6666 
    67 $t = new lime_test(141, new lime_output_color()); 
     67$t = new lime_test(142, new lime_output_color()); 
    6868 
    6969$t->diag('find()'); 
     
    358358  $t->pass('where() throws an exception when the column is not found'); 
    359359} 
     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'); 
    360362$article = sfPropelFinder::from('Article')->where('Article_Title', 'abc')->findOne(); 
    361363$t->is($article->getId(), $article1->getId(), 'where() accepts a complete column name ClassName_ColumnName'); 
     
    464466$finder = sfPropelFinder::from('Article')-> 
    465467  joinCategory()-> 
    466   where('Category_Name', 'foo')-> 
    467   _or('Category_Name', 'bar'); 
     468  where('Category.Name', 'foo')-> 
     469  _or('Category.Name', 'bar'); 
    468470$finder->find(); 
    469471$t->is( 
     
    476478  joinArticle()-> 
    477479  joinAuthor()-> 
    478   where('Article_Title', 'foo')-> 
    479   _or('Author_Name', 'bar'); 
     480  where('Article.Title', 'foo')-> 
     481  _or('Author.Name', 'bar'); 
    480482$finder->find(); 
    481483$t->is( 
     
    622624$article3->setCategory($category2); 
    623625$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(); 
    625627$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(); 
    627629$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(); 
    629631$t->is($article->getTitle(), 'ccccc', 'join() allows to join to another table (many-to-one)'); 
    630632ArticlePeer::doDeleteAll(); 
     
    642644$comment->setArticleId($article2->getId()); 
    643645$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(); 
    645647$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(); 
    647649$t->is($article->getTitle(), 'bbbbb', 'join() allows to join to another table (one-to-many)'); 
    648650 
     
    662664$comment->setAuthor($author1); 
    663665$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(); 
    665667$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(); 
    667669$t->is($article->getTitle(), 'aaaaa', 'joinXXX() does a join according to the XXX column name'); 
    668670 
     
    782784$comment = sfPropelFinder::from('Comment')-> 
    783785  join('Article')-> 
    784   withColumn('Article_Title')-> 
     786  withColumn('Article.Title')-> 
    785787  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()'); 
    787789 
    788790$comment = sfPropelFinder::from('Comment')-> 
     
    791793try 
    792794{ 
    793   $comment->getColumn('Article_Title'); 
     795  $comment->getColumn('Article.Title'); 
    794796  $t->fail('getColumn() is not available as long as you don\'t add a column with withColumn()'); 
    795797} 
     
    800802 
    801803$comment = sfPropelFinder::from('Comment')-> 
    802   withColumn('Article_Title')-> 
     804  withColumn('Article.Title')-> 
    803805  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'); 
    805807 
    806808$comment = sfPropelFinder::from('Comment')-> 
    807809  join('Article')-> 
    808   withColumn('Article_Title', 'ArticleTitle')-> 
     810  withColumn('Article.Title', 'ArticleTitle')-> 
    809811  findOne(); 
    810812$t->is($comment->getColumn('ArticleTitle'), 'bbbbb', 'withColumn() second parameter serves as a column alias'); 
     
    812814$comment = sfPropelFinder::from('Comment')-> 
    813815  join('Article')-> 
    814   withColumn('Article_Title', 'ArticleTitle', 'int')-> 
     816  withColumn('Article.Title', 'ArticleTitle', 'int')-> 
    815817  findOne(); 
    816818$t->is($comment->getColumn('ArticleTitle'), '0', 'withColumn() third parameter serves as a type caster'); 
     
    818820$comment = sfPropelFinder::from('Comment')-> 
    819821  join('Article')->join('Author')-> 
    820   withColumn('Article_Title')-> 
    821   withColumn('Author_Name')-> 
     822  withColumn('Article.Title')-> 
     823  withColumn('Author.Name')-> 
    822824  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'); 
    825827 
    826828$comment = sfPropelFinder::from('Comment')-> 
    827829  join('Article')->with('Author')-> 
    828   withColumn('Article_Title')-> 
     830  withColumn('Article.Title')-> 
    829831  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()'); 
    831833$t->is($comment->getAuthor()->getName(), 'John', 'Related objects added with with() live together well with columns added with withColumn()'); 
    832834 
    833835$article = sfPropelFinder::from('Article')-> 
    834836  join('Comment')-> 
    835   groupBy('Article_Id')-> 
     837  groupBy('Article.Id')-> 
    836838  withColumn('COUNT(comment.ID)', 'NbComments')-> 
    837839  findOne(); 
     
    840842$finder = sfPropelFinder::from('Article')-> 
    841843  join('Comment')-> 
    842   groupBy('Article_Id')-> 
     844  groupBy('Article.Id')-> 
    843845  withColumn('COUNT(comment.ID)', 'NbComments')-> 
    844846  orderBy('NbComments'); 
    845847$article = $finder->findOne(); 
    846 $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'); 
     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'); 
    847849 
    848850$t->diag('set()');