Changeset 9911
- Timestamp:
- 06/27/08 10:48:13 (5 years ago)
- Files:
-
- plugins/sfPropelFinderPlugin/README (modified) (2 diffs)
- plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php (modified) (1 diff)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelFinderPlugin/README
r9889 r9911 61 61 // Finding a single Article 62 62 $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(); 63 65 }}} 64 66 … … 363 365 == Changelog == 364 366 365 === 2008-06-26 | Trunk === 366 367 === 2008-06-27 | Trunk === 368 369 * francois: Added `sfPropelFinder::findFirst()` and `sfPropelFinder::findLast()` methods 367 370 * francois: Added `sfPropelFinder::withColumn()` method 368 371 * jug: Fixed problem in a particular join case plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php
r9889 r9911 179 179 } 180 180 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'); 181 214 } 182 215 plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php
r9889 r9911 63 63 ArticlePeer::doDeleteAll(); 64 64 65 $t = new lime_test(10 0, new lime_output_color());65 $t = new lime_test(107, new lime_output_color()); 66 66 67 67 $t->diag('find()'); … … 70 70 $articles = $finder->find(); 71 71 $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');76 72 77 73 $article1 = new Article(); … … 107 103 $t->is(count($articles), 2, 'find() with an argument returns a limited array of records'); 108 104 105 $t->diag('findOne()'); 106 107 ArticlePeer::doDeleteAll(); 108 109 109 $finder = new sfPropelFinder('Article'); 110 110 $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 128 ArticlePeer::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'); 112 155 113 156 $t->diag('findPk()'); 157 158 ArticlePeer::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(); 114 171 115 172 $finder = new sfPropelFinder('Article');