Development

Changeset 9941

You must first sign up to be able to contribute.

Changeset 9941

Show
Ignore:
Timestamp:
06/27/08 18:55:31 (2 months ago)
Author:
francois
Message:

sfPropelFinderPlugin Added sfPropelFinder::findByXXX() and sfPropelFinder::findOneByXXX() methods

Files:

Legend:

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

    r9919 r9941  
    6161// Finding a single Article 
    6262$article = sfPropelFinder::from('Article')->findOne(); 
    63 // Finding the last Article of a list (the finder will figure out the column to use for sorting) 
     63// Finding the last Article (the finder will figure out the column to use for sorting) 
    6464$article = sfPropelFinder::from('Article')->findLast(); 
    6565}}} 
     
    9494// You can also use the magic whereXXX() method, removing the column argument and concatenating it to the method name 
    9595$articles = $articleFinder->whereTitle('foo')->find(); 
     96// Or, when your search is on a single column, use the magic findByXXX() method 
     97$articles = $articleFinder->findByTitle('foo'); 
    9698}}} 
    9799 
     
    108110// Finding all Articles ordered by created_at desc 
    109111$articles = $articleFinder-> 
    110   orderBy('CreatedAt', 'desc') 
    111   ->find(); 
     112  orderBy('CreatedAt', 'desc')-> 
     113  find(); 
    112114// You can also use the magic orderByXXX() method 
    113 $articles = $articleFinder 
    114   ->orderByCreatedAt()- 
    115   >find(); 
     115$articles = $articleFinder-> 
     116  orderByCreatedAt()-> 
     117  find(); 
    116118}}} 
    117119 
    118120=== Combining methods === 
    119121 
    120 The methods of the `sfPropelFinder` object return the current finder object, so you can chain them together in a single call, and finish by a `find()` to launch the query. 
     122The methods of the `sfPropelFinder` object return the current finder object, so you can chain them together in a single call, and finish by any of the `find()` methods to launch the query. 
    121123 
    122124{{{ 
     
    137139=== Finding records related to another one === 
    138140 
    139 As you can add condition, it is easy to find records related to another one. Imagine that you have an Article object and that you want to retrieve its comments. The usual Propel way of doing it is to call the generated Propel getter: 
    140 {{{ 
    141 #!php 
    142 <?php 
     141{{{ 
     142#!php 
     143<?php 
     144// Propel way 
    143145$comments = $article->getComments(); 
    144 }}} 
    145  
    146 You could do the same with a finder: 
    147 {{{ 
    148 #!php 
    149 <?php 
     146// sfPropelFinder way 
    150147$commentFinder = sfPropelFinder::from('Comment'); 
    151148$comments = $commentFinder-> 
    152149  where('ArticleId', $article->getId())-> 
    153150  find(); 
    154 }}} 
    155  
    156 Or you could use the `relatedTo()` method, which guesses the local and foreign columns to check based on your schema definition: 
    157 {{{ 
    158 #!php 
    159 <?php 
     151// Or let the finder guess local and foreign columns based on the schema 
    160152$comments = $commentFinder-> 
    161153  relatedTo($article)-> 
     
    163155}}} 
    164156 
    165 But since the finder way is longer than the native Propel way, what is the interest of using this `relatedTo()`? You get is a `sfPropelFinder` object when you use `relatedTo()`, so it allows you to do things that the generated Propel getter don't allow: 
     157Since the finder way is longer than the native Propel way, what is the interest of using this `relatedTo()`? You get a `sfPropelFinder` object when you use `relatedTo()`, so it allows you to do things that the generated Propel getter don't allow: 
    166158 
    167159{{{ 
     
    283275  findOne(); 
    284276$categoryName = $article->getColumn('Category_Name');  // No supplementary query 
    285 }}} 
    286  
    287 Beware that in this case, the related `Category` object is not hydrated, since `with()` was not used. That means that retrieving the related `Category` object will issue a new database query, so use `withColumn()` only when you need one or two supplementary columns instead of the whole object. 
    288 {{{ 
    289 #!php 
    290 <?php 
     277 
     278// Beware that in this case, the related `Category` object is not hydrated, since `with()` was not used. 
     279// That means that retrieving the related `Category` object will issue a new database query, 
     280// so use `withColumn()` only when you need one or two supplementary columns instead of the whole object. 
    291281$categoryName = $article->getCategory()->getName();  // One supplementary query 
    292 }}} 
    293  
    294 Just like `with()`, `withColumn()` will add an internal join automatically (based on the TableMap) if you don't do it yourself in the finder: 
    295 {{{ 
    296 #!php 
    297 <?php 
     282 
     283// Just like with(), withColumn() adds an internal join if you don't do it yourself 
    298284$article = sfPropelFinder::from('Article')-> 
    299285  withColumn('Category_Name')-> 
    300286  findOne(); 
    301287$categoryName = $article->getColumn('Category_Name');  // Works without a call to `join('Category')` 
    302 }}} 
    303  
    304  
    305 This `withColumn()` method can use a column alias as second argument. 
    306 {{{ 
    307 #!php 
    308 <?php 
     288 
     289// withColumn() can use a column alias as second argument. 
    309290$article = sfPropelFinder::from('Article')-> 
    310291  join('Category')-> 
     
    312293  findOne(); 
    313294$categoryName = $article->getColumn('category'); 
    314 }}} 
    315  
    316 This is particularly useful if you want to reuse a calculated column for sorting or grouping: 
    317 {{{ 
    318 #!php 
    319 <?php 
     295 
     296// This is particularly useful if you want to reuse a calculated column for sorting or grouping 
    320297$articles = sfPropelFinder::from('Article')-> 
    321298  join('Comment')-> 
     
    323300  orderBy('NbComments')-> 
    324301  find(); 
    325 }}} 
    326  
    327 Lastly, the supplementary columns added with `withColumn()` are considered string by default, by you can force another data type by providing a third argument: 
    328 {{{ 
    329 #!php 
    330 <?php 
     302 
     303// Lastly, the supplementary columns added with withColumn() are considered string by default 
     304// But you can force another data type by providing a third argument 
    331305$article = sfPropelFinder::from('Article')-> 
    332306  join('Category')-> 
     
    362336You can create a new finder for your objects, with custom methods. The only prerequisites are to extend `sfPropelFinder` and to define a protected `$peerClass` property. Then, the object has access to a protected `$criteria` property, which is a Propel Criteria that can be augmented in the usual way. Don't forget to return the current object (`$this`) in the new methods. 
    363337 
    364 For instance, to add a `recent()` method to an article finder: 
    365  
    366 {{{ 
    367 #!php 
    368 <?php 
     338{{{ 
     339#!php 
     340<?php 
     341// For instance, add a `recent()` method to an article finder 
    369342class ArticleFinder extends sfPropelFinder 
    370343{ 
     
    373346  public function recent() 
    374347  { 
    375     $this->criteria->add( 
    376       ArticlePeer::CREATED_AT, 
    377       time() - sfConfig::get('app_recent_days', 5) * 24 * 60 * 60, 
    378       Criteria::GREATER_THAN 
    379     ); 
    380     return $this; 
     348    return $this->where('CreatedAt', '>=', time() - sfConfig::get('app_recent_days', 5) * 24 * 60 * 60); 
    381349  } 
    382350} 
     351// You can now use your custom finder and its methods together with the usual ones 
     352$articleFinder = new ArticleFinder(); 
     353$articles = $articleFinder-> 
     354  recent()-> 
     355  orderByTitle()-> 
     356  find(); 
    383357}}} 
    384358 
     
    419393=== 2008-06-27 | Trunk === 
    420394 
     395 * francois: Added `sfPropelFinder::findByXXX()` and `sfPropelFinder::findOneByXXX()` methods 
    421396 * francois: Added `sfPropelFinder::relatedTo()` method 
    422397 * francois: Added `sfPropelFinder::findFirst()` and `sfPropelFinder::findLast()` methods 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r9928 r9941  
    226226     
    227227    throw new Exception('Unable to figure out the column to use to order rows'); 
     228  } 
     229   
     230  public function findBy($columnName, $value, $limit = null, $con = null, $reinitCriteria = false) 
     231  { 
     232    $column = $this->getColName($columnName); 
     233    $this->addCondition('and', $column, $value, Criteria::EQUAL); 
     234     
     235    return $this->find($limit, $con, $reinitCriteria); 
     236  } 
     237 
     238  public function findOneBy($columnName, $value, $con = null, $reinitCriteria = false) 
     239  { 
     240    $column = $this->getColName($columnName); 
     241    $this->addCondition('and', $column, $value, Criteria::EQUAL); 
     242     
     243    return $this->findOne($con, $reinitCriteria); 
    228244  } 
    229245   
     
    810826      return $this->_or(substr($name, 2), $arguments); 
    811827    } 
     828    if(strpos($name, 'findBy') === 0) 
     829    { 
     830      array_unshift($arguments, substr($name, 6)); 
     831      return call_user_func_array(array($this, 'findBy'), $arguments); 
     832    } 
     833    if(strpos($name, 'findOneBy') === 0) 
     834    { 
     835      array_unshift($arguments, substr($name, 9)); 
     836      return call_user_func_array(array($this, 'findOneBy'), $arguments); 
     837    } 
    812838    if(method_exists($this->criteria, $name)) 
    813839    { 
  • plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php

    r9919 r9941  
    6363ArticlePeer::doDeleteAll(); 
    6464 
    65 $t = new lime_test(112, new lime_output_color()); 
     65$t = new lime_test(122, new lime_output_color()); 
    6666 
    6767$t->diag('find()'); 
     
    153153$t->isa_ok($article, 'Article', 'findLast() returns a single object'); 
    154154$t->is($article->getTitle(), 'foo2', 'findLast() returns the last object matching the conditions'); 
     155 
     156$t->diag('findBy() and findOneBy'); 
     157 
     158ArticlePeer::doDeleteAll(); 
     159 
     160$article1 = new Article(); 
     161$article1->setTitle('foo'); 
     162$article1->save(); 
     163 
     164$article2 = new Article(); 
     165$article2->setTitle('foo2'); 
     166$article2->save(); 
     167 
     168$article3 = new Article(); 
     169$article3->setTitle('foo'); 
     170$article3->save(); 
     171 
     172$articles = sfPropelFinder::from('Article')->findBy('Title', 'foo'); 
     173$t->is(count($articles), 2, 'findBy() adds a condition on a given column'); 
     174foreach ($articles as $article) 
     175{ 
     176  $t->is($article->getTitle(), 'foo', 'findBy() adds a condition on a given column'); 
     177} 
     178 
     179$articles = sfPropelFinder::from('Article')->findBy('Title', 'foo', 1); 
     180$t->is(count($articles), 1, 'findBy() accepts a limit parameter'); 
     181 
     182$article = sfPropelFinder::from('Article')->findOneBy('Title', 'foo2'); 
     183$t->is($article->getTitle(), 'foo2', 'findOneBy() adds a condition on a given column'); 
     184 
     185$articles = sfPropelFinder::from('Article')->findByTitle('foo'); 
     186$t->is(count($articles), 2, 'findByXXX() adds a condition on a given column'); 
     187foreach ($articles as $article) 
     188{ 
     189  $t->is($article->getTitle(), 'foo', 'findByXXX() adds a condition on a given column'); 
     190} 
     191 
     192$articles = sfPropelFinder::from('Article')->findByTitle('foo', 1); 
     193$t->is(count($articles), 1, 'findByXXX() accepts a limit parameter'); 
     194 
     195$article = sfPropelFinder::from('Article')->findOneByTitle('foo2'); 
     196$t->is($article->getTitle(), 'foo2', 'findOneByXXX() adds a condition on a given column'); 
    155197 
    156198$t->diag('findPk()');