Development

Changeset 10341

You must first sign up to be able to contribute.

Changeset 10341

Show
Ignore:
Timestamp:
07/17/08 14:43:06 (4 months ago)
Author:
francois
Message:

sfPropelFinderPlugin Implemented sfDoctrineFinder::findOne(), findFirst(), findLast() and orderBy()

Files:

Legend:

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

    r10213 r10341  
    589589== Changelog == 
    590590 
    591 === 2008-07-10 | Trunk === 
    592  
     591=== 2008-07-17 | Trunk === 
     592 
     593 * francois: Implemented `sfDoctrineFinder::findOne()`, `findFirst()`, `findLast()` and `orderBy()` 
    593594 * francois: Initialized `DbFinder` and `sfDoctrineFinder` (WIP) 
    594595 
  • plugins/sfPropelFinderPlugin/lib/sfDoctrineFinder.php

    r10213 r10341  
    1313{ 
    1414  protected $class = null; 
     15  protected $object = null; 
    1516  protected $relations = null; 
    1617  protected $latestQuery = ''; 
     
    2526    { 
    2627      $this->class = $class; 
     28      $this->object = new $class(); 
    2729    } 
    2830  } 
     
    3032  public function getClass() 
    3133  { 
    32     return $this->peerClass; 
     34    return $this->class; 
    3335  } 
    3436 
     
    4143  } 
    4244   
     45  public function getObject() 
     46  { 
     47    return $this->object; 
     48  } 
     49 
    4350  public function getLatestQuery($con = null) 
    4451  { 
     
    8289  } 
    8390   
     91  /** 
     92   * Class initializer 
     93   * 
     94   * @param string $from Doctrine classname on which the search will be done 
     95   * @return sfDoctrineFinder a finder object 
     96   */ 
     97  public static function fromClass($class) 
     98  { 
     99    $me = __CLASS__; 
     100    $finder = new $me($class); 
     101     
     102    return $finder; 
     103  } 
     104   
     105   
    84106  // Finder Executers 
    85107   
     
    91113  public function find($limit = null, $con = null, $reinitCriteria = false) 
    92114  { 
     115    if($limit) 
     116    { 
     117      $this->query->limit($limit); 
     118    } 
    93119    return $this->query->execute(); 
    94120  } 
     
    96122  public function findOne($con = null, $reinitCriteria = true) 
    97123  { 
    98     throw new Exception('This method is not yet implemented'); 
     124    $coll = $this->find(1, $con, $reinitCriteria); 
     125     
     126    return isset($coll[0]) ? $coll[0] : null; 
    99127  } 
    100128   
    101129  public function findLast($column = null, $con = null, $reinitCriteria = true) 
    102130  { 
    103     throw new Exception('This method is not yet implemented'); 
     131    if($column) 
     132    { 
     133      $this->orderBy($column, 'desc'); 
     134    } 
     135    else 
     136    { 
     137      $this->guessOrder('desc'); 
     138    } 
     139     
     140    return $this->findOne(); 
    104141  } 
    105142   
    106143  public function findFirst($column = null, $con = null, $reinitCriteria = true) 
    107144  { 
    108     throw new Exception('This method is not yet implemented'); 
     145    if($column) 
     146    { 
     147      $this->orderBy($column, 'asc'); 
     148    } 
     149    else 
     150    { 
     151      $this->guessOrder('asc'); 
     152    } 
     153     
     154    return $this->findOne(); 
    109155  } 
    110156   
     
    259305  } 
    260306   
    261   public function orderBy($columnName, $arguments = array()) 
    262   { 
    263     throw new Exception('This method is not yet implemented'); 
     307  public function orderBy($columnName, $order = 'asc') 
     308  { 
     309    $column = $this->getColName($columnName); 
     310    if(!in_array(strtolower($order), array('asc', 'desc'))) 
     311    { 
     312      throw new Exception('sfPropelFinder::orderBy() only accepts "asc" or "desc" as argument'); 
     313    } 
     314    $this->query->orderby(sprintf('%s %s', $column, strtoupper($order))); 
    264315     
    265316    return $this; 
     
    285336  public function guessOrder($direction = 'desc') 
    286337  { 
    287     throw new Exception('This method is not yet implemented'); 
     338    $columnNames = self::camelize($this->getObject()->getTable()->getColumnNames()); 
     339    foreach(sfConfig::get('app_sfPropelFinder_sort_column_guesses', array('UpdatedAt', 'UpdatedOn', 'CreatedAt', 'CreatedOn', 'Id')) as $testColumnName) 
     340    { 
     341      if(in_array($testColumnName, $columnNames)) 
     342      { 
     343        $this->orderBy($testColumnName, $direction); 
     344        return $this; 
     345      } 
     346    } 
    288347     
    289348    throw new Exception('Unable to figure out the column to use to order rows in sfPropelFinder::guessOrder()'); 
    290349  } 
    291350   
     351  protected static function camelize($arg) 
     352  { 
     353    if(is_array($arg)) 
     354    { 
     355      $ret = array(); 
     356      foreach ($arg as $arg1) 
     357      { 
     358        $ret []= self::camelize($arg1); 
     359      } 
     360      return $ret; 
     361    } 
     362    else 
     363    { 
     364      return sfInflector::camelize($arg); 
     365    } 
     366  } 
     367 
     368  protected static function underscore($arg) 
     369  { 
     370    if(is_array($arg)) 
     371    { 
     372      $ret = array(); 
     373      foreach ($arg as $arg1) 
     374      { 
     375        $ret []= self::underscore($arg1); 
     376      } 
     377      return $ret; 
     378    } 
     379    else 
     380    { 
     381      return sfInflector::underscore($arg); 
     382    } 
     383  } 
     384   
     385   
    292386  /** 
    293387   * Infers $column1, $column2 and $operator from $relatedClass and some optional arguments 
     
    328422  } 
    329423   
    330   protected function getColName($phpName, $peerClass = null, $withPeerClass = false, $autoAddJoin = true) 
    331   { 
    332     throw new Exception('This method is not yet implemented'); 
     424  protected function getColName($phpName, $class = null, $autoAddJoin = true) 
     425  { 
     426    if(strpos($phpName, '.') !== false) 
     427    { 
     428      // Table.Column 
     429      list($class, $phpName) = explode('.', $phpName); 
     430    } 
     431    else if(strpos($phpName, '_') !== false) 
     432    { 
     433      // Table_Column, or Table_Name_Column, so explode is not a solution here 
     434      $limit = strrpos($phpName, '_'); 
     435      $class = substr($phpName, 0, $limit); 
     436      $phpName = substr($phpName, $limit + 1); 
     437    } 
     438    else 
     439    { 
     440      // Column 
     441      if(!$class) 
     442      { 
     443        // TODO: guess class 
     444        $class = $this->getClass(); 
     445      } 
     446    } 
     447     
     448    return self::underscore($phpName); 
    333449  } 
    334450   
  • plugins/sfPropelFinderPlugin/test/unit/sfDoctrineFinderTest.php

    r10213 r10341  
    1313The tests expect a model similar to this one: 
    1414 
     15    connection:    doctrine 
    1516    DArticle: 
    1617      columns: 
     
    6364Doctrine_Query::create()->delete()->from('DArticle')->execute(); 
    6465 
    65 $t = new lime_test(8, new lime_output_color()); 
     66$t = new lime_test(18, new lime_output_color()); 
    6667 
    6768$t->diag('find()'); 
     
    101102$t->is($article->getTitle(), 'foo3', 'find() with no argument returns an array of all the records'); 
    102103 
     104$finder = new sfDoctrineFinder('DArticle'); 
     105$articles = $finder->find(2); 
     106$t->is(count($articles), 2, 'find() with an argument returns a limited array of records'); 
     107 
     108$t->diag('findOne()'); 
     109 
     110Doctrine_Query::create()->delete()->from('DArticle')->execute(); 
     111 
     112$finder = new sfDoctrineFinder('DArticle'); 
     113$article = $finder->findOne(); 
     114$t->is($article, null, 'findOne() returns null when no records match'); 
     115 
     116$article1 = new DArticle(); 
     117$article1->setTitle('foo'); 
     118$article1->save(); 
     119 
     120$article2 = new DArticle(); 
     121$article2->setTitle('foo2'); 
     122$article2->save(); 
     123 
     124$finder = new sfDoctrineFinder('DArticle'); 
     125$article = $finder->findOne(); 
     126$t->isa_ok($article, 'DArticle', 'findOne() returns a single object'); 
     127$t->is($article->getTitle(), 'foo', 'findOne() returns the first object matching the conditions'); 
     128 
     129$t->diag('findLast() and findFirst()'); 
     130 
     131Doctrine_Query::create()->delete()->from('DArticle')->execute(); 
     132 
     133$finder = new sfDoctrineFinder('DArticle'); 
     134$article = $finder->findFirst(); 
     135$t->is($article, null, 'findFirst() returns null when no records match'); 
     136 
     137$finder = new sfDoctrineFinder('DArticle'); 
     138$article = $finder->findLast(); 
     139$t->is($article, null, 'findLast() returns null when no records match'); 
     140 
     141$article1 = new DArticle(); 
     142$article1->setTitle('foo'); 
     143$article1->save(); 
     144 
     145$article2 = new DArticle(); 
     146$article2->setTitle('foo2'); 
     147$article2->save(); 
     148 
     149$finder = new sfDoctrineFinder('DArticle'); 
     150$article = $finder->findFirst(); 
     151$t->isa_ok($article, 'DArticle', 'findFirst() returns a single object'); 
     152$t->is($article->getTitle(), 'foo', 'findFirst() returns the last object matching the conditions'); 
     153 
     154$finder = new sfDoctrineFinder('DArticle'); 
     155$article = $finder->findLast(); 
     156$t->isa_ok($article, 'DArticle', 'findLast() returns a single object'); 
     157$t->is($article->getTitle(), 'foo2', 'findLast() returns the last object matching the conditions');