Changeset 10341
- Timestamp:
- 07/17/08 14:43:06 (4 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelFinderPlugin/README
r10213 r10341 589 589 == Changelog == 590 590 591 === 2008-07-10 | Trunk === 592 591 === 2008-07-17 | Trunk === 592 593 * francois: Implemented `sfDoctrineFinder::findOne()`, `findFirst()`, `findLast()` and `orderBy()` 593 594 * francois: Initialized `DbFinder` and `sfDoctrineFinder` (WIP) 594 595 plugins/sfPropelFinderPlugin/lib/sfDoctrineFinder.php
r10213 r10341 13 13 { 14 14 protected $class = null; 15 protected $object = null; 15 16 protected $relations = null; 16 17 protected $latestQuery = ''; … … 25 26 { 26 27 $this->class = $class; 28 $this->object = new $class(); 27 29 } 28 30 } … … 30 32 public function getClass() 31 33 { 32 return $this-> peerClass;34 return $this->class; 33 35 } 34 36 … … 41 43 } 42 44 45 public function getObject() 46 { 47 return $this->object; 48 } 49 43 50 public function getLatestQuery($con = null) 44 51 { … … 82 89 } 83 90 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 84 106 // Finder Executers 85 107 … … 91 113 public function find($limit = null, $con = null, $reinitCriteria = false) 92 114 { 115 if($limit) 116 { 117 $this->query->limit($limit); 118 } 93 119 return $this->query->execute(); 94 120 } … … 96 122 public function findOne($con = null, $reinitCriteria = true) 97 123 { 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; 99 127 } 100 128 101 129 public function findLast($column = null, $con = null, $reinitCriteria = true) 102 130 { 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(); 104 141 } 105 142 106 143 public function findFirst($column = null, $con = null, $reinitCriteria = true) 107 144 { 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(); 109 155 } 110 156 … … 259 305 } 260 306 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))); 264 315 265 316 return $this; … … 285 336 public function guessOrder($direction = 'desc') 286 337 { 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 } 288 347 289 348 throw new Exception('Unable to figure out the column to use to order rows in sfPropelFinder::guessOrder()'); 290 349 } 291 350 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 292 386 /** 293 387 * Infers $column1, $column2 and $operator from $relatedClass and some optional arguments … … 328 422 } 329 423 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); 333 449 } 334 450 plugins/sfPropelFinderPlugin/test/unit/sfDoctrineFinderTest.php
r10213 r10341 13 13 The tests expect a model similar to this one: 14 14 15 connection: doctrine 15 16 DArticle: 16 17 columns: … … 63 64 Doctrine_Query::create()->delete()->from('DArticle')->execute(); 64 65 65 $t = new lime_test( 8, new lime_output_color());66 $t = new lime_test(18, new lime_output_color()); 66 67 67 68 $t->diag('find()'); … … 101 102 $t->is($article->getTitle(), 'foo3', 'find() with no argument returns an array of all the records'); 102 103 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 110 Doctrine_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 131 Doctrine_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');