Changeset 10068
- Timestamp:
- 07/02/08 16:40:21 (2 months ago)
- Files:
-
- plugins/sfPropelFinderPlugin/README (modified) (2 diffs)
- plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php (modified) (20 diffs)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelFinderPlugin/README
r10065 r10068 387 387 }}} 388 388 389 === Updating objects === 390 391 {{{ 392 #!php 393 <?php 394 $article1 = new Article; 395 $article1->setTitle('foo'); 396 $article1->save(); 397 $article2 = new Article; 398 $article2->setTitle('bar'); 399 $article2->save(); 400 401 // set() issues an UPDATE ... SET query based on an associative array column => value 402 sfPropelFinder::from('Article')-> 403 where('Title', 'foo')-> 404 set(array('Title' => 'updated title')); // 1 405 406 // set() returns the number of modified columns 407 sfPropelFinder::from('Article')-> 408 where('Title', 'updated title')-> 409 count(); // 1 410 }}} 411 389 412 === Writing your own business logic into a finder === 390 413 … … 448 471 === 2008-07-02 | Trunk === 449 472 473 * francois: Added `sfPropelFinder::set()` method (based on a patch by jug) 450 474 * francois: Added `sfPropelFinder::withI18n()` method 451 475 * francois: Added `sfPropelFinderPager` class and `sfPropelFinder::paginate()` method plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php
r10067 r10068 76 76 } 77 77 78 public function getLatestQuery( )79 { 80 $con = Propel::getConnection();78 public function getLatestQuery($con = null) 79 { 80 if(is_null($con)) $con = Propel::getConnection(); 81 81 if(method_exists($con, 'getLastExecutedQuery')) 82 82 { … … 85 85 else 86 86 { 87 throw new RuntimeException(' getLatestQuery() only works when debug mode is enabled');88 } 89 } 90 91 public function updateLatestQuery( )92 { 93 $con = Propel::getConnection();87 throw new RuntimeException('sfPropelFinder::getLatestQuery() only works when debug mode is enabled'); 88 } 89 } 90 91 public function updateLatestQuery($con = null) 92 { 93 if(is_null($con)) $con = Propel::getConnection(); 94 94 if(method_exists($con, 'getLastExecutedQuery')) 95 95 { … … 149 149 return self::fromCollection($from); 150 150 } 151 throw new Exception(' from() only accepts a Propel object classname or an array of Propel objects');151 throw new Exception('sfPropelFinder::from() only accepts a Propel object classname or an array of Propel objects'); 152 152 } 153 153 … … 185 185 if($class) 186 186 { 187 throw new Exception('A finder can only be initialized from an array of objects of a single class');187 throw new Exception('A sfPropelFinder can only be initialized from an array of objects of a single class'); 188 188 } 189 189 if($object instanceof BaseObject) … … 193 193 else 194 194 { 195 throw new Exception('A finder can only be initialized from an array of Propel objects');195 throw new Exception('A sfPropelFinder can only be initialized from an array of Propel objects'); 196 196 } 197 197 } … … 200 200 if(!$class) 201 201 { 202 throw new Exception('A finder cannot be initialized with an empty array');202 throw new Exception('A sfPropelFinder cannot be initialized with an empty array'); 203 203 } 204 204 … … 210 210 if($pkName) 211 211 { 212 throw new Exception('A finder cannot be initialized from an array of objects with several foreign keys');212 throw new Exception('A sfPropelFinder cannot be initialized from an array of objects with several foreign keys'); 213 213 } 214 214 else … … 243 243 { 244 244 $ret = call_user_func(array($this->getPeerClass(), 'doCount'), $this->getCriteria(), $con); 245 $this->updateLatestQuery( );245 $this->updateLatestQuery($con); 246 246 if($reinitCriteria) 247 247 { … … 259 259 } 260 260 $ret = $this->doFind($this->getCriteria(), $con); 261 $this->updateLatestQuery( );261 $this->updateLatestQuery($con); 262 262 if($reinitCriteria) 263 263 { … … 272 272 $this->criteria->setLimit(1); 273 273 $ret = $this->doFind($this->getCriteria(), $con); 274 $this->updateLatestQuery( );274 $this->updateLatestQuery($con); 275 275 if($reinitCriteria) 276 276 { … … 338 338 $ret = call_user_func(array($this->getPeerClass(), 'retrieveByPk'), $pk, $con); 339 339 } 340 $this->updateLatestQuery( );340 $this->updateLatestQuery($con); 341 341 342 342 return $ret; … … 355 355 } 356 356 $ret = call_user_func(array($this->getPeerClass(), 'doDelete'), $deleteCriteria, $con); 357 $this->updateLatestQuery( );357 $this->updateLatestQuery($con); 358 358 if($reinitCriteria) 359 359 { … … 374 374 return $pager; 375 375 } 376 377 public function set($values, $con = null, $reinitCriteria = true ) 378 { 379 if (!is_array($values)) 380 { 381 throw new Exception('sfPropelFinder::set() expects an array as first argument'); 382 } 383 384 $find = $this->getCriteria(); 385 if (count($find->getJoins())) 386 { 387 throw new Exception('sfPropelFinder::set() does not support multitable updates, please do not use join'); 388 } 389 if($find->equals(new Criteria())) 390 { 391 // doUpdate will delete nothing when passed an empty criteria 392 // while it should, in fact, update all 393 $fieldNames = call_user_func(array($this->getPeerClass(), 'getFieldNames'), BasePeer::TYPE_COLNAME); 394 $firstFieldName = $fieldNames[0]; 395 $find->add($firstFieldName, true, Criteria::BINARY_OR); 396 } 397 398 $set = new Criteria(); 399 foreach ($values as $columnName => $value) 400 { 401 $set->add($this->getColName($columnName), $value); 402 } 403 404 if(is_null($con)) $con = Propel::getConnection(); 405 $ret = BasePeer::doUpdate($find, $set, $con); 406 $this->updateLatestQuery($con); 407 if($reinitCriteria) 408 { 409 $this->reinitCriteria(); 410 } 411 412 return $ret; 413 } 414 376 415 377 416 public function doFind($criteria, $con = null) … … 558 597 if($isCalculationColumn) 559 598 { 560 throw new Exception('Calculated colums added with withColumn() need an alias as second parameter');599 throw new Exception('Calculated colums added with sfPropelFinder::withColumn() need an alias as second parameter'); 561 600 } 562 601 else … … 736 775 break; 737 776 default: 738 throw new Exception(' {sfPropelFinder} whereXXXcan only be called with one or two arguments');777 throw new Exception('sfPropelFinder::whereXXX() can only be called with one or two arguments'); 739 778 } 740 779 … … 795 834 break; 796 835 default: 797 throw new Exception(' {sfPropelFinder} orderByonly accepts "asc" or "desc" as argument');836 throw new Exception('sfPropelFinder::orderBy() only accepts "asc" or "desc" as argument'); 798 837 } 799 838 … … 842 881 } 843 882 844 throw new Exception('Unable to figure out the column to use to order rows ');883 throw new Exception('Unable to figure out the column to use to order rows in sfPropelFinder::guessOrder()'); 845 884 } 846 885 … … 889 928 } 890 929 } 891 throw new Exception(sprintf(' {sfPropelFinder}%s has no %s related table', $this->peerClass, $phpName));930 throw new Exception(sprintf('sfPropelFinder: %s has no %s related table', $this->peerClass, $phpName)); 892 931 } 893 932 … … 955 994 catch (PropelException $e) 956 995 { 957 throw new Exception(sprintf(' {sfPropelFinder}%s has no %s column', $peerClass, $phpName));996 throw new Exception(sprintf('sfPropelFinder: %s has no %s column', $peerClass, $phpName)); 958 997 } 959 998 } … … 1004 1043 return $this; 1005 1044 } 1006 throw new Exception(sprintf(' {sfPropelFinder} Undefined method %s', $name));1045 throw new Exception(sprintf('Undefined method sfPropelFinder::%s()', $name)); 1007 1046 } 1008 1047 } plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php
r10065 r10068 65 65 ArticlePeer::doDeleteAll(); 66 66 67 $t = new lime_test(13 2, new lime_output_color());67 $t = new lime_test(139, new lime_output_color()); 68 68 69 69 $t->diag('find()'); … … 842 842 $t->is($finder->getLatestQuery(), 'SELECT article.ID, article.VERSION, article.TITLE, article.CATEGORY_ID, COUNT(comment.ID) AS NbComments FROM article, comment WHERE article.ID=comment.ARTICLE_ID GROUP BY NbComments LIMIT 1', 'Columns added with withColumn() can be used for grouping'); 843 843 844 $t->diag('set()'); 845 846 ArticlePeer::doDeleteAll(); 847 848 $article1 = new Article; 849 $article1->setTitle('foo'); 850 $article1->save(); 851 $article2 = new Article; 852 $article2->setTitle('bar'); 853 $article2->save(); 854 855 $finder = sfPropelFinder::from('Article'); 856 $t->is($finder->set(array('Title' => 'updated title')), 2, 'set() returns the number of updated rows'); 857 $t->is($finder->getLatestQuery(), 'UPDATE article SET TITLE = \'updated title\' WHERE article.ID|1', 'set() issues an update query even when passed an empty finder'); 858 $t->is(sfPropelFinder::from('Article')->where('Title', 'updated title')->count(), 2, 'set() updates all records when passed an empty finder'); 859 860 ArticlePeer::doDeleteAll(); 861 862 $article1 = new Article; 863 $article1->setTitle('foo'); 864 $article1->save(); 865 $article2 = new Article; 866 $article2->setTitle('bar'); 867 $article2->save(); 868 869 $finder = sfPropelFinder::from('Article')->where('Title', 'foo'); 870 $t->is($finder->set(array('Title' => 'updated title')), 1, 'set() returns the number of updated rows'); 871 $t->is($finder->getLatestQuery(), 'UPDATE article SET TITLE = \'updated title\' WHERE article.TITLE=\'foo\'', 'set() issues an Update query when passed a finder'); 872 $t->is(sfPropelFinder::from('Article')->where('Title', 'updated title')->count(), 1, 'set() updates only the records found based on the array of values'); 873 874 875 try 876 { 877 sfPropelFinder::from('Comment')->joinArticle()->where('Article_Title', 'updated title')->set(array('Version' => 3)); 878 $t->fail('set() throws an exception when called on a finder with join()'); 879 } 880 catch( Exception $e ) 881 { 882 $t->pass('set() throws an exception when called on a finder with join()'); 883 } 884 844 885 $t->diag('Debugging functions'); 845 886