Development

Changeset 9919

You must first sign up to be able to contribute.

Changeset 9919

Show
Ignore:
Timestamp:
06/27/08 11:30:42 (2 months ago)
Author:
francois
Message:

sfPropelFinderPlugin Added sfPropelFinder::relatedTo() method

Files:

Legend:

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

    r9911 r9919  
    103103$articleFinder = sfPropelFinder::from('Article'); 
    104104// 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(); 
    106108// Finding all Articles ordered by created_at desc 
    107 $articles = $articleFinder->orderBy('CreatedAt', 'desc')->find(); 
     109$articles = $articleFinder-> 
     110  orderBy('CreatedAt', 'desc') 
     111  ->find(); 
    108112// You can also use the magic orderByXXX() method 
    109 $articles = $articleFinder->orderByCreatedAt()->find(); 
     113$articles = $articleFinder 
     114  ->orderByCreatedAt()- 
     115  >find(); 
    110116}}} 
    111117 
     
    128134 
    129135The syntax should remind you of `sfFinder` and `sfTestBrowser`. 
     136 
     137=== Finding records related to another one === 
     138 
     139As 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 
     146You 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 
     156Or 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 
     165But 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 
     181Compare it to the code required to get these `Comment` objects without `sfPropelFinder`, and you will understand all the benefits the `relatedTo()` method provide. 
    130182 
    131183=== Joins === 
     
    367419=== 2008-06-27 | Trunk === 
    368420 
     421 * francois: Added `sfPropelFinder::relatedTo()` method 
    369422 * francois: Added `sfPropelFinder::findFirst()` and `sfPropelFinder::findLast()` methods 
    370423 * francois: Added `sfPropelFinder::withColumn()` method 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r9912 r9919  
    606606      if($c->getRelatedTableName() == $relatedObjectTableName) 
    607607      { 
    608         $this->addCondition('and', $c->getFullyQualifiedName(), $object->getByName($c->getRelatedColumnName()), Criteria::EQUAL); 
     608        $this->addCondition('and', $c->getFullyQualifiedName(), $object->getByName($c->getRelatedName(), BasePeer::TYPE_COLNAME), Criteria::EQUAL); 
    609609      } 
    610610    } 
  • plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php

    r9912 r9919  
    6363ArticlePeer::doDeleteAll(); 
    6464 
    65 $t = new lime_test(107, new lime_output_color()); 
     65$t = new lime_test(112, new lime_output_color()); 
    6666 
    6767$t->diag('find()'); 
     
    430430$finder = sfPropelFinder::from('Article')->relatedTo($category1); 
    431431$articles = $finder->find(); 
    432  
     432$t->is(count($articles), 2, 'relatedTo() filters results to the ones relative to a record'); 
     433foreach($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'); 
     440foreach($articles as $article) 
     441
     442  $t->is($article->getCategoryId(), $category2->getId(), 'relatedTo() filters results to the ones relative to a record'); 
     443
    433444 
    434445$t->diag('orderBy()');