Changeset 8168
- Timestamp:
- 03/31/08 14:03:43 (8 months ago)
- Files:
-
- plugins/sfPropelFinderPlugin/README (modified) (10 diffs)
- plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php (modified) (5 diffs)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelFinderPlugin/README
r8132 r8168 70 70 $articleFinder = sfPropelFinder::from('Article'); 71 71 // Finding all Articles where title = 'foo' 72 $articles = $articleFinder->where('Title', 'foo')->find(); 73 // Finding all Articles where title like 'foo%' 74 $articles = $articleFinder->where('Title', 'like', 'foo%')->find(); 75 // Finding all Articles where published_at less than time() 76 $articles = $articleFinder->where('PublishedAt', '<', time())->find(); 77 78 // You can chain WHERE clauses 79 $articles = $articleFinder-> 80 where('Title', 'foo')-> 81 where('PublishedAt', '<', time())-> 82 find(); 83 // Or even better, use the _and() and _or() methods for SQL-like code 84 $articles = $articleFinder-> 85 where('Title', 'foo')-> 86 _and('PublishedAt', '<', time())-> 87 _or('Title', 'like', 'bar%')-> 88 find(); 89 90 // The where() method accepts simple or composed column names ('ClassName_ColumnName') 91 $articles = $articleFinder->where('Article_Title', 'foo')->find(); 92 // You can also use the magic whereXXX() method, removing the column argument and concatenating it to the method name 72 93 $articles = $articleFinder->whereTitle('foo')->find(); 73 // Finding all Articles where title like 'foo%'74 $articles = $articleFinder->whereTitle('like', 'foo%')->find();75 // Finding all Articles where published_at less than time()76 $articles = $articleFinder->wherePublishedAt('<', time())->find();77 // The whereXXX method accepts simple or composed column names78 $articles = $articleFinder->whereArticle_PublishedAt('<', time())->find();79 94 }}} 80 95 … … 85 100 <?php 86 101 $articleFinder = sfPropelFinder::from('Article'); 87 // Finding all Articles ordered by created_at ('asc') 102 // Finding all Articles ordered by created_at (ascending order by default) 103 $articles = $articleFinder->orderBy('CreatedAt')->find(); 104 // Finding all Articles ordered by created_at desc 105 $articles = $articleFinder->orderBy('CreatedAt', 'desc')->find(); 106 // You can also use the magic orderByXXX() method 88 107 $articles = $articleFinder->orderByCreatedAt()->find(); 89 // Finding all Articles ordered by created_at desc90 $articles = $articleFinder->orderByCreatedAt('desc')->find();91 }}}92 93 === Counting objects ===94 95 {{{96 #!php97 <?php98 // Counting all Articles99 $nbArticles = sfPropelFinder::from('Article')->count();100 108 }}} 101 109 … … 108 116 <?php 109 117 // everything chained together 110 $articles = sfPropelFinder::from('Article')->where Title('like', '%world')->whereIsPublished(true)->orderByCreatedAt()->find();118 $articles = sfPropelFinder::from('Article')->where('Title', 'like', '%world')->_and('IsPublished', true)->orderBy('CreatedAt')->find(); 111 119 // You can write it in several lines, too 112 120 $articles = sfPropelFinder::from('Article')-> 113 where Title('like', '%world')->114 whereIsPublished(true)->115 orderBy CreatedAt()->121 where('Title', 'like', '%world')-> 122 _and('IsPublished', true)-> 123 orderBy('CreatedAt')-> 116 124 find(); 117 125 }}} … … 135 143 // After all, the columns of the FK are already defined in the schema 136 144 $article = sfPropelFinder::from('Article')-> 137 join Comment()->138 where Comment_Content('You rock!')->145 join('Comment')-> 146 where('Comment_Content', 'You rock!')-> 139 147 findOne(); 140 148 // You can chain joins if you want to make it more complex 141 149 $article2 = new Article(); 142 $article2->setTitle('Hello gain, world!');150 $article2->setTitle('Hello again, world!'); 143 151 $article2->save(); 144 152 $author1 = new Author(); … … 151 159 $comment->save(); 152 160 $article = sfPropelFinder::from('Article')-> 153 join Comment()->154 join Author()->155 where Author_Name('John')->161 join('Comment')-> 162 join('Author')-> 163 where('Author_Name', 'John')-> 156 164 findOne(); 165 // You can also use the magic joinXXX() method 166 }}} 167 168 === Counting objects === 169 170 {{{ 171 #!php 172 <?php 173 // Counting all Articles 174 $nbArticles = sfPropelFinder::from('Article')->count(); 157 175 }}} 158 176 … … 165 183 $nbArticles = sfPropelFinder::from('Article')->delete(); 166 184 // Deleting a selection of Articles 167 $nbArticles = sfPropelFinder::from('Article')->whereTitle('like', 'foo%')->delete(); 185 $nbArticles = sfPropelFinder::from('Article')-> 186 where('Title', 'like', 'foo%')-> 187 delete(); 168 188 }}} 169 189 … … 201 221 <?php 202 222 $articles = sfPropelFinder::from('Article')-> 203 where Title('like', 'foo%')->223 where('Title', 'like', 'foo%')-> 204 224 addOr(ArticlePeer::TITLE, 'bar%', Criteria::LIKE)-> // that's a Criteria method 205 225 findOne(); … … 211 231 #!php 212 232 <?php 213 $finder = sfPropelFinder::from('Article')->where Title('foo');233 $finder = sfPropelFinder::from('Article')->where('Title', 'foo'); 214 234 $finder->findOne(); 215 235 $query = $finder->getLatestQuery(); … … 220 240 221 241 * Allow hydrating of related objects (doSelectJoinXXX equivalent) 222 * Add And223 * Add Or224 242 * Put as a parent class in the PeerBuilder so that every Peer class can be a finder 225 243 * Merge with sfPropelImpersonatorPlugin! … … 227 245 == Changelog == 228 246 229 === 2008-03-28 | 0.2.0 Beta === 230 247 === 2008-03-31 | 0.2.0 Beta === 248 249 * francois: De-emphasized the use of magic methods in the unit tests and README 250 * francois: Added `sfPropelFinder::_and()` and `sfPropelFinder::_or()` methods 231 251 * francois: Added support for Criteria methods (no more limit to what you can do with a finder!) 232 252 * francois: Added `sfPropelFinder::getLatestQuery()` method plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php
r8160 r8168 196 196 { 197 197 $column = $this->getColName($columnName); 198 if(!is_array($arguments)) 199 { 200 $arguments = func_get_args(); 201 array_shift($arguments); 202 } 203 list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments); 204 $this->criteria->add($column, $value, $comparison); 205 206 return $this; 207 } 208 209 /** 210 * Finder Fluid Interface for Criteria::addAnd() 211 * Infers $column, $value, $comparison from $columnName and some optional arguments 212 * Examples: 213 * $articleFinder->_and('CommentId', 3) 214 * => $c->addAnd(ArticlePeer::COMMENT_ID, 3) 215 * 216 * @param string $columnName PHPName of the column bearing the condition 217 * @param array $arguments Optional array of arguments 218 * 219 * @return sfPropelFinder the current finder object 220 */ 221 public function _and($columnName, $arguments = array()) 222 { 223 $column = $this->getColName($columnName); 224 if(!is_array($arguments)) 225 { 226 $arguments = func_get_args(); 227 array_shift($arguments); 228 } 229 list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments); 230 $this->criteria->addAnd($column, $value, $comparison); 231 232 return $this; 233 } 234 235 /** 236 * Finder Fluid Interface for Criteria::addOr() 237 * Infers $column, $value, $comparison from $columnName and some optional arguments 238 * Examples: 239 * $articleFinder->_or('CommentId', 3) 240 * => $c->addOr(ArticlePeer::COMMENT_ID, 3) 241 * 242 * @param string $columnName PHPName of the column bearing the condition 243 * @param array $arguments Optional array of arguments 244 * 245 * @return sfPropelFinder the current finder object 246 */ 247 public function _or($columnName, $arguments = array()) 248 { 249 $column = $this->getColName($columnName); 250 if(!is_array($arguments)) 251 { 252 $arguments = func_get_args(); 253 array_shift($arguments); 254 } 255 list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments); 256 $this->criteria->addOr($column, $value, $comparison); 257 258 return $this; 259 } 260 261 protected function getValueAndComparisonFromArguments($arguments = array()) 262 { 198 263 $comparison = Criteria::EQUAL; 199 264 switch (count($arguments)) … … 217 282 throw new Exception('{sfPropelFinder} whereXXX can only be called with one or two arguments'); 218 283 } 219 $this->criteria->add($column, $value, $comparison); 220 221 return $this; 222 284 285 return array($value, $comparison); 223 286 } 224 287 … … 236 299 { 237 300 $column = $this->getColName($columnName); 301 if(!is_array($arguments)) 302 { 303 $arguments = func_get_args(); 304 array_shift($arguments); 305 } 238 306 $order = strtoupper(array_shift($arguments)); 239 307 if(!$order) … … 271 339 list($column1, $column2) = $this->getRelation($relatedClass); 272 340 $this->relations[]= $relatedClass.'Peer'; 341 if(!is_array($arguments)) 342 { 343 $arguments = func_get_args(); 344 array_shift($arguments); 345 } 273 346 $operator = array_shift($arguments); 274 347 if(!$operator) … … 364 437 return $this->join(substr($name, 4), $arguments); 365 438 } 439 if(strpos($name, '_and') === 0) 440 { 441 return $this->_and(substr($name, 4), $arguments); 442 } 443 if(strpos($name, '_or') === 0) 444 { 445 return $this->_or(substr($name, 3), $arguments); 446 } 366 447 if(method_exists($this->criteria, $name)) 367 448 { plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php
r8160 r8168 63 63 ArticlePeer::doDeleteAll(); 64 64 65 $t = new lime_test( 61, new lime_output_color());65 $t = new lime_test(70, new lime_output_color()); 66 66 67 67 $t->diag('find()'); … … 196 196 $article3->save(); 197 197 $finder = new sfPropelFinder('Article'); 198 $nbDeleted = $finder->where Title('foo2')->delete();198 $nbDeleted = $finder->where('Title', 'foo2')->delete(); 199 199 $t->is($nbDeleted, 1, 'delete() deletes all rows found by a finder'); 200 200 $nbArticles = $finder->count(); 201 201 $t->is($nbArticles, 1, 'delete() does not delete rows not found by the finder'); 202 202 203 $t->diag('where() magic'); 204 203 $t->diag('where()'); 204 205 ArticlePeer::doDeleteAll(); 206 $article1 = new Article(); 207 $article1->setTitle('abc'); 208 $article1->save(); 209 $article2 = new Article(); 210 $article2->setTitle('def'); 211 $article2->save(); 212 $article3 = new Article(); 213 $article3->setTitle('bbc'); 214 $article3->save(); 215 $article = sfPropelFinder::from('Article')->where('Title', 'abc')->findOne(); 216 $t->is($article->getId(), $article1->getId(), 'where() accepts a simple CamelCase column name like ClassName'); 217 try 218 { 219 $article = sfPropelFinder::from('Article')->where('Foo', 'abc'); 220 $t->fail('where() throws an exception when the column is not found'); 221 } 222 catch (Exception $e) 223 { 224 $t->pass('where() throws an exception when the column is not found'); 225 } 226 $article = sfPropelFinder::from('Article')->where('Article_Title', 'abc')->findOne(); 227 $t->is($article->getId(), $article1->getId(), 'where() accepts a complete column name ClassName_ColumnName'); 228 $article = sfPropelFinder::from('Article')->where('Title', 'def')->findOne(); 229 $t->is($article->getId(), $article2->getId(), 'where() adds a WHERE condition on the column given as first argument'); 230 $articles = sfPropelFinder::from('Article')->where('Title', Criteria::LIKE, '%bc')->find(); 231 $t->is(count($articles), 2, 'where() accepts a comparator as second argument when three arguments are given'); 232 $articles = sfPropelFinder::from('Article')->where('Title', 'like', '%bc')->find(); 233 $t->is(count($articles), 2, 'where() accepts a text comparator and is permissive on syntax'); 234 $articles = sfPropelFinder::from('Article')->where('Title', 'is not null', null)->find(); 235 $t->is(count($articles), 3, 'where() accepts a text comparator and is permissive on syntax'); 205 236 try 206 237 { … … 243 274 } 244 275 245 ArticlePeer::doDeleteAll();246 $article1 = new Article();247 $article1->setTitle('abc');248 $article1->save();249 $article2 = new Article();250 $article2->setTitle('def');251 $article2->save();252 $article3 = new Article();253 $article3->setTitle('bbc');254 $article3->save();255 256 276 $article = sfPropelFinder::from('Article')->whereTitle('abc')->findOne(); 257 $t->is($article->getId(), $article1->getId(), 'whereXXX() a ccepts a simple column name like whereColumnName()');277 $t->is($article->getId(), $article1->getId(), 'whereXXX() adds a where condition according to the XXX column name'); 258 278 $article = sfPropelFinder::from('Article')->whereArticle_Title('abc')->findOne(); 259 279 $t->is($article->getId(), $article1->getId(), 'whereXXX() accepts a complete column name like whereClassName_ColumnName()'); … … 262 282 $articles = sfPropelFinder::from('Article')->whereTitle(Criteria::LIKE, '%bc')->find(); 263 283 $t->is(count($articles), 2, 'whereXXX() accepts a comparator as first argument when two arguments are given'); 264 $articles = sfPropelFinder::from('Article')->whereTitle('like', '%bc')->find(); 265 $t->is(count($articles), 2, 'whereXXX() accepts a text comparator and is permissive on syntax'); 266 $articles = sfPropelFinder::from('Article')->whereTitle('is not null', null)->find(); 267 $t->is(count($articles), 3, 'whereXXX() accepts a text comparator and is permissive on syntax'); 268 269 270 $t->diag('orderBy() magic'); 284 285 286 $t->diag('orderBy()'); 287 288 ArticlePeer::doDeleteAll(); 289 $article1 = new Article(); 290 $article1->setTitle('bbbbb'); 291 $article1->save(); 292 $article2 = new Article(); 293 $article2->setTitle('aaaaa'); 294 $article2->save(); 295 $article3 = new Article(); 296 $article3->setTitle('ccccc'); 297 $article3->save(); 298 299 $article = sfPropelFinder::from('Article')->orderBy('Title')->findOne(); 300 $t->is($article->getTitle(), 'aaaaa', 'orderBy() orders by column asc by default'); 301 302 $article = sfPropelFinder::from('Article')->orderBy('Title', 'asc')->findOne(); 303 $t->is($article->getTitle(), 'aaaaa', 'orderBy() orders by column and takes an order as second argument'); 304 305 $article = sfPropelFinder::from('Article')->orderBy('Title', 'desc')->findOne(); 306 $t->is($article->getTitle(), 'ccccc', 'orderBy() orders by column and takes an order as second argument'); 307 308 try 309 { 310 $article = sfPropelFinder::from('Article')->orderBy('Title', 'abc'); 311 $t->fail('orderBy() throws an exception when called with a second argument different from "asc" or "desc"'); 312 } 313 catch (Exception $e) 314 { 315 $t->pass('orderBy() throws an exception when called with a second argument different from "asc" or "desc"'); 316 } 317 271 318 try 272 319 { … … 299 346 } 300 347 301 try302 {303 $article = sfPropelFinder::from('Article')->orderByTitle('abc');304 $t->fail('orderByXXX() throws an exception when called with an argument different from "asc" or "desc"');305 }306 catch (Exception $e)307 {308 $t->pass('orderByXXX() throws an exception when called with an argument different from "asc" or "desc"');309 }310 311 ArticlePeer::doDeleteAll();312 $article1 = new Article();313 $article1->setTitle('bbbbb');314 $article1->save();315 $article2 = new Article();316 $article2->setTitle('aaaaa');317 $article2->save();318 $article3 = new Article();319 $article3->setTitle('ccccc');320 $article3->save();321 322 348 $article = sfPropelFinder::from('Article')->orderByTitle()->findOne(); 323 $t->is($article->getTitle(), 'aaaaa', 'orderByXXX() orders by column a sc by default');349 $t->is($article->getTitle(), 'aaaaa', 'orderByXXX() orders by column according to the XXX column name'); 324 350 325 351 $article = sfPropelFinder::from('Article')->orderByTitle('asc')->findOne(); 326 $t->is($article->getTitle(), 'aaaaa', 'orderByXXX() orders by column and takes an order as argment');352 $t->is($article->getTitle(), 'aaaaa', 'orderByXXX() takes an order as argument'); 327 353 328 354 $article = sfPropelFinder::from('Article')->orderByTitle('desc')->findOne(); 329 $t->is($article->getTitle(), 'ccccc', 'orderByXXX() orders by column and takes an order as argment');330 331 $t->diag('join() magic');355 $t->is($article->getTitle(), 'ccccc', 'orderByXXX() takes an order as argument'); 356 357 $t->diag('join()'); 332 358 333 359 list($column1, $column2) = sfPropelFinder::from('Article')->getRelation('Category'); … … 357 383 $article3->setCategory($category2); 358 384 $article3->save(); 359 $nbArticles = sfPropelFinder::from('Article')->join Category()->whereCategory_Name('cat1')->count();360 $t->is($nbArticles, 2, 'join XXX() allows to join to another table (many-to-one)');361 $nbArticles = sfPropelFinder::from('Article')->join Category()->whereCategory_Name('cat2')->count();362 $t->is($nbArticles, 1, 'join XXX() allows to join to another table (many-to-one)');363 $article = sfPropelFinder::from('Article')->join Category()->whereCategory_Name('cat2')->findOne();364 $t->is($article->getTitle(), 'ccccc', 'join XXX() allows to join to another table (many-to-one)');385 $nbArticles = sfPropelFinder::from('Article')->join('Category')->where('Category_Name', 'cat1')->count(); 386 $t->is($nbArticles, 2, 'join() allows to join to another table (many-to-one)'); 387 $nbArticles = sfPropelFinder::from('Article')->join('Category')->where('Category_Name', 'cat2')->count(); 388 $t->is($nbArticles, 1, 'join() allows to join to another table (many-to-one)'); 389 $article = sfPropelFinder::from('Article')->join('Category')->where('Category_Name', 'cat2')->findOne(); 390 $t->is($article->getTitle(), 'ccccc', 'join() allows to join to another table (many-to-one)'); 365 391 ArticlePeer::doDeleteAll(); 366 392 CommentPeer::doDeleteAll(); … … 377 403 $comment->setArticle($article2); 378 404 $comment->save(); 379 $nbArticles = sfPropelFinder::from('Article')->join Comment()->whereComment_Content('foo')->count();380 $t->is($nbArticles, 1, 'join XXX() allows to join to another table (one-to-many)');381 $article = sfPropelFinder::from('Article')->join Comment()->whereComment_Content('foo')->findOne();382 $t->is($article->getTitle(), 'bbbbb', 'join XXX() allows to join to another table (one-to-many)');405 $nbArticles = sfPropelFinder::from('Article')->join('Comment')->where('Comment_Content', 'foo')->count(); 406 $t->is($nbArticles, 1, 'join() allows to join to another table (one-to-many)'); 407 $article = sfPropelFinder::from('Article')->join('Comment')->where('Comment_Content', 'foo')->findOne(); 408 $t->is($article->getTitle(), 'bbbbb', 'join() allows to join to another table (one-to-many)'); 383 409 384 410 ArticlePeer::doDeleteAll(); … … 397 423 $comment->setAuthor($author1); 398 424 $comment->save(); 399 $article = sfPropelFinder::from('Article')->joinComment()->joinAuthor()->whereAuthor_Name('John')->findOne(); 400 $t->is($article->getTitle(), 'aaaaa', 'you can chain several joinXXX() statements'); 425 $article = sfPropelFinder::from('Article')->join('Comment')->join('Author')->where('Author_Name', 'John')->findOne(); 426 $t->is($article->getTitle(), 'aaaaa', 'you can chain several join() statements'); 427 $article = sfPropelFinder::from('Article')->joinComment()->joinAuthor()->where('Author_Name', 'John')->findOne(); 428 $t->is($article->getTitle(), 'aaaaa', 'joinXXX() does a join according to the XXX column name'); 401 429 402 430 $t->diag('Debugging functions'); 403 431 404 $finder = sfPropelFinder::from('Article')->where Title('foo');432 $finder = sfPropelFinder::from('Article')->where('Title', 'foo'); 405 433 $t->isa_ok($finder->getCriteria(), 'Criteria', 'getCriteria() returns the criteria as composed by the finder'); 406 434 $finder->findOne();