Development

Changeset 10047

You must first sign up to be able to contribute.

Changeset 10047

Show
Ignore:
Timestamp:
07/01/08 18:49:24 (5 years ago)
Author:
francois
Message:

sfPropelFinderPlugin Added sfPropelFinderPager class, sfPropelFinder::paginate() and sfPropelFinder::groupBy() methods

Files:

Legend:

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

    r9948 r10047  
    329329}}} 
    330330 
     331=== Getting a paginated list of results === 
     332 
     333{{{ 
     334#!php 
     335<?php 
     336// Getting an initialized sfPropelPager object 
     337$pager = sfPropelFinder::from('Article')->paginate($currentPage = 1, $maxResultsPerPage = 10); 
     338// You can use the pager object as usual 
     339printf("Showing results %d to %d on %d\n", 
     340  $pager->getfirstIndice(), 
     341  $pager->getLastIndice(), 
     342  $pager->getNbResults()); 
     343foreach($pager->getResuts() as $article) 
     344{ 
     345  echo $article->getTitle(); 
     346} 
     347}}} 
     348 
    331349=== Deleting objects === 
    332350 
     
    401419== Changelog == 
    402420 
    403 === 2008-06-27 | Trunk === 
    404  
     421=== 2008-07-01 | Trunk === 
     422 
     423 * francois: Added `sfPropelFinderPager` class and `sfPropelFinder::paginate()` method 
     424 * francois: Added `sfPropelFinder::groupBy()` method 
    405425 * francois: `sfPropelFinder::from()` now accepts an array of Propel objects 
    406426 * francois: Added `sfPropelFinder::findByXXX()` and `sfPropelFinder::findOneByXXX()` methods 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r9948 r10047  
    1212class sfPropelFinder 
    1313{ 
     14  protected $class = null; 
    1415  protected $peerClass = null; 
    1516  protected $databaseMap = null; 
     
    2627    if($class) 
    2728    { 
    28       $this->setPeerClass($class.'Peer'); 
     29      $this->class = $class; 
     30      $tmp = new $class; 
     31      $this->setPeerClass(get_class($tmp->getPeer())); 
    2932    } 
    3033    if($this->peerClass) 
     
    307310    return $this->findOne($con, $reinitCriteria); 
    308311  } 
    309  
    310   protected function guessOrder($direction = 'desc') 
    311   { 
    312     $columnNames = array(); 
    313     foreach ($this->getColumnsForPeerClass($this->getPeerClass()) as $c) 
    314     { 
    315       $columnNames []= $c->getPhpName(); 
    316     } 
    317     foreach(sfConfig::get('app_sfPropelFinder_sort_column_guesses', array('UpdatedAt', 'UpdatedOn', 'CreatedAt', 'CreatedOn', 'Id')) as $testColumnName) 
    318     { 
    319       if(in_array($testColumnName, $columnNames)) 
    320       { 
    321         $this->orderBy($testColumnName, $direction); 
    322         return; 
    323       } 
    324     } 
    325      
    326     throw new Exception('Unable to figure out the column to use to order rows'); 
    327   } 
    328312   
    329313  public function findBy($columnName, $value, $limit = null, $con = null, $reinitCriteria = false) 
     
    356340     
    357341    return $ret; 
     342  } 
     343   
     344  public function delete($con = null, $reinitCriteria = true) 
     345  { 
     346    $deleteCriteria = $this->getCriteria(); 
     347    if($deleteCriteria->equals(new Criteria())) 
     348    { 
     349      // delete will delete nothing when passed an empty criteria 
     350      // while it should, in fact, delete all 
     351      $fieldNames = call_user_func(array($this->getPeerClass(), 'getFieldNames'), BasePeer::TYPE_COLNAME); 
     352      $firstFieldName = $fieldNames[0]; 
     353      $deleteCriteria->add($firstFieldName, true, Criteria::BINARY_OR); 
     354    } 
     355    $ret = call_user_func(array($this->getPeerClass(), 'doDelete'), $deleteCriteria, $con); 
     356    $this->updateLatestQuery(); 
     357    if($reinitCriteria) 
     358    { 
     359      $this->reinitCriteria(); 
     360    } 
     361     
     362    return $ret; 
     363  } 
     364   
     365  public function paginate($page = 1, $maxPerPage = 10, $con = null) 
     366  { 
     367    $pager = new sfPropelFinderPager($this->class, $maxPerPage); 
     368    $pager->setFinder($this); 
     369    $pager->setConnection($con); 
     370    $pager->setPage($page); 
     371    $pager->init(); 
     372     
     373    return $pager; 
    358374  } 
    359375   
     
    494510  } 
    495511   
    496   public function delete($con = null, $reinitCriteria = true) 
    497   { 
    498     $deleteCriteria = $this->getCriteria(); 
    499     if($deleteCriteria->equals(new Criteria())) 
    500     { 
    501       // delete will delete nothing when passed an empty criteria 
    502       // while it should, in fact, delete all 
    503       $fieldNames = call_user_func(array($this->getPeerClass(), 'getFieldNames'), BasePeer::TYPE_COLNAME); 
    504       $firstFieldName = $fieldNames[0]; 
    505       $deleteCriteria->add($firstFieldName, true, Criteria::BINARY_OR); 
    506     } 
    507     $ret = call_user_func(array($this->getPeerClass(), 'doDelete'), $deleteCriteria, $con); 
    508     $this->updateLatestQuery(); 
    509     if($reinitCriteria) 
    510     { 
    511       $this->reinitCriteria(); 
    512     } 
    513      
    514     return $ret; 
    515   } 
    516    
    517512  // Hydrating 
    518513   
     
    780775    return $this; 
    781776  } 
     777 
     778  /** 
     779   * Finder Fluid Interface for Criteria::addGroupByColumn() 
     780   * Infers $column and $order from $columnName and some optional arguments 
     781   * Examples: 
     782   *   $articleFinder->groupBy('CreatedAt') 
     783   *    => $c->addGroupByColumn(ArticlePeer::CREATED_AT) 
     784   */ 
     785  public function groupBy($columnName) 
     786  { 
     787    $column = $this->getColName($columnName); 
     788    $this->criteria->addGroupByColumn($column); 
     789     
     790    return $this; 
     791  } 
     792   
     793  /** 
     794   * Guess sort column based on their names 
     795   * Will look primarily for columns named: 
     796   * 'UpdatedAt', 'UpdatedOn', 'CreatedAt', 'CreatedOn', 'Id' 
     797   * You can change this sequence by modifying the app_sfPropelFinder_sort_column_guesses value 
     798   * 
     799   * @param string $direction 'desc' (default) or 'asc' 
     800   * 
     801   * @return sfPropelFinder $this the current finder object 
     802   */ 
     803  public function guessOrder($direction = 'desc') 
     804  { 
     805    $columnNames = array(); 
     806    foreach ($this->getColumnsForPeerClass($this->getPeerClass()) as $c) 
     807    { 
     808      $columnNames []= $c->getPhpName(); 
     809    } 
     810    foreach(sfConfig::get('app_sfPropelFinder_sort_column_guesses', array('UpdatedAt', 'UpdatedOn', 'CreatedAt', 'CreatedOn', 'Id')) as $testColumnName) 
     811    { 
     812      if(in_array($testColumnName, $columnNames)) 
     813      { 
     814        $this->orderBy($testColumnName, $direction); 
     815        return $this; 
     816      } 
     817    } 
     818     
     819    throw new Exception('Unable to figure out the column to use to order rows'); 
     820  } 
    782821   
    783822  /** 
  • plugins/sfPropelFinderPlugin/package.xml

    r8132 r10047  
    2828      <dir name="lib"> 
    2929        <file name="sfPropelFinder.php" role="data"/> 
     30        <file name="sfPropelFinderPager.php" role="data"/> 
    3031      </dir> 
    3132    </dir>