Development

Changeset 10805

You must first sign up to be able to contribute.

Changeset 10805

Show
Ignore:
Timestamp:
08/12/08 14:21:31 (3 months ago)
Author:
francois
Message:

sfPropelFinderPlugin Minor fixes:

  • Implemented limit() and offset() in both sfPropelFinder and sfDoctrineFinder
  • Implemented not in comparison in sfDoctrineFinder::where()
  • Added finder_methods parameter in generator.yml
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfPropelFinderPlugin/README

    r10785 r10805  
    533533--------- 
    534534 
    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` 
    537540* francois: `sfPropelFinder::join()` now defaults to an `INNER JOIN` instead of a `WHERE` statement (will facilitate compatibility with Doctrine) 
    538541* francois: Implemented `sfDoctrine::combine()` 
  • plugins/sfPropelFinderPlugin/README_generator

    r10786 r10805  
    1515        theme:            default 
    1616 
    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--------------------------- 
    3519 
    3620Being 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: 
    3721 
    3822    list: 
    39       display: [=Title, Category] 
     23      display: [=title, category] 
    4024      with:    [Category] 
    4125 
     
    4327 
    4428    list: 
    45       display: [=Title, Category, Author] 
     29      display: [=title, category, author] 
    4630      with:    [i18n, Category, Author] 
    4731 
    4832*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 
     34The 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] 
    4939 
    5040TODO 
     
    5343* Pake task to generate a DbFinder Admin Module 
    5444* 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  
    3939<?php endforeach ?> 
    4040<?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 ?> 
    4345<?php endif ?> 
    4446    $this->pager = $this->finder->paginate($this->getRequestParameter('page', 1), <?php echo $this->getParameterValue('list.max_per_page', 20) ?>); 
     
    378380        $finder->where('<?php echo $name ?>', '<=', $this->filters['<?php echo $column->getName() ?>']['to']); 
    379381<?php endif; ?> 
    380         } 
    381382      } 
    382383 
     
    400401    if ($sort_column = $this->getUser()->getAttribute('sort', null, 'sf_admin/<?php echo $this->getSingularName() ?>/sort')) 
    401402    { 
     403      $sort_column = sfInflector::camelize($sort_column); 
    402404      $finder->orderBy($sort_column, $this->getUser()->getAttribute('type', null, 'sf_admin/<?php echo $this->getSingularName() ?>/sort')); 
    403405    } 
  • plugins/sfPropelFinderPlugin/lib/DbFinder.php

    r10786 r10805  
    479479  { 
    480480    $this->adapter->withColumn($column, $alias, $type); 
     481     
     482    return $this; 
    481483  } 
    482484   
     
    491493  { 
    492494    $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); 
    493519     
    494520    return $this; 
  • plugins/sfPropelFinderPlugin/lib/sfDoctrineFinder.php

    r10780 r10805  
    648648  public function distinct() 
    649649  { 
    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); 
    651675     
    652676    return $this; 
     
    715739    if($comparison == ' NOT IN ') 
    716740    { 
    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 ') 
    720748    { 
    721749      if($cond == 'or') 
     
    803831    else 
    804832    { 
    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); 
    806836    } 
    807837     
     
    907937     
    908938    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     else 
    923     { 
    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     else 
    940     { 
    941       return sfInflector::underscore($arg); 
    942     } 
    943939  } 
    944940   
  • plugins/sfPropelFinderPlugin/lib/sfModelFinder.php

    r10779 r10805  
    8080   
    8181  abstract public function distinct(); 
     82  abstract public function limit($limit); 
     83  abstract public function offset($offset); 
    8284  abstract public function where(); 
    8385  abstract public function orWhere(); 
     
    237239    return array_keys($attributes); 
    238240  } 
     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  } 
    239275} 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r10785 r10805  
    925925   
    926926  /** 
     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  /** 
    927951   * Finder Fluid Interface for Criteria::add() 
    928952   * Infers $column, $value, $comparison from $columnName and some optional arguments 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinderPager.php

    r10655 r10805  
    5151    $finderForCount = clone $this->getFinder(); 
    5252    $count = $finderForCount-> 
    53       setOffset(0)-> 
    54       setLimit(0)-> 
     53      offset(0)-> 
     54      limit(0)-> 
    5555      clearGroupByColumns()-> 
    5656      count(); 
     
    5959     
    6060    $this->finder-> 
    61       setOffset(0)-> 
    62       setLimit(0); 
     61      offset(0)-> 
     62      limit(0); 
    6363       
    6464    if (($this->getPage() == 0 || $this->getMaxPerPage() == 0)) 
     
    7171 
    7272      $offset = ($this->getPage() - 1) * $this->getMaxPerPage(); 
    73       $this->finder->setOffset($offset); 
     73      $this->finder->offset($offset); 
    7474 
    7575      if ($hasMaxRecordLimit) 
     
    7878        if ($maxRecordLimit > $this->getMaxPerPage()) 
    7979        { 
    80           $this->finder->setLimit($this->getMaxPerPage()); 
     80          $this->finder->limit($this->getMaxPerPage()); 
    8181        } 
    8282        else 
    8383        { 
    84           $this->finder->setLimit($maxRecordLimit); 
     84          $this->finder->limit($maxRecordLimit); 
    8585        } 
    8686      } 
    8787      else 
    8888      { 
    89         $this->finder->setLimit($this->getMaxPerPage()); 
     89        $this->finder->limit($this->getMaxPerPage()); 
    9090      } 
    9191    } 
     
    104104 
    105105    return $finderForRetrieve-> 
    106       setOffset($offset - 1)-> 
     106      offset($offset - 1)-> 
    107107      findOne(); 
    108108  } 
  • plugins/sfPropelFinderPlugin/test/unit/sfDoctrineFinderTest.php

    r10780 r10805  
    7373Doctrine_Query::create()->delete()->from('DArticle')->execute(); 
    7474 
    75 $t = new lime_test(92, new lime_output_color()); 
     75$t = new lime_test(103, new lime_output_color()); 
    7676 
    7777$t->diag('find()'); 
     
    425425$articles = sfDoctrineFinder::from('DArticle')->where('Title', 'is not null', null)->find(); 
    426426$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(); 
    428428$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'); 
    430431try 
    431432{ 
     
    606607$t->is( 
    607608  $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'))", 
    609610  'combine() clauses live well with the usual conditions' 
    610611); 
     
    618619$t->is( 
    619620  $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')", 
    621622  'combine() clauses live well with the usual conditions and appear ordered as they were called' 
    622623); 
     
    661662  'combine() can combine more than two conditions' 
    662663); 
     664 
     665$t->diag('limit() and offset()'); 
     666 
     667Doctrine_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  
    6464ArticlePeer::doDeleteAll(); 
    6565 
    66 $t = new lime_test(122, new lime_output_color()); 
     66$t = new lime_test(133, new lime_output_color()); 
    6767 
    6868$t->diag('find()'); 
     
    409409$articles = sfPropelFinder::from('Article')->where('Title', 'is not null', null)->find(); 
    410410$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(); 
    412412$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'); 
    413415try 
    414416{ 
     
    640642); 
    641643 
     644$t->diag('limit() and offset()'); 
     645 
     646ArticlePeer::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'); 
    642679 
    643680$t->diag('relatedTo()');