Changeset 10805
- Timestamp:
- 08/12/08 14:21:31 (3 months ago)
- Files:
-
- plugins/sfPropelFinderPlugin/README (modified) (1 diff)
- plugins/sfPropelFinderPlugin/README_generator (modified) (3 diffs)
- plugins/sfPropelFinderPlugin/data/generator/DbFinderAdmin/default/template/actions/actions.class.php (modified) (3 diffs)
- plugins/sfPropelFinderPlugin/lib/DbFinder.php (modified) (2 diffs)
- plugins/sfPropelFinderPlugin/lib/sfDoctrineFinder.php (modified) (4 diffs)
- plugins/sfPropelFinderPlugin/lib/sfModelFinder.php (modified) (2 diffs)
- plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php (modified) (1 diff)
- plugins/sfPropelFinderPlugin/lib/sfPropelFinderPager.php (modified) (5 diffs)
- plugins/sfPropelFinderPlugin/test/unit/sfDoctrineFinderTest.php (modified) (5 diffs)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelFinderPlugin/README
r10785 r10805 533 533 --------- 534 534 535 ### 2008-08-11 | Trunk 536 535 ### 2008-08-12 | Trunk 536 537 * francois: Added `finder_methods` parameter in `generator.yml` 538 * francois: Implemented `not in` comparison in `sfDoctrineFinder::where()` 539 * francois: Implemented `limit()` and `offset()` in both `sfPropelFinder` and `sfDoctrineFinder` 537 540 * francois: `sfPropelFinder::join()` now defaults to an `INNER JOIN` instead of a `WHERE` statement (will facilitate compatibility with Doctrine) 538 541 * francois: Implemented `sfDoctrine::combine()` plugins/sfPropelFinderPlugin/README_generator
r10786 r10805 15 15 theme: default 16 16 17 There is another tweak in the `generator.yml` syntax with this generator: column names must be in CamelCase, since that's what the `DbFinder` expects. 18 19 So instead of 20 21 list: 22 display: [=title, Category] 23 sort: [title, desc] 24 filters: [title, id, category_id] 25 26 You must write 27 28 list: 29 display: [=Title, Category] 30 sort: [Title, desc] 31 filters: [Title, Id, CategoryId] 32 33 `with` and `peer_method` 34 ----------------------- 17 `with` and `finder_methods` 18 --------------------------- 35 19 36 20 Being ORM agnostic, the `DbFinderAdminGenerator` does not accept the `peer_method` parameter. Instead, it supports a `with` parameter, where you can stipulate the classes of all the related objects you want to hydrate to save queries: 37 21 38 22 list: 39 display: [= Title, Category]23 display: [=title, category] 40 24 with: [Category] 41 25 … … 43 27 44 28 list: 45 display: [= Title, Category, Author]29 display: [=title, category, author] 46 30 with: [i18n, Category, Author] 47 31 48 32 *Note*: The resulting query contains a left join, while `sfPropelAdminGenerator` issues queries with a simple `where` condition. That means that even if you choose to hydrate related `Category` objects, the list view will still display `Article` objects with no `Category`. That's a huge difference with the usual generator based on Propel 1.2, which hides records as soon as you declare a `peer_method`. 33 34 The list view uses a finder to retrieve the list of records. By adding method names to the `finder_methods` parameter, you 'filter' the view by these filters. This allows you to changes the display order, restrict results or hydrate supplementary columns to save on queries. 35 36 list: 37 display: [=title, category, author] 38 finder_methods: [withNbComments] 49 39 50 40 TODO … … 53 43 * Pake task to generate a DbFinder Admin Module 54 44 * Migrate many-to-many code to DbFinder 55 * Implement enough DoctrineFinder to make it work with Doctrine! (mostly `_or()`)plugins/sfPropelFinderPlugin/data/generator/DbFinderAdmin/default/template/actions/actions.class.php
r10592 r10805 39 39 <?php endforeach ?> 40 40 <?php endif ?> 41 <?php if ($this->getParameterValue('list.peer_count_method')): ?> 42 //$this->pager->setPeerCountMethod('<?php echo $this->getParameterValue('list.peer_count_method') ?>'); 41 <?php if ($this->getParameterValue('list.finder_methods')): ?> 42 <?php foreach ($this->getParameterValue('list.finder_methods') as $method): ?> 43 $this->finder-><?php echo $method ?>(); 44 <?php endforeach ?> 43 45 <?php endif ?> 44 46 $this->pager = $this->finder->paginate($this->getRequestParameter('page', 1), <?php echo $this->getParameterValue('list.max_per_page', 20) ?>); … … 378 380 $finder->where('<?php echo $name ?>', '<=', $this->filters['<?php echo $column->getName() ?>']['to']); 379 381 <?php endif; ?> 380 }381 382 } 382 383 … … 400 401 if ($sort_column = $this->getUser()->getAttribute('sort', null, 'sf_admin/<?php echo $this->getSingularName() ?>/sort')) 401 402 { 403 $sort_column = sfInflector::camelize($sort_column); 402 404 $finder->orderBy($sort_column, $this->getUser()->getAttribute('type', null, 'sf_admin/<?php echo $this->getSingularName() ?>/sort')); 403 405 } plugins/sfPropelFinderPlugin/lib/DbFinder.php
r10786 r10805 479 479 { 480 480 $this->adapter->withColumn($column, $alias, $type); 481 482 return $this; 481 483 } 482 484 … … 491 493 { 492 494 $this->adapter->distinct(); 495 496 return $this; 497 } 498 499 /** 500 * Finder Fluid Interface for SQL LIMIT 501 * 502 * @return DbFinder the current finder object 503 */ 504 public function limit($limit) 505 { 506 $this->adapter->limit($limit); 507 508 return $this; 509 } 510 511 /** 512 * Finder Fluid Interface for SQL OFFSET 513 * 514 * @return DbFinder the current finder object 515 */ 516 public function offset($offset) 517 { 518 $this->adapter->offset($offset); 493 519 494 520 return $this; plugins/sfPropelFinderPlugin/lib/sfDoctrineFinder.php
r10780 r10805 648 648 public function distinct() 649 649 { 650 throw new Exception('This method is not yet implemented'); 650 $this->query->distinct(); 651 652 return $this; 653 } 654 655 /** 656 * Finder Fluid Interface for Doctrine_Query::limit() 657 * 658 * @return sfDoctrineFinder the current finder object 659 */ 660 public function limit($limit) 661 { 662 $this->query->limit($limit); 663 664 return $this; 665 } 666 667 /** 668 * Finder Fluid Interface for Doctrine_Query::offset() 669 * 670 * @return sfDoctrineFinder the current finder object 671 */ 672 public function offset($offset) 673 { 674 $this->query->offset($offset); 651 675 652 676 return $this; … … 715 739 if($comparison == ' NOT IN ') 716 740 { 717 throw new Exception('sfDoctrineFinder cannot add a condition with a NOT IN'); 718 } 719 if($comparison == ' IN ') 741 if($cond == 'or') 742 { 743 throw new Exception('sfDoctrineFinder cannot OR a condition with a NOT IN'); 744 } 745 $this->query->whereIn($column, $value, true); 746 } 747 else if($comparison == ' IN ') 720 748 { 721 749 if($cond == 'or') … … 803 831 else 804 832 { 805 $this->query->addWhere($pattern, $args); 833 $cond = $this->queryPattern ? 'and' : ''; 834 $this->queryPattern .= sprintf(' %s %s', $cond, $pattern); 835 $this->queryArgs = array_merge($this->queryArgs, $args); 806 836 } 807 837 … … 907 937 908 938 throw new Exception('Unable to figure out the column to use to order rows in sfDoctrineFinder::guessOrder()'); 909 }910 911 protected static function camelize($arg)912 {913 if(is_array($arg))914 {915 $ret = array();916 foreach ($arg as $arg1)917 {918 $ret []= self::camelize($arg1);919 }920 return $ret;921 }922 else923 {924 return sfInflector::camelize($arg);925 }926 }927 928 protected static function underscore($arg)929 {930 if(is_array($arg))931 {932 $ret = array();933 foreach ($arg as $arg1)934 {935 $ret []= self::underscore($arg1);936 }937 return $ret;938 }939 else940 {941 return sfInflector::underscore($arg);942 }943 939 } 944 940 plugins/sfPropelFinderPlugin/lib/sfModelFinder.php
r10779 r10805 80 80 81 81 abstract public function distinct(); 82 abstract public function limit($limit); 83 abstract public function offset($offset); 82 84 abstract public function where(); 83 85 abstract public function orWhere(); … … 237 239 return array_keys($attributes); 238 240 } 241 242 public static function camelize($arg) 243 { 244 if(is_array($arg)) 245 { 246 $ret = array(); 247 foreach ($arg as $arg1) 248 { 249 $ret []= self::camelize($arg1); 250 } 251 return $ret; 252 } 253 else 254 { 255 return sfInflector::camelize($arg); 256 } 257 } 258 259 public static function underscore($arg) 260 { 261 if(is_array($arg)) 262 { 263 $ret = array(); 264 foreach ($arg as $arg1) 265 { 266 $ret []= self::underscore($arg1); 267 } 268 return $ret; 269 } 270 else 271 { 272 return sfInflector::underscore($arg); 273 } 274 } 239 275 } plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php
r10785 r10805 925 925 926 926 /** 927 * Finder Fluid Interface for Criteria::setLimit() 928 * 929 * @return sfPropelFinder the current finder object 930 */ 931 public function limit($limit) 932 { 933 $this->criteria->setLimit($limit); 934 935 return $this; 936 } 937 938 /** 939 * Finder Fluid Interface for Criteria::setOffset() 940 * 941 * @return sfPropelFinder the current finder object 942 */ 943 public function offset($offset) 944 { 945 $this->criteria->setOffset($offset); 946 947 return $this; 948 } 949 950 /** 927 951 * Finder Fluid Interface for Criteria::add() 928 952 * Infers $column, $value, $comparison from $columnName and some optional arguments plugins/sfPropelFinderPlugin/lib/sfPropelFinderPager.php
r10655 r10805 51 51 $finderForCount = clone $this->getFinder(); 52 52 $count = $finderForCount-> 53 setOffset(0)->54 setLimit(0)->53 offset(0)-> 54 limit(0)-> 55 55 clearGroupByColumns()-> 56 56 count(); … … 59 59 60 60 $this->finder-> 61 setOffset(0)->62 setLimit(0);61 offset(0)-> 62 limit(0); 63 63 64 64 if (($this->getPage() == 0 || $this->getMaxPerPage() == 0)) … … 71 71 72 72 $offset = ($this->getPage() - 1) * $this->getMaxPerPage(); 73 $this->finder-> setOffset($offset);73 $this->finder->offset($offset); 74 74 75 75 if ($hasMaxRecordLimit) … … 78 78 if ($maxRecordLimit > $this->getMaxPerPage()) 79 79 { 80 $this->finder-> setLimit($this->getMaxPerPage());80 $this->finder->limit($this->getMaxPerPage()); 81 81 } 82 82 else 83 83 { 84 $this->finder-> setLimit($maxRecordLimit);84 $this->finder->limit($maxRecordLimit); 85 85 } 86 86 } 87 87 else 88 88 { 89 $this->finder-> setLimit($this->getMaxPerPage());89 $this->finder->limit($this->getMaxPerPage()); 90 90 } 91 91 } … … 104 104 105 105 return $finderForRetrieve-> 106 setOffset($offset - 1)->106 offset($offset - 1)-> 107 107 findOne(); 108 108 } plugins/sfPropelFinderPlugin/test/unit/sfDoctrineFinderTest.php
r10780 r10805 73 73 Doctrine_Query::create()->delete()->from('DArticle')->execute(); 74 74 75 $t = new lime_test( 92, new lime_output_color());75 $t = new lime_test(103, new lime_output_color()); 76 76 77 77 $t->diag('find()'); … … 425 425 $articles = sfDoctrineFinder::from('DArticle')->where('Title', 'is not null', null)->find(); 426 426 $t->is(count($articles), 3, 'where() accepts a text comparator and is permissive on syntax'); 427 $articles = sfDoctrineFinder::from('DArticle')->where('Title', ' in', array('abc', 'def'))->find();427 $articles = sfDoctrineFinder::from('DArticle')->where('Title', 'in', array('abc', 'def'))->find(); 428 428 $t->is(count($articles), 2, 'where() accepts a "in" comparator'); 429 429 $articles = sfDoctrineFinder::from('DArticle')->where('Title', 'not in', array('abc', 'def'))->find(); 430 $t->is(count($articles), 1, 'where() accepts a "not in" comparator'); 430 431 try 431 432 { … … 606 607 $t->is( 607 608 $finder->getLatestQuery(), 608 $baseSelect . "(d.title = 'foo ' OR d.title = 'bar') AND d.title = 'foobar'",609 $baseSelect . "(d.title = 'foobar' AND (d.title = 'foo' OR d.title = 'bar'))", 609 610 'combine() clauses live well with the usual conditions' 610 611 ); … … 618 619 $t->is( 619 620 $finder->getLatestQuery(), 620 $baseSelect . "( d.title = 'foo' OR d.title = 'bar') AND d.title = 'foobar'",621 $baseSelect . "((d.title = 'foo' OR d.title = 'bar') AND d.title = 'foobar')", 621 622 'combine() clauses live well with the usual conditions and appear ordered as they were called' 622 623 ); … … 661 662 'combine() can combine more than two conditions' 662 663 ); 664 665 $t->diag('limit() and offset()'); 666 667 Doctrine_Query::create()->delete()->from('DArticle')->execute(); 668 $article1 = new DArticle(); 669 $article1->setTitle('abc'); 670 $article1->save(); 671 $article2 = new DArticle(); 672 $article2->setTitle('def'); 673 $article2->save(); 674 $article3 = new DArticle(); 675 $article3->setTitle('bbc'); 676 $article3->save(); 677 678 $articles = sfDoctrineFinder::from('DArticle')->limit(1)->find(); 679 $t->is(count($articles), 1, 'limit() adds a limit to the SQL clause'); 680 $article = $articles[0]; 681 $t->is($article->getTitle(), 'abc', 'limit() adds a limit to the SQL clause'); 682 $articles = sfDoctrineFinder::from('DArticle')->limit(2)->find(); 683 $t->is(count($articles), 2, 'limit() adds a limit to the SQL clause'); 684 $article = $articles[0]; 685 $t->is($article->getTitle(), 'abc', 'limit() adds a limit to the SQL clause'); 686 $article = $articles[1]; 687 $t->is($article->getTitle(), 'def', 'limit() adds a limit to the SQL clause'); 688 689 $articles = sfDoctrineFinder::from('DArticle')->offset(1)->find(); 690 $t->is(count($articles), 2, 'offset() adds an offset to the SQL clause'); 691 $article = $articles[0]; 692 $t->is($article->getTitle(), 'def', 'offset() adds an offset to the SQL clause'); 693 $article = $articles[1]; 694 $t->is($article->getTitle(), 'bbc', 'offset() adds an offset to the SQL clause'); 695 696 $articles = sfDoctrineFinder::from('DArticle')->offset(1)->limit(1)->find(); 697 $t->is(count($articles), 1, 'limit() and offset() can be combined'); 698 $article = $articles[0]; 699 $t->is($article->getTitle(), 'def', 'limit() and offset() can be combined'); plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php
r10785 r10805 64 64 ArticlePeer::doDeleteAll(); 65 65 66 $t = new lime_test(1 22, new lime_output_color());66 $t = new lime_test(133, new lime_output_color()); 67 67 68 68 $t->diag('find()'); … … 409 409 $articles = sfPropelFinder::from('Article')->where('Title', 'is not null', null)->find(); 410 410 $t->is(count($articles), 3, 'where() accepts a text comparator and is permissive on syntax'); 411 $articles = sfPropelFinder::from('Article')->where('Title', ' in', array('abc', 'def'))->find();411 $articles = sfPropelFinder::from('Article')->where('Title', 'in', array('abc', 'def'))->find(); 412 412 $t->is(count($articles), 2, 'where() accepts a "in" comparator'); 413 $articles = sfPropelFinder::from('Article')->where('Title', 'not in', array('abc', 'def'))->find(); 414 $t->is(count($articles), 1, 'where() accepts a "not in" comparator'); 413 415 try 414 416 { … … 640 642 ); 641 643 644 $t->diag('limit() and offset()'); 645 646 ArticlePeer::doDeleteAll(); 647 $article1 = new Article(); 648 $article1->setTitle('abc'); 649 $article1->save(); 650 $article2 = new Article(); 651 $article2->setTitle('def'); 652 $article2->save(); 653 $article3 = new Article(); 654 $article3->setTitle('bbc'); 655 $article3->save(); 656 657 $articles = sfPropelFinder::from('Article')->limit(1)->find(); 658 $t->is(count($articles), 1, 'limit() adds a limit to the SQL clause'); 659 $article = $articles[0]; 660 $t->is($article->getTitle(), 'abc', 'limit() adds a limit to the SQL clause'); 661 $articles = sfPropelFinder::from('Article')->limit(2)->find(); 662 $t->is(count($articles), 2, 'limit() adds a limit to the SQL clause'); 663 $article = $articles[0]; 664 $t->is($article->getTitle(), 'abc', 'limit() adds a limit to the SQL clause'); 665 $article = $articles[1]; 666 $t->is($article->getTitle(), 'def', 'limit() adds a limit to the SQL clause'); 667 668 $articles = sfPropelFinder::from('Article')->offset(1)->find(); 669 $t->is(count($articles), 2, 'offset() adds an offset to the SQL clause'); 670 $article = $articles[0]; 671 $t->is($article->getTitle(), 'def', 'offset() adds an offset to the SQL clause'); 672 $article = $articles[1]; 673 $t->is($article->getTitle(), 'bbc', 'offset() adds an offset to the SQL clause'); 674 675 $articles = sfPropelFinder::from('Article')->offset(1)->limit(1)->find(); 676 $t->is(count($articles), 1, 'limit() and offset() can be combined'); 677 $article = $articles[0]; 678 $t->is($article->getTitle(), 'def', 'limit() and offset() can be combined'); 642 679 643 680 $t->diag('relatedTo()');