Changeset 9941
- Timestamp:
- 06/27/08 18:55:31 (2 months ago)
- Files:
-
- plugins/sfPropelFinderPlugin/README (modified) (11 diffs)
- plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php (modified) (2 diffs)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelFinderPlugin/README
r9919 r9941 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)63 // Finding the last Article (the finder will figure out the column to use for sorting) 64 64 $article = sfPropelFinder::from('Article')->findLast(); 65 65 }}} … … 94 94 // You can also use the magic whereXXX() method, removing the column argument and concatenating it to the method name 95 95 $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'); 96 98 }}} 97 99 … … 108 110 // Finding all Articles ordered by created_at desc 109 111 $articles = $articleFinder-> 110 orderBy('CreatedAt', 'desc') 111 ->find();112 orderBy('CreatedAt', 'desc')-> 113 find(); 112 114 // You can also use the magic orderByXXX() method 113 $articles = $articleFinder 114 ->orderByCreatedAt()-115 >find();115 $articles = $articleFinder-> 116 orderByCreatedAt()-> 117 find(); 116 118 }}} 117 119 118 120 === Combining methods === 119 121 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.122 The 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. 121 123 122 124 {{{ … … 137 139 === Finding records related to another one === 138 140 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 #!php142 <?php 141 {{{ 142 #!php 143 <?php 144 // Propel way 143 145 $comments = $article->getComments(); 144 }}} 145 146 You could do the same with a finder: 147 {{{ 148 #!php 149 <?php 146 // sfPropelFinder way 150 147 $commentFinder = sfPropelFinder::from('Comment'); 151 148 $comments = $commentFinder-> 152 149 where('ArticleId', $article->getId())-> 153 150 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 160 152 $comments = $commentFinder-> 161 153 relatedTo($article)-> … … 163 155 }}} 164 156 165 But since the finder way is longer than the native Propel way, what is the interest of using this `relatedTo()`? You get isa `sfPropelFinder` object when you use `relatedTo()`, so it allows you to do things that the generated Propel getter don't allow:157 Since 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: 166 158 167 159 {{{ … … 283 275 findOne(); 284 276 $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. 291 281 $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 298 284 $article = sfPropelFinder::from('Article')-> 299 285 withColumn('Category_Name')-> 300 286 findOne(); 301 287 $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. 309 290 $article = sfPropelFinder::from('Article')-> 310 291 join('Category')-> … … 312 293 findOne(); 313 294 $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 320 297 $articles = sfPropelFinder::from('Article')-> 321 298 join('Comment')-> … … 323 300 orderBy('NbComments')-> 324 301 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 331 305 $article = sfPropelFinder::from('Article')-> 332 306 join('Category')-> … … 362 336 You 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. 363 337 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 369 342 class ArticleFinder extends sfPropelFinder 370 343 { … … 373 346 public function recent() 374 347 { 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); 381 349 } 382 350 } 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(); 383 357 }}} 384 358 … … 419 393 === 2008-06-27 | Trunk === 420 394 395 * francois: Added `sfPropelFinder::findByXXX()` and `sfPropelFinder::findOneByXXX()` methods 421 396 * francois: Added `sfPropelFinder::relatedTo()` method 422 397 * francois: Added `sfPropelFinder::findFirst()` and `sfPropelFinder::findLast()` methods plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php
r9928 r9941 226 226 227 227 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); 228 244 } 229 245 … … 810 826 return $this->_or(substr($name, 2), $arguments); 811 827 } 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 } 812 838 if(method_exists($this->criteria, $name)) 813 839 { plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php
r9919 r9941 63 63 ArticlePeer::doDeleteAll(); 64 64 65 $t = new lime_test(1 12, new lime_output_color());65 $t = new lime_test(122, new lime_output_color()); 66 66 67 67 $t->diag('find()'); … … 153 153 $t->isa_ok($article, 'Article', 'findLast() returns a single object'); 154 154 $t->is($article->getTitle(), 'foo2', 'findLast() returns the last object matching the conditions'); 155 156 $t->diag('findBy() and findOneBy'); 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('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'); 174 foreach ($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'); 187 foreach ($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'); 155 197 156 198 $t->diag('findPk()');