Development

Changeset 8168

You must first sign up to be able to contribute.

Changeset 8168

Show
Ignore:
Timestamp:
03/31/08 14:03:43 (8 months ago)
Author:
francois
Message:

sfPropelFinderPlugin

  • De-emphasized the use of magic methods in the unit tests and README
  • Added sfPropelFinder::_and() and sfPropelFinder::_or() methods
Files:

Legend:

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

    r8132 r8168  
    7070$articleFinder = sfPropelFinder::from('Article'); 
    7171// Finding all Articles where title = 'foo' 
     72$articles = $articleFinder->where('Title', 'foo')->find(); 
     73// Finding all Articles where title like 'foo%' 
     74$articles = $articleFinder->where('Title', 'like', 'foo%')->find(); 
     75// Finding all Articles where published_at less than time() 
     76$articles = $articleFinder->where('PublishedAt', '<', time())->find(); 
     77 
     78// You can chain WHERE clauses 
     79$articles = $articleFinder-> 
     80  where('Title', 'foo')-> 
     81  where('PublishedAt', '<', time())-> 
     82  find(); 
     83// Or even better, use the _and() and _or() methods for SQL-like code 
     84$articles = $articleFinder-> 
     85  where('Title', 'foo')-> 
     86   _and('PublishedAt', '<', time())-> 
     87    _or('Title', 'like', 'bar%')-> 
     88  find(); 
     89 
     90// The where() method accepts simple or composed column names ('ClassName_ColumnName') 
     91$articles = $articleFinder->where('Article_Title', 'foo')->find(); 
     92// You can also use the magic whereXXX() method, removing the column argument and concatenating it to the method name 
    7293$articles = $articleFinder->whereTitle('foo')->find(); 
    73 // Finding all Articles where title like 'foo%' 
    74 $articles = $articleFinder->whereTitle('like', 'foo%')->find(); 
    75 // Finding all Articles where published_at less than time() 
    76 $articles = $articleFinder->wherePublishedAt('<', time())->find(); 
    77 // The whereXXX method accepts simple or composed column names 
    78 $articles = $articleFinder->whereArticle_PublishedAt('<', time())->find(); 
    7994}}} 
    8095 
     
    85100<?php 
    86101$articleFinder = sfPropelFinder::from('Article'); 
    87 // Finding all Articles ordered by created_at ('asc') 
     102// Finding all Articles ordered by created_at (ascending order by default) 
     103$articles = $articleFinder->orderBy('CreatedAt')->find(); 
     104// Finding all Articles ordered by created_at desc 
     105$articles = $articleFinder->orderBy('CreatedAt', 'desc')->find(); 
     106// You can also use the magic orderByXXX() method 
    88107$articles = $articleFinder->orderByCreatedAt()->find(); 
    89 // Finding all Articles ordered by created_at desc 
    90 $articles = $articleFinder->orderByCreatedAt('desc')->find(); 
    91 }}} 
    92  
    93 === Counting objects === 
    94  
    95 {{{ 
    96 #!php 
    97 <?php 
    98 // Counting all Articles 
    99 $nbArticles = sfPropelFinder::from('Article')->count(); 
    100108}}} 
    101109 
     
    108116<?php 
    109117// everything chained together 
    110 $articles = sfPropelFinder::from('Article')->whereTitle('like', '%world')->whereIsPublished(true)->orderByCreatedAt()->find(); 
     118$articles = sfPropelFinder::from('Article')->where('Title', 'like', '%world')->_and('IsPublished', true)->orderBy('CreatedAt')->find(); 
    111119// You can write it in several lines, too 
    112120$articles = sfPropelFinder::from('Article')-> 
    113   whereTitle('like', '%world')-> 
    114   whereIsPublished(true)-> 
    115   orderByCreatedAt()-> 
     121  where('Title', 'like', '%world')-> 
     122  _and('IsPublished', true)-> 
     123  orderBy('CreatedAt')-> 
    116124  find(); 
    117125}}} 
     
    135143// After all, the columns of the FK are already defined in the schema 
    136144$article = sfPropelFinder::from('Article')-> 
    137   joinComment()-> 
    138   whereComment_Content('You rock!')-> 
     145  join('Comment')-> 
     146  where('Comment_Content', 'You rock!')-> 
    139147  findOne(); 
    140148// You can chain joins if you want to make it more complex 
    141149$article2 = new Article(); 
    142 $article2->setTitle('Hello gain, world!'); 
     150$article2->setTitle('Hello again, world!'); 
    143151$article2->save(); 
    144152$author1 = new Author(); 
     
    151159$comment->save(); 
    152160$article = sfPropelFinder::from('Article')-> 
    153   joinComment()-> 
    154   joinAuthor()-> 
    155   whereAuthor_Name('John')-> 
     161  join('Comment')-> 
     162  join('Author')-> 
     163  where('Author_Name', 'John')-> 
    156164  findOne(); 
     165// You can also use the magic joinXXX() method 
     166}}} 
     167 
     168=== Counting objects === 
     169 
     170{{{ 
     171#!php 
     172<?php 
     173// Counting all Articles 
     174$nbArticles = sfPropelFinder::from('Article')->count(); 
    157175}}} 
    158176 
     
    165183$nbArticles = sfPropelFinder::from('Article')->delete(); 
    166184// Deleting a selection of Articles 
    167 $nbArticles = sfPropelFinder::from('Article')->whereTitle('like', 'foo%')->delete(); 
     185$nbArticles = sfPropelFinder::from('Article')-> 
     186  where('Title', 'like', 'foo%')-> 
     187  delete(); 
    168188}}} 
    169189 
     
    201221<?php 
    202222$articles = sfPropelFinder::from('Article')-> 
    203   whereTitle('like', 'foo%')-> 
     223  where('Title', 'like', 'foo%')-> 
    204224  addOr(ArticlePeer::TITLE, 'bar%', Criteria::LIKE)-> // that's a Criteria method 
    205225  findOne(); 
     
    211231#!php 
    212232<?php 
    213 $finder = sfPropelFinder::from('Article')->whereTitle('foo'); 
     233$finder = sfPropelFinder::from('Article')->where('Title', 'foo'); 
    214234$finder->findOne(); 
    215235$query = $finder->getLatestQuery(); 
     
    220240 
    221241 * Allow hydrating of related objects (doSelectJoinXXX equivalent) 
    222  * Add And 
    223  * Add Or 
    224242 * Put as a parent class in the PeerBuilder so that every Peer class can be a finder 
    225243 * Merge with sfPropelImpersonatorPlugin! 
     
    227245== Changelog == 
    228246 
    229 === 2008-03-28 | 0.2.0 Beta === 
    230  
     247=== 2008-03-31 | 0.2.0 Beta === 
     248 
     249 * francois: De-emphasized the use of magic methods in the unit tests and README 
     250 * francois: Added `sfPropelFinder::_and()` and `sfPropelFinder::_or()` methods 
    231251 * francois: Added support for Criteria methods (no more limit to what you can do with a finder!) 
    232252 * francois: Added `sfPropelFinder::getLatestQuery()` method 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r8160 r8168  
    196196  { 
    197197    $column = $this->getColName($columnName); 
     198    if(!is_array($arguments)) 
     199    { 
     200      $arguments = func_get_args(); 
     201      array_shift($arguments); 
     202    }  
     203    list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments); 
     204    $this->criteria->add($column, $value, $comparison); 
     205     
     206    return $this; 
     207  } 
     208 
     209  /** 
     210   * Finder Fluid Interface for Criteria::addAnd() 
     211   * Infers $column, $value, $comparison from $columnName and some optional arguments 
     212   * Examples: 
     213   *   $articleFinder->_and('CommentId', 3) 
     214   *    => $c->addAnd(ArticlePeer::COMMENT_ID, 3) 
     215   * 
     216   * @param      string  $columnName PHPName of the column bearing the condition 
     217   * @param      array   $arguments  Optional array of arguments 
     218   * 
     219   * @return     sfPropelFinder the current finder object 
     220   */ 
     221  public function _and($columnName, $arguments = array()) 
     222  { 
     223    $column = $this->getColName($columnName); 
     224    if(!is_array($arguments)) 
     225    { 
     226      $arguments = func_get_args(); 
     227      array_shift($arguments); 
     228    }  
     229    list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments); 
     230    $this->criteria->addAnd($column, $value, $comparison); 
     231     
     232    return $this; 
     233  } 
     234 
     235  /** 
     236   * Finder Fluid Interface for Criteria::addOr() 
     237   * Infers $column, $value, $comparison from $columnName and some optional arguments 
     238   * Examples: 
     239   *   $articleFinder->_or('CommentId', 3) 
     240   *    => $c->addOr(ArticlePeer::COMMENT_ID, 3) 
     241   * 
     242   * @param      string  $columnName PHPName of the column bearing the condition 
     243   * @param      array   $arguments  Optional array of arguments 
     244   * 
     245   * @return     sfPropelFinder the current finder object 
     246   */ 
     247  public function _or($columnName, $arguments = array()) 
     248  { 
     249    $column = $this->getColName($columnName); 
     250    if(!is_array($arguments)) 
     251    { 
     252      $arguments = func_get_args(); 
     253      array_shift($arguments); 
     254    }  
     255    list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments); 
     256    $this->criteria->addOr($column, $value, $comparison); 
     257     
     258    return $this; 
     259  } 
     260   
     261  protected function getValueAndComparisonFromArguments($arguments = array()) 
     262  { 
    198263    $comparison = Criteria::EQUAL; 
    199264    switch (count($arguments)) 
     
    217282        throw new Exception('{sfPropelFinder} whereXXX can only be called with one or two arguments'); 
    218283    } 
    219     $this->criteria->add($column, $value, $comparison); 
    220      
    221     return $this; 
    222  
     284 
     285    return array($value, $comparison); 
    223286  } 
    224287 
     
    236299  { 
    237300    $column = $this->getColName($columnName); 
     301    if(!is_array($arguments)) 
     302    { 
     303      $arguments = func_get_args(); 
     304      array_shift($arguments); 
     305    }  
    238306    $order = strtoupper(array_shift($arguments)); 
    239307    if(!$order) 
     
    271339    list($column1, $column2) = $this->getRelation($relatedClass); 
    272340    $this->relations[]= $relatedClass.'Peer'; 
     341    if(!is_array($arguments)) 
     342    { 
     343      $arguments = func_get_args(); 
     344      array_shift($arguments); 
     345    }  
    273346    $operator = array_shift($arguments); 
    274347    if(!$operator) 
     
    364437      return $this->join(substr($name, 4), $arguments); 
    365438    } 
     439    if(strpos($name, '_and') === 0) 
     440    { 
     441      return $this->_and(substr($name, 4), $arguments); 
     442    } 
     443    if(strpos($name, '_or') === 0) 
     444    { 
     445      return $this->_or(substr($name, 3), $arguments); 
     446    } 
    366447    if(method_exists($this->criteria, $name)) 
    367448    { 
  • plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php

    r8160 r8168  
    6363ArticlePeer::doDeleteAll(); 
    6464 
    65 $t = new lime_test(61, new lime_output_color()); 
     65$t = new lime_test(70, new lime_output_color()); 
    6666 
    6767$t->diag('find()'); 
     
    196196$article3->save(); 
    197197$finder = new sfPropelFinder('Article'); 
    198 $nbDeleted = $finder->whereTitle('foo2')->delete(); 
     198$nbDeleted = $finder->where('Title', 'foo2')->delete(); 
    199199$t->is($nbDeleted, 1, 'delete() deletes all rows found by a finder'); 
    200200$nbArticles = $finder->count(); 
    201201$t->is($nbArticles, 1, 'delete() does not delete rows not found by the finder'); 
    202202 
    203 $t->diag('where() magic'); 
    204  
     203$t->diag('where()'); 
     204 
     205ArticlePeer::doDeleteAll(); 
     206$article1 = new Article(); 
     207$article1->setTitle('abc'); 
     208$article1->save(); 
     209$article2 = new Article(); 
     210$article2->setTitle('def'); 
     211$article2->save(); 
     212$article3 = new Article(); 
     213$article3->setTitle('bbc'); 
     214$article3->save(); 
     215$article = sfPropelFinder::from('Article')->where('Title', 'abc')->findOne(); 
     216$t->is($article->getId(), $article1->getId(), 'where() accepts a simple CamelCase column name like ClassName'); 
     217try 
     218
     219  $article = sfPropelFinder::from('Article')->where('Foo', 'abc'); 
     220  $t->fail('where() throws an exception when the column is not found'); 
     221
     222catch (Exception $e) 
     223
     224  $t->pass('where() throws an exception when the column is not found'); 
     225
     226$article = sfPropelFinder::from('Article')->where('Article_Title', 'abc')->findOne(); 
     227$t->is($article->getId(), $article1->getId(), 'where() accepts a complete column name ClassName_ColumnName'); 
     228$article = sfPropelFinder::from('Article')->where('Title', 'def')->findOne(); 
     229$t->is($article->getId(), $article2->getId(), 'where() adds a WHERE condition on the column given as first argument'); 
     230$articles = sfPropelFinder::from('Article')->where('Title', Criteria::LIKE, '%bc')->find(); 
     231$t->is(count($articles), 2, 'where() accepts a comparator as second argument when three arguments are given'); 
     232$articles = sfPropelFinder::from('Article')->where('Title', 'like', '%bc')->find(); 
     233$t->is(count($articles), 2, 'where() accepts a text comparator and is permissive on syntax'); 
     234$articles = sfPropelFinder::from('Article')->where('Title', 'is not null', null)->find(); 
     235$t->is(count($articles), 3, 'where() accepts a text comparator and is permissive on syntax'); 
    205236try 
    206237{ 
     
    243274} 
    244275 
    245 ArticlePeer::doDeleteAll(); 
    246 $article1 = new Article(); 
    247 $article1->setTitle('abc'); 
    248 $article1->save(); 
    249 $article2 = new Article(); 
    250 $article2->setTitle('def'); 
    251 $article2->save(); 
    252 $article3 = new Article(); 
    253 $article3->setTitle('bbc'); 
    254 $article3->save(); 
    255  
    256276$article = sfPropelFinder::from('Article')->whereTitle('abc')->findOne(); 
    257 $t->is($article->getId(), $article1->getId(), 'whereXXX() accepts a simple column name like whereColumnName()'); 
     277$t->is($article->getId(), $article1->getId(), 'whereXXX() adds a where condition according to the XXX column name'); 
    258278$article = sfPropelFinder::from('Article')->whereArticle_Title('abc')->findOne(); 
    259279$t->is($article->getId(), $article1->getId(), 'whereXXX() accepts a complete column name like whereClassName_ColumnName()'); 
     
    262282$articles = sfPropelFinder::from('Article')->whereTitle(Criteria::LIKE, '%bc')->find(); 
    263283$t->is(count($articles), 2, 'whereXXX() accepts a comparator as first argument when two arguments are given'); 
    264 $articles = sfPropelFinder::from('Article')->whereTitle('like', '%bc')->find(); 
    265 $t->is(count($articles), 2, 'whereXXX() accepts a text comparator and is permissive on syntax'); 
    266 $articles = sfPropelFinder::from('Article')->whereTitle('is not null', null)->find(); 
    267 $t->is(count($articles), 3, 'whereXXX() accepts a text comparator and is permissive on syntax'); 
    268  
    269  
    270 $t->diag('orderBy() magic'); 
     284 
     285 
     286$t->diag('orderBy()'); 
     287 
     288ArticlePeer::doDeleteAll(); 
     289$article1 = new Article(); 
     290$article1->setTitle('bbbbb'); 
     291$article1->save(); 
     292$article2 = new Article(); 
     293$article2->setTitle('aaaaa'); 
     294$article2->save(); 
     295$article3 = new Article(); 
     296$article3->setTitle('ccccc'); 
     297$article3->save(); 
     298 
     299$article = sfPropelFinder::from('Article')->orderBy('Title')->findOne(); 
     300$t->is($article->getTitle(), 'aaaaa', 'orderBy() orders by column asc by default'); 
     301 
     302$article = sfPropelFinder::from('Article')->orderBy('Title', 'asc')->findOne(); 
     303$t->is($article->getTitle(), 'aaaaa', 'orderBy() orders by column and takes an order as second argument'); 
     304 
     305$article = sfPropelFinder::from('Article')->orderBy('Title', 'desc')->findOne(); 
     306$t->is($article->getTitle(), 'ccccc', 'orderBy() orders by column and takes an order as second argument'); 
     307 
     308try 
     309
     310  $article = sfPropelFinder::from('Article')->orderBy('Title', 'abc'); 
     311  $t->fail('orderBy() throws an exception when called with a second argument different from "asc" or "desc"'); 
     312
     313catch (Exception $e) 
     314
     315  $t->pass('orderBy() throws an exception when called with a second argument different from "asc" or "desc"'); 
     316
     317 
    271318try 
    272319{ 
     
    299346} 
    300347 
    301 try 
    302 { 
    303   $article = sfPropelFinder::from('Article')->orderByTitle('abc'); 
    304   $t->fail('orderByXXX() throws an exception when called with an argument different from "asc" or "desc"'); 
    305 } 
    306 catch (Exception $e) 
    307 { 
    308   $t->pass('orderByXXX() throws an exception when called with an argument different from "asc" or "desc"'); 
    309 } 
    310  
    311 ArticlePeer::doDeleteAll(); 
    312 $article1 = new Article(); 
    313 $article1->setTitle('bbbbb'); 
    314 $article1->save(); 
    315 $article2 = new Article(); 
    316 $article2->setTitle('aaaaa'); 
    317 $article2->save(); 
    318 $article3 = new Article(); 
    319 $article3->setTitle('ccccc'); 
    320 $article3->save(); 
    321  
    322348$article = sfPropelFinder::from('Article')->orderByTitle()->findOne(); 
    323 $t->is($article->getTitle(), 'aaaaa', 'orderByXXX() orders by column asc by default'); 
     349$t->is($article->getTitle(), 'aaaaa', 'orderByXXX() orders by column according to the XXX column name'); 
    324350 
    325351$article = sfPropelFinder::from('Article')->orderByTitle('asc')->findOne(); 
    326 $t->is($article->getTitle(), 'aaaaa', 'orderByXXX() orders by column and takes an order as argment'); 
     352$t->is($article->getTitle(), 'aaaaa', 'orderByXXX() takes an order as argument'); 
    327353 
    328354$article = sfPropelFinder::from('Article')->orderByTitle('desc')->findOne(); 
    329 $t->is($article->getTitle(), 'ccccc', 'orderByXXX() orders by column and takes an order as argment'); 
    330  
    331 $t->diag('join() magic'); 
     355$t->is($article->getTitle(), 'ccccc', 'orderByXXX() takes an order as argument'); 
     356 
     357$t->diag('join()'); 
    332358 
    333359list($column1, $column2) = sfPropelFinder::from('Article')->getRelation('Category'); 
     
    357383$article3->setCategory($category2); 
    358384$article3->save(); 
    359 $nbArticles = sfPropelFinder::from('Article')->joinCategory()->whereCategory_Name('cat1')->count(); 
    360 $t->is($nbArticles, 2, 'joinXXX() allows to join to another table (many-to-one)'); 
    361 $nbArticles = sfPropelFinder::from('Article')->joinCategory()->whereCategory_Name('cat2')->count(); 
    362 $t->is($nbArticles, 1, 'joinXXX() allows to join to another table (many-to-one)'); 
    363 $article = sfPropelFinder::from('Article')->joinCategory()->whereCategory_Name('cat2')->findOne(); 
    364 $t->is($article->getTitle(), 'ccccc', 'joinXXX() allows to join to another table (many-to-one)'); 
     385$nbArticles = sfPropelFinder::from('Article')->join('Category')->where('Category_Name', 'cat1')->count(); 
     386$t->is($nbArticles, 2, 'join() allows to join to another table (many-to-one)'); 
     387$nbArticles = sfPropelFinder::from('Article')->join('Category')->where('Category_Name', 'cat2')->count(); 
     388$t->is($nbArticles, 1, 'join() allows to join to another table (many-to-one)'); 
     389$article = sfPropelFinder::from('Article')->join('Category')->where('Category_Name', 'cat2')->findOne(); 
     390$t->is($article->getTitle(), 'ccccc', 'join() allows to join to another table (many-to-one)'); 
    365391ArticlePeer::doDeleteAll(); 
    366392CommentPeer::doDeleteAll(); 
     
    377403$comment->setArticle($article2); 
    378404$comment->save(); 
    379 $nbArticles = sfPropelFinder::from('Article')->joinComment()->whereComment_Content('foo')->count(); 
    380 $t->is($nbArticles, 1, 'joinXXX() allows to join to another table (one-to-many)'); 
    381 $article = sfPropelFinder::from('Article')->joinComment()->whereComment_Content('foo')->findOne(); 
    382 $t->is($article->getTitle(), 'bbbbb', 'joinXXX() allows to join to another table (one-to-many)'); 
     405$nbArticles = sfPropelFinder::from('Article')->join('Comment')->where('Comment_Content', 'foo')->count(); 
     406$t->is($nbArticles, 1, 'join() allows to join to another table (one-to-many)'); 
     407$article = sfPropelFinder::from('Article')->join('Comment')->where('Comment_Content', 'foo')->findOne(); 
     408$t->is($article->getTitle(), 'bbbbb', 'join() allows to join to another table (one-to-many)'); 
    383409 
    384410ArticlePeer::doDeleteAll(); 
     
    397423$comment->setAuthor($author1); 
    398424$comment->save(); 
    399 $article = sfPropelFinder::from('Article')->joinComment()->joinAuthor()->whereAuthor_Name('John')->findOne(); 
    400 $t->is($article->getTitle(), 'aaaaa', 'you can chain several joinXXX() statements'); 
     425$article = sfPropelFinder::from('Article')->join('Comment')->join('Author')->where('Author_Name', 'John')->findOne(); 
     426$t->is($article->getTitle(), 'aaaaa', 'you can chain several join() statements'); 
     427$article = sfPropelFinder::from('Article')->joinComment()->joinAuthor()->where('Author_Name', 'John')->findOne(); 
     428$t->is($article->getTitle(), 'aaaaa', 'joinXXX() does a join according to the XXX column name'); 
    401429 
    402430$t->diag('Debugging functions'); 
    403431 
    404 $finder = sfPropelFinder::from('Article')->whereTitle('foo'); 
     432$finder = sfPropelFinder::from('Article')->where('Title', 'foo'); 
    405433$t->isa_ok($finder->getCriteria(), 'Criteria', 'getCriteria() returns the criteria as composed by the finder'); 
    406434$finder->findOne();