Changeset 8370
- Timestamp:
- 04/09/08 12:10:14 (6 months ago)
- Files:
-
- plugins/sfPropelFinderPlugin/README (modified) (3 diffs)
- plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php (modified) (12 diffs)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelFinderPlugin/README
r8168 r8370 226 226 }}} 227 227 228 If you're not sure about what query is issued by the finder, you can always check the SQL code after executing a termination method (`find()`, `findOne()`, `findPk()`, `count()`, `delete()`)by calling the `getLatestQuery()` method.228 If 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. 229 229 230 230 {{{ … … 232 232 <?php 233 233 $finder = sfPropelFinder::from('Article')->where('Title', 'foo'); 234 echo $finder->getCriteria()->toString(); 235 // SELECT FROM article WHERE article.TITLE=? 234 236 $finder->findOne(); 235 $query =$finder->getLatestQuery();237 echo $finder->getLatestQuery(); 236 238 // 'SELECT article.ID, article.VERSION, article.TITLE, article.CATEGORY_ID FROM article WHERE article.TITLE=\'foo\' LIMIT 1' 237 239 }}} … … 244 246 245 247 == Changelog == 248 249 === 2008-04-08 | Trunk === 250 251 * francois: Added support for magic `andXXX()` and `orXXX()` methods. 252 * jug: Fixed `_and()` and `_or()` so that they give expected results, rather than the buggy results of Propel's `addAnd()` and `addOr()` 246 253 247 254 === 2008-03-31 | 0.2.0 Beta === plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php
r8212 r8370 17 17 protected $relations = null; 18 18 protected $latestQuery = ''; 19 19 protected $criterions = array(); 20 20 21 public function __construct($class = '') 21 22 { … … 47 48 $this->relations[]= $peerClass; 48 49 $this->peerClass = $peerClass; 50 49 51 return $this; 50 52 } … … 52 54 public function getCriteria() 53 55 { 54 return $this-> criteria;56 return $this->buildCriteria(); 55 57 } 56 58 … … 58 60 { 59 61 $this->criteria = $criteria; 62 $this->criterions = array(); 63 60 64 return $this; 61 65 } 62 66 63 67 public function reinitCriteria() 64 { 65 $this->criteria = new Criteria(); 66 return $this; 68 { 69 return $this->setCriteria(new Criteria()); 67 70 } 68 71 … … 103 106 public function count($con = null, $reinitCriteria = true) 104 107 { 105 $ret = call_user_func(array($this->getPeerClass(), 'doCount'), $this-> criteria, $con);108 $ret = call_user_func(array($this->getPeerClass(), 'doCount'), $this->getCriteria(), $con); 106 109 $this->updateLatestQuery(); 107 110 if($reinitCriteria) … … 119 122 $this->criteria->setLimit($limit); 120 123 } 121 $ret = call_user_func(array($this->getPeerClass(), 'doSelect'), $this-> criteria, $con);124 $ret = call_user_func(array($this->getPeerClass(), 'doSelect'), $this->getCriteria(), $con); 122 125 $this->updateLatestQuery(); 123 126 if($reinitCriteria) … … 131 134 public function findOne($con = null, $reinitCriteria = true) 132 135 { 133 $ret = call_user_func(array($this->getPeerClass(), 'doSelectOne'), $this-> criteria, $con);136 $ret = call_user_func(array($this->getPeerClass(), 'doSelectOne'), $this->getCriteria(), $con); 134 137 $this->updateLatestQuery(); 135 138 if($reinitCriteria) … … 158 161 public function delete($con = null, $reinitCriteria = true) 159 162 { 160 $deleteCriteria = clone $this->criteria;163 $deleteCriteria = $this->getCriteria(); 161 164 if($deleteCriteria->equals(new Criteria())) 162 165 { … … 214 217 } 215 218 list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments); 216 $this->criteria->add($column, $value, $comparison); 219 220 $this->addCondition('and', $column, $value, $comparison ); 217 221 218 222 return $this; … … 240 244 } 241 245 list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments); 242 $this->criteria->addAnd($column, $value, $comparison); 246 247 $this->addCondition('and', $column, $value, $comparison); 243 248 244 249 return $this; … … 266 271 } 267 272 list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments); 268 $this->criteria->addOr($column, $value, $comparison); 269 270 return $this; 273 $this->addCondition('or', $column, $value, $comparison); 274 275 return $this; 276 } 277 278 /** 279 * Conditions have to be stocked before being really used 280 * cf. sfPropelFinder::buildCriteria() 281 */ 282 protected function addCondition($cond, $column, $value, $comparison) 283 { 284 $criterion = $this->criteria->getNewCriterion($column, $value, $comparison); 285 $criterion->func = "add".$cond; 286 $this->criterions []= $criterion; 287 } 288 289 /** 290 * We want that the Finder fuild Interface works like: 291 * PHP : whereA()->_andB->_orC()->_orD()->_andE() 292 * SQL : where A=? AND (B=? OR (C=? OR (D=? AND E=?))) 293 * So we have to add condition starting by the last one! 294 */ 295 protected function buildCriteria() 296 { 297 $criteria = clone $this->criteria; 298 $criterions = $this->criterions; 299 300 while ($criterion = array_pop($criterions)) 301 { 302 if ($c = count($criterions)) 303 { 304 call_user_func(array($criterions[$c-1], $criterion->func), $criterion); 305 } 306 else 307 { 308 call_user_func(array($criteria, $criterion->func), $criterion); 309 } 310 } 311 312 return $criteria; 271 313 } 272 314 … … 453 495 return $this->_and(substr($name, 4), $arguments); 454 496 } 497 if(strpos($name, 'and') === 0) 498 { 499 return $this->_and(substr($name, 3), $arguments); 500 } 455 501 if(strpos($name, '_or') === 0) 456 502 { 457 503 return $this->_or(substr($name, 3), $arguments); 504 } 505 if(strpos($name, 'or') === 0) 506 { 507 return $this->_or(substr($name, 2), $arguments); 458 508 } 459 509 if(method_exists($this->criteria, $name)) plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php
r8168 r8370 63 63 ArticlePeer::doDeleteAll(); 64 64 65 $t = new lime_test(7 0, new lime_output_color());65 $t = new lime_test(77, new lime_output_color()); 66 66 67 67 $t->diag('find()'); … … 283 283 $t->is(count($articles), 2, 'whereXXX() accepts a comparator as first argument when two arguments are given'); 284 284 285 $t->diag('_and() and _or()'); 286 287 $columns = "article.ID, article.VERSION, article.TITLE, article.CATEGORY_ID"; 288 $baseSelect = "SELECT $columns FROM article WHERE "; 289 290 $finder = sfPropelFinder::from('Article')->whereTitle('foo')->_orTitle('bar'); 291 $finder->find(); 292 $t->is( 293 $finder->getLatestQuery(), 294 $baseSelect . "(article.TITLE='foo' OR article.TITLE='bar')", 295 '_or() adds a SQL OR clause when called on a column where there is already a condition' 296 ); 297 298 $finder = sfPropelFinder::from('Article')->whereTitle('foo')->_orCategoryId(1); 299 $finder->find(); 300 $t->is( 301 $finder->getLatestQuery(), 302 $baseSelect . "(article.TITLE='foo' OR article.CATEGORY_ID=1)", 303 '_or() adds a SQL OR clause when called on a column where there is no condition yet' 304 ); 305 306 $finder = sfPropelFinder::from('Article')->whereTitle('foo')->_andTitle('bar'); 307 $finder->find(); 308 $t->is( 309 $finder->getLatestQuery(), 310 $baseSelect . "(article.TITLE='foo' AND article.TITLE='bar')", 311 '_and() adds a SQL AND clause when called on a column where there is already a condition' 312 ); 313 314 $finder = sfPropelFinder::from('Article')->whereTitle('foo')->_andCategoryId(1); 315 $finder->find(); 316 $t->is( 317 $finder->getLatestQuery(), 318 $baseSelect . "(article.TITLE='foo' AND article.CATEGORY_ID=1)", 319 '_and() adds a SQL AND clause when called on a column where there is no condition yet' 320 ); 321 322 $finder = sfPropelFinder::from('Article')->whereCategoryId(1)->_andTitle('foo')->_orTitle('bar'); 323 $finder->find(); 324 $t->is( 325 $finder->getLatestQuery(), 326 $baseSelect . "(article.CATEGORY_ID=1 AND (article.TITLE='foo' OR article.TITLE='bar'))", 327 '_and() and _or() can be combined on the same finder' 328 ); 329 330 $finder = sfPropelFinder::from('Article')->joinCategory()->where('Category_Name', 'foo')->_or('Category_Name', 'bar'); 331 $finder->find(); 332 $t->is( 333 $finder->getLatestQuery(), 334 "SELECT $columns FROM article, category WHERE (category.NAME='foo' OR category.NAME='bar') AND article.CATEGORY_ID=category.ID", 335 '_or() works on a simple jointure' 336 ); 337 338 $finder = sfPropelFinder::from('Comment') 339 ->joinArticle() 340 ->joinAuthor() 341 ->where('Article_Title', 'foo') 342 ->_or('Author_Name', 'bar'); 343 $finder->find(); 344 $t->is( 345 $finder->getLatestQuery(), 346 "SELECT comment.ID, comment.CONTENT, comment.ARTICLE_ID, comment.AUTHOR_ID FROM comment, article, author WHERE (article.TITLE='foo' OR author.NAME='bar') AND comment.ARTICLE_ID=article.ID AND comment.AUTHOR_ID=author.ID", 347 '_or() works on a multiple jointure' 348 ); 285 349 286 350 $t->diag('orderBy()');