Changeset 9919
- Timestamp:
- 06/27/08 11:30:42 (2 months ago)
- Files:
-
- plugins/sfPropelFinderPlugin/README (modified) (3 diffs)
- plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php (modified) (1 diff)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelFinderPlugin/README
r9911 r9919 103 103 $articleFinder = sfPropelFinder::from('Article'); 104 104 // Finding all Articles ordered by created_at (ascending order by default) 105 $articles = $articleFinder->orderBy('CreatedAt')->find(); 105 $articles = $articleFinder-> 106 orderBy('CreatedAt')-> 107 find(); 106 108 // Finding all Articles ordered by created_at desc 107 $articles = $articleFinder->orderBy('CreatedAt', 'desc')->find(); 109 $articles = $articleFinder-> 110 orderBy('CreatedAt', 'desc') 111 ->find(); 108 112 // You can also use the magic orderByXXX() method 109 $articles = $articleFinder->orderByCreatedAt()->find(); 113 $articles = $articleFinder 114 ->orderByCreatedAt()- 115 >find(); 110 116 }}} 111 117 … … 128 134 129 135 The syntax should remind you of `sfFinder` and `sfTestBrowser`. 136 137 === Finding records related to another one === 138 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 143 $comments = $article->getComments(); 144 }}} 145 146 You could do the same with a finder: 147 {{{ 148 #!php 149 <?php 150 $commentFinder = sfPropelFinder::from('Comment'); 151 $comments = $commentFinder-> 152 where('ArticleId', $article->getId())-> 153 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 160 $comments = $commentFinder-> 161 relatedTo($article)-> 162 find(); 163 }}} 164 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: 166 167 {{{ 168 #!php 169 <?php 170 // Retrieving the related comments, orderd by date 171 $comments = $commentFinder-> 172 relatedTo($article)-> 173 orderBy('CreatedAt', 'desc')-> 174 find(); 175 // Retrieving the last one of the related comments 176 $comments = $commentFinder-> 177 relatedTo($article)-> 178 findLast(); 179 }}} 180 181 Compare it to the code required to get these `Comment` objects without `sfPropelFinder`, and you will understand all the benefits the `relatedTo()` method provide. 130 182 131 183 === Joins === … … 367 419 === 2008-06-27 | Trunk === 368 420 421 * francois: Added `sfPropelFinder::relatedTo()` method 369 422 * francois: Added `sfPropelFinder::findFirst()` and `sfPropelFinder::findLast()` methods 370 423 * francois: Added `sfPropelFinder::withColumn()` method plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php
r9912 r9919 606 606 if($c->getRelatedTableName() == $relatedObjectTableName) 607 607 { 608 $this->addCondition('and', $c->getFullyQualifiedName(), $object->getByName($c->getRelated ColumnName()), Criteria::EQUAL);608 $this->addCondition('and', $c->getFullyQualifiedName(), $object->getByName($c->getRelatedName(), BasePeer::TYPE_COLNAME), Criteria::EQUAL); 609 609 } 610 610 } plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php
r9912 r9919 63 63 ArticlePeer::doDeleteAll(); 64 64 65 $t = new lime_test(1 07, new lime_output_color());65 $t = new lime_test(112, new lime_output_color()); 66 66 67 67 $t->diag('find()'); … … 430 430 $finder = sfPropelFinder::from('Article')->relatedTo($category1); 431 431 $articles = $finder->find(); 432 432 $t->is(count($articles), 2, 'relatedTo() filters results to the ones relative to a record'); 433 foreach($articles as $article) 434 { 435 $t->is($article->getCategoryId(), $category1->getId(), 'relatedTo() filters results to the ones relative to a record'); 436 } 437 $finder = sfPropelFinder::from('Article')->relatedTo($category2); 438 $articles = $finder->find(); 439 $t->is(count($articles), 1, 'relatedTo() filters results to the ones relative to a record'); 440 foreach($articles as $article) 441 { 442 $t->is($article->getCategoryId(), $category2->getId(), 'relatedTo() filters results to the ones relative to a record'); 443 } 433 444 434 445 $t->diag('orderBy()');