Development

Changeset 10545

You must first sign up to be able to contribute.

Changeset 10545

Show
Ignore:
Timestamp:
07/31/08 17:18:52 (4 months ago)
Author:
francois
Message:

sfPropelFinderPlugin Added sfPropelFinder::groupByClass() to ease PostgreSQL grouping (based on a patch by mrhyde) (closes #4105)

Files:

Legend:

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

    r10543 r10545  
    427427  orderBy('NbComments')-> 
    428428  find(); 
     429$articles = sfPropelFinder::from('Article')-> 
     430  join('Comment')-> 
     431  groupBy('Article.Id')-> 
     432  withColumn('COUNT(comment.ID)', 'NbComments')-> 
     433  find(); 
    429434 
    430435// Lastly, the supplementary columns added with withColumn() are considered string by default 
     
    608613=== 2008-07-31 | Trunk === 
    609614 
     615 * mrhyde: Added `sfPropelFinder::groupByClass()` to ease PostgreSQL grouping 
    610616 * francois: Fixed problem with table alias and PostgreSQL (based on a patch by mrhyde) 
    611617 * mrhyde: Fixed problem with group by clauses being ripped off by pager 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r10543 r10545  
    848848   
    849849  /** 
     850   * Finder Fluid Interface for Criteria::addGroupByColumn() but this times we add all columns from given class. 
     851   * Examples: 
     852   *   $articleFinder->groupBy('Article') 
     853   *    => $c->addGroupByColumn(ArticlePeer::ID);$c->addGroupByColumn(ArticlePeer::TITLE);$c->addGroupByColumn(ArticlePeer::CREATED_AT);... 
     854   * @param string $class 
     855   * 
     856   * @return sfPropelFinder 
     857   */ 
     858  public function groupByClass($class) 
     859  { 
     860    $peerClass = sfPropelFinderUtils::getPeerClassFromClass($class); 
     861    $columns = call_user_func(array($peerClass, 'getFieldNames'), BasePeer::TYPE_COLNAME); 
     862    foreach ($columns as $column) 
     863    { 
     864      $this->criteria->addGroupByColumn($column); 
     865    } 
     866     
     867    return $this; 
     868  } 
     869    
     870  /** 
    850871   * Guess sort column based on their names 
    851872   * Will look primarily for columns named: 
     
    10811102      return $this->orderBy(substr($name, 7), $arguments); 
    10821103    } 
     1104    if(strpos($name, 'groupBy') === 0) 
     1105    { 
     1106      return $this->groupBy(substr($name, 7), $arguments); 
     1107    } 
    10831108    if(strpos($name, 'join') === 0) 
    10841109    { 
  • plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php

    r10540 r10545  
    6565ArticlePeer::doDeleteAll(); 
    6666 
    67 $t = new lime_test(116, new lime_output_color()); 
     67$t = new lime_test(120, new lime_output_color()); 
    6868 
    6969$t->diag('find()'); 
     
    752752$t->is($article->getTitle(), 'ccccc', 'orderByXXX() takes an order as argument'); 
    753753 
     754$t->diag('groupBy()'); 
     755 
     756ArticlePeer::doDeleteAll(); 
     757CommentPeer::doDeleteAll(); 
     758 
     759$article1 = new Article(); 
     760$article1->setTitle('bbbbb'); 
     761$article1->setCategory($category1); 
     762$article1->save(); 
     763$author1 = new Author(); 
     764$author1->setName('John'); 
     765$author1->save(); 
     766$comment = new Comment(); 
     767$comment->setContent('foo'); 
     768$comment->setArticleId($article1->getId()); 
     769$comment->setAuthor($author1); 
     770$comment->save(); 
     771 
     772$finder = sfPropelFinder::from('Article')-> 
     773  join('Comment')-> 
     774  groupBy('Article.Id')-> 
     775  withColumn('COUNT(comment.ID)', 'NbComments'); 
     776$article = $finder->findOne(); 
     777$t->is($finder->getLatestQuery(), 'SELECT article.ID, article.TITLE, article.CATEGORY_ID, COUNT(comment.ID) AS NbComments FROM article, comment WHERE article.ID=comment.ARTICLE_ID GROUP BY article.ID LIMIT 1', 'groupBy() accepts a column name and issues a GROUP BY clause'); 
     778 
     779$finder = sfPropelFinder::from('Article')-> 
     780  join('Comment')-> 
     781  withColumn('Article.Id', 'foo')-> 
     782  groupBy('foo')-> 
     783  withColumn('COUNT(comment.ID)', 'NbComments'); 
     784$article = $finder->findOne(); 
     785$t->is($finder->getLatestQuery(), 'SELECT article.ID, article.TITLE, article.CATEGORY_ID, article.ID AS foo, COUNT(comment.ID) AS NbComments FROM article, comment WHERE article.ID=comment.ARTICLE_ID GROUP BY foo LIMIT 1', 'groupBy() accepts a column alias and issues a GROUP BY clause'); 
     786 
     787$finder = sfPropelFinder::from('Article')-> 
     788  join('Comment')-> 
     789  groupByArticle_Id()-> 
     790  withColumn('COUNT(comment.ID)', 'NbComments'); 
     791$article = $finder->findOne(); 
     792$t->is($finder->getLatestQuery(), 'SELECT article.ID, article.TITLE, article.CATEGORY_ID, COUNT(comment.ID) AS NbComments FROM article, comment WHERE article.ID=comment.ARTICLE_ID GROUP BY article.ID LIMIT 1', 'groupByXXX() accepts a column name and issues a GROUP BY clause'); 
     793 
     794$t->diag('groupByClass()'); 
     795 
     796ArticlePeer::doDeleteAll(); 
     797CommentPeer::doDeleteAll(); 
     798 
     799$article1 = new Article(); 
     800$article1->setTitle('bbbbb'); 
     801$article1->setCategory($category1); 
     802$article1->save(); 
     803$author1 = new Author(); 
     804$author1->setName('John'); 
     805$author1->save(); 
     806$comment = new Comment(); 
     807$comment->setContent('foo'); 
     808$comment->setArticleId($article1->getId()); 
     809$comment->setAuthor($author1); 
     810$comment->save(); 
     811 
     812$finder = sfPropelFinder::from('Article')-> 
     813  join('Comment')-> 
     814  groupByClass('Article')-> 
     815  withColumn('COUNT(comment.ID)', 'NbComments'); 
     816$article = $finder->findOne(); 
     817$t->is($finder->getLatestQuery(), 'SELECT article.ID, article.TITLE, article.CATEGORY_ID, COUNT(comment.ID) AS NbComments FROM article, comment WHERE article.ID=comment.ARTICLE_ID GROUP BY article.ID,article.TITLE,article.CATEGORY_ID LIMIT 1', 'groupByClass() accepts a model class name and issues a GROUP BY clause on all columns'); 
     818 
    754819$t->diag('set()'); 
    755820