Development

Changeset 9911

You must first sign up to be able to contribute.

Changeset 9911

Show
Ignore:
Timestamp:
06/27/08 10:48:13 (5 years ago)
Author:
francois
Message:

sfPropelFinderPlugin Added sfPropelFinder::findFirst() and sfPropelFinder::findLast() methods

Files:

Legend:

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

    r9889 r9911  
    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) 
     64$article = sfPropelFinder::from('Article')->findLast(); 
    6365}}} 
    6466 
     
    363365== Changelog == 
    364366 
    365 === 2008-06-26 | Trunk === 
    366  
     367=== 2008-06-27 | Trunk === 
     368 
     369 * francois: Added `sfPropelFinder::findFirst()` and `sfPropelFinder::findLast()` methods 
    367370 * francois: Added `sfPropelFinder::withColumn()` method 
    368371 * jug: Fixed problem in a particular join case 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r9889 r9911  
    179179    } 
    180180    return null; 
     181  } 
     182   
     183  public function findLast($con = null, $reinitCriteria = true) 
     184  { 
     185    $this->guessOrder('desc'); 
     186     
     187    return $this->findOne($con, $reinitCriteria); 
     188  } 
     189   
     190  public function findFirst($con = null, $reinitCriteria = true) 
     191  { 
     192    $this->guessOrder('asc'); 
     193     
     194    return $this->findOne($con, $reinitCriteria); 
     195  } 
     196 
     197  protected function guessOrder($direction = 'desc') 
     198  { 
     199    $columnNames = array(); 
     200    foreach ($this->getColumnsForPeerClass($this->getPeerClass()) as $c) 
     201    { 
     202      $columnNames []= $c->getPhpName(); 
     203    } 
     204    foreach(array('UpdatedAt', 'UpdatedOn', 'CreatedAt', 'CreatedOn', 'Id') as $testColumnName) 
     205    { 
     206      if(in_array($testColumnName, $columnNames)) 
     207      { 
     208        $this->orderBy($testColumnName, $direction); 
     209        return; 
     210      } 
     211    } 
     212     
     213    throw new Exception('Unable to figure out the column to use to order rows'); 
    181214  } 
    182215   
  • plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php

    r9889 r9911  
    6363ArticlePeer::doDeleteAll(); 
    6464 
    65 $t = new lime_test(100, new lime_output_color()); 
     65$t = new lime_test(107, new lime_output_color()); 
    6666 
    6767$t->diag('find()'); 
     
    7070$articles = $finder->find(); 
    7171$t->is($articles, array(), 'find() returns an empty array when no records match'); 
    72  
    73 $finder = new sfPropelFinder('Article'); 
    74 $article = $finder->findOne(); 
    75 $t->is($article, null, 'findOne() returns null when no records match'); 
    7672 
    7773$article1 = new Article(); 
     
    107103$t->is(count($articles), 2, 'find() with an argument returns a limited array of records'); 
    108104 
     105$t->diag('findOne()'); 
     106 
     107ArticlePeer::doDeleteAll(); 
     108 
    109109$finder = new sfPropelFinder('Article'); 
    110110$article = $finder->findOne(); 
    111 $t->is($article->getTitle(), 'foo', 'findOne() returns a single object'); 
     111$t->is($article, null, 'findOne() returns null when no records match'); 
     112 
     113$article1 = new Article(); 
     114$article1->setTitle('foo'); 
     115$article1->save(); 
     116 
     117$article2 = new Article(); 
     118$article2->setTitle('foo2'); 
     119$article2->save(); 
     120 
     121$finder = new sfPropelFinder('Article'); 
     122$article = $finder->findOne(); 
     123$t->isa_ok($article, 'Article', 'findOne() returns a single object'); 
     124$t->is($article->getTitle(), 'foo', 'findOne() returns the first object matching the conditions'); 
     125 
     126$t->diag('findLast() and findFirst()'); 
     127 
     128ArticlePeer::doDeleteAll(); 
     129 
     130$finder = new sfPropelFinder('Article'); 
     131$article = $finder->findFirst(); 
     132$t->is($article, null, 'findFirst() returns null when no records match'); 
     133 
     134$finder = new sfPropelFinder('Article'); 
     135$article = $finder->findLast(); 
     136$t->is($article, null, 'findLast() returns null when no records match'); 
     137 
     138$article1 = new Article(); 
     139$article1->setTitle('foo'); 
     140$article1->save(); 
     141 
     142$article2 = new Article(); 
     143$article2->setTitle('foo2'); 
     144$article2->save(); 
     145 
     146$finder = new sfPropelFinder('Article'); 
     147$article = $finder->findFirst(); 
     148$t->isa_ok($article, 'Article', 'findFirst() returns a single object'); 
     149$t->is($article->getTitle(), 'foo', 'findFirst() returns the last object matching the conditions'); 
     150 
     151$finder = new sfPropelFinder('Article'); 
     152$article = $finder->findLast(); 
     153$t->isa_ok($article, 'Article', 'findLast() returns a single object'); 
     154$t->is($article->getTitle(), 'foo2', 'findLast() returns the last object matching the conditions'); 
    112155 
    113156$t->diag('findPk()'); 
     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('foo3'); 
     170$article3->save(); 
    114171 
    115172$finder = new sfPropelFinder('Article');