Development

Changeset 10809 for plugins/sfPropelFinderPlugin/README

You must first sign up to be able to contribute.

Show
Ignore:
Timestamp:
08/12/08 15:40:27 (3 months ago)
Author:
francois
Message:

Play well with symfony-project site Markdown code highliter

Files:

Legend:

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

    r10808 r10809  
    1818The idea behind this plugin is to write queries to retrieve model objects through an ORM, but fast. Inspired by Doctrine, Rails has_finder plugin and SQLAlchemy, `DbFinder` can be seen as "jQuery for symfony's model layer". It also aims at putting the things in the right order, meaning that writing a `find()` query will feel natural for those familiar with SQL. 
    1919 
     20    [php] 
    2021    // With Propel Peer and Criteria 
    2122    $c = new Criteria() 
     
    3738You can also implement your own business logic to encapsulate complex queries, so that your queries look like real language: 
    3839 
     40    [php] 
    3941    // ArticleFinder extends sfPropelFinder. See how below 
    4042    $finder = new ArticleFinder(); 
     
    5759### Finding objects 
    5860 
     61    [php] 
    5962    // Finding all Articles 
    6063    $articles = DbFinder::from('Article')->find(); 
     
    7073### Adding WHERE clause 
    7174 
     75    [php] 
    7276    $articleFinder = DbFinder::from('Article'); 
    7377    // Finding all Articles where title = 'foo' 
     
    99103### Ordering results 
    100104 
     105    [php] 
    101106    $articleFinder = DbFinder::from('Article'); 
    102107    // Finding all Articles ordered by created_at (ascending order by default) 
     
    117122The methods of the `DbFinder` 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. 
    118123 
     124    [php] 
    119125    // everything chained together 
    120126    $articles = DbFinder::from('Article')->where('Title', 'like', '%world')->where('IsPublished', true)->orderBy('CreatedAt')->find(); 
     
    130136### Finding records related to another one 
    131137 
     138    [php] 
    132139    // Propel way 
    133140    $comments = $article->getComments(); 
     
    144151Since the finder way is longer than the native Propel way, what is the interest of using this `relatedTo()`? You get a `DbFinder` object when you use `relatedTo()`, so it allows you to do things that the generated Propel getter don't allow: 
    145152 
     153    [php] 
    146154    // Retrieving the related comments, orderd by date 
    147155    $comments = $commentFinder-> 
     
    158166**Tip**: Alternatively, a finder can be initialized from an array of Propel object. The resulting SQL query contains a 'IN ()' clause, so use this possibility with caution. 
    159167 
     168    [php] 
    160169    // Retrieving the last one of the related comments 
    161170    $comments = DbFinder::from($article->getComments())-> 
     
    164173### Joins 
    165174 
     175    [php] 
    166176    // Test data 
    167177    $article1 = new Article(); 
     
    241251### Complex logic 
    242252 
     253    [php] 
    243254    // where() and orWhere() only allow simple logical operations on a single condition 
    244255    // For more complex logic, you have to use combine() 
     
    282293Even if you do a Join, Propel or Doctrine will issue new queries when you fetch related objects: 
    283294 
     295    [php] 
    284296    $comment = DbFinder::from('Comment')-> 
    285297      join('Article')-> 
     
    290302Just as Propel offers generated `doSelectJoinXXX()` methods, `DbFinder` allows you to hydrate related objects in a single query - you just have to call the `with()` method to specify which objects the main object should be hydrated with. 
    291303 
     304    [php] 
    292305    $comment = DbFinder::from('Comment')-> 
    293306      with('Article')-> 
     
    299312The power of the `with()` method is that it can guess relationships just as well as `join()`, and will add the call to `join()` if you didn't do it yourself. So you can do for instance: 
    300313 
     314    [php] 
    301315    $category1 = new Category(); 
    302316    $category1->setName('Category1'); 
     
    321335The `with()` method can also hydrate the related I18n objects, thus providing an equivalent to symfony's `doSelectWithI18n()` methods. 
    322336 
     337    [php] 
    323338    // Consider the following schema 
    324339    //article: 
     
    349364Warning: The `withColumn()` feature requires symfony's Behavior system. It will only work if you enable behaviors in `propel.ini` and rebuild your model afterwards. 
    350365 
     366    [php] 
    351367    $article = DbFinder::from('Article')-> 
    352368      join('Category')-> 
     
    395411### Counting objects 
    396412 
     413    [php] 
    397414    // Counting all Articles 
    398415    $nbArticles = DbFinder::from('Article')->count(); 
     
    400417### Getting a paginated list of results 
    401418 
     419    [php] 
    402420    // Getting an initialized sfPropelPager object 
    403421    $pager = DbFinder::from('Article')->paginate($currentPage = 1, $maxResultsPerPage = 10); 
     
    414432### Deleting objects 
    415433 
     434    [php] 
    416435    // Deleting all Articles 
    417436    $nbArticles = DbFinder::from('Article')->delete(); 
     
    423442### Updating objects 
    424443 
     444    [php] 
    425445    $article1 = new Article; 
    426446    $article1->setTitle('foo'); 
     
    453473For instance, you can create an child of `DbFinder` to retrieve Propel `Article` objects. This new finder has access to a protected query object by way of `getQueryObject()`. This object 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. 
    454474 
     475    [php] 
    455476    class ArticleFinder extends DbFinder 
    456477    { 
     
    471492**Tip**: Once you define an `ArticleFinder` class, any call to `DbFinder::from('Article')` will return an instance of `ArticleFinder` instead of an instance of `DbFinder`. So the following also works: 
    472493 
     494    [php] 
    473495    $articleFinder = DbFinder::from('Article')-> 
    474496      recent()-> 
     
    480502### Finding Objects From A Primary Key 
    481503 
     504    [php] 
    482505    $article = DbFinder::from('Article')->findPk(123); 
    483506    // is equivalent to 
     
    496519### Using Class Shortcuts 
    497520 
     521    [php] 
    498522    $article = DbFinder::from('Article a')-> 
    499523      where('a.Title', 'foo')-> 
     
    508532If the finder doesn't (yet) provide the method to build the query you need, you can still call `Criteria` methods on the `sfPropelFinder` objects, or call `Doctrine_Query` methods on the `sfDoctrineFinder` objects, and they will be applied to the finder's internal query object. 
    509533 
     534    [php] 
    510535    $articles = DbFinder::from('Article')-> 
    511536      where('Title', 'like', 'foo%')-> 
     
    515540If you're not sure about what query is issued by the finder, you can always check the SQL code before executing a termination method by calling `getCriteria()->toString()`, or after executing a termination method by calling the `getLatestQuery()` method. 
    516541 
     542    [php] 
    517543    $finder = DbFinder::from('Article')->where('Title', 'foo'); 
    518544    echo $finder->getCriteria()->toString();