Development

Changeset 10068

You must first sign up to be able to contribute.

Changeset 10068

Show
Ignore:
Timestamp:
07/02/08 16:40:21 (2 months ago)
Author:
francois
Message:

sfPropelFinderPlugin Added sfPropelFinder::set() method (based on a patch by jug) (closes #3306)

Files:

Legend:

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

    r10065 r10068  
    387387}}} 
    388388 
     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 
     402sfPropelFinder::from('Article')-> 
     403  where('Title', 'foo')-> 
     404  set(array('Title' => 'updated title')); // 1 
     405 
     406// set() returns the number of modified columns 
     407sfPropelFinder::from('Article')-> 
     408  where('Title', 'updated title')-> 
     409  count(); // 1 
     410}}} 
     411 
    389412=== Writing your own business logic into a finder === 
    390413 
     
    448471=== 2008-07-02 | Trunk === 
    449472 
     473 * francois: Added `sfPropelFinder::set()` method (based on a patch by jug) 
    450474 * francois: Added `sfPropelFinder::withI18n()` method 
    451475 * francois: Added `sfPropelFinderPager` class and `sfPropelFinder::paginate()` method 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r10067 r10068  
    7676  } 
    7777   
    78   public function getLatestQuery(
    79   { 
    80     $con = Propel::getConnection(); 
     78  public function getLatestQuery($con = null
     79  { 
     80    if(is_null($con)) $con = Propel::getConnection(); 
    8181    if(method_exists($con, 'getLastExecutedQuery')) 
    8282    { 
     
    8585    else 
    8686    { 
    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(); 
    9494    if(method_exists($con, 'getLastExecutedQuery')) 
    9595    { 
     
    149149      return self::fromCollection($from); 
    150150    } 
    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'); 
    152152  } 
    153153 
     
    185185        if($class) 
    186186        { 
    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'); 
    188188        } 
    189189        if($object instanceof BaseObject) 
     
    193193        else 
    194194        { 
    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'); 
    196196        } 
    197197      } 
     
    200200    if(!$class) 
    201201    { 
    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'); 
    203203    } 
    204204     
     
    210210        if($pkName) 
    211211        { 
    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'); 
    213213        } 
    214214        else 
     
    243243  { 
    244244    $ret = call_user_func(array($this->getPeerClass(), 'doCount'), $this->getCriteria(), $con); 
    245     $this->updateLatestQuery(); 
     245    $this->updateLatestQuery($con); 
    246246    if($reinitCriteria) 
    247247    { 
     
    259259    } 
    260260    $ret = $this->doFind($this->getCriteria(), $con); 
    261     $this->updateLatestQuery(); 
     261    $this->updateLatestQuery($con); 
    262262    if($reinitCriteria) 
    263263    { 
     
    272272    $this->criteria->setLimit(1); 
    273273    $ret = $this->doFind($this->getCriteria(), $con); 
    274     $this->updateLatestQuery(); 
     274    $this->updateLatestQuery($con); 
    275275    if($reinitCriteria) 
    276276    { 
     
    338338      $ret = call_user_func(array($this->getPeerClass(), 'retrieveByPk'), $pk, $con); 
    339339    } 
    340     $this->updateLatestQuery(); 
     340    $this->updateLatestQuery($con); 
    341341     
    342342    return $ret; 
     
    355355    } 
    356356    $ret = call_user_func(array($this->getPeerClass(), 'doDelete'), $deleteCriteria, $con); 
    357     $this->updateLatestQuery(); 
     357    $this->updateLatestQuery($con); 
    358358    if($reinitCriteria) 
    359359    { 
     
    374374    return $pager; 
    375375  } 
     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   
    376415   
    377416  public function doFind($criteria, $con = null) 
     
    558597      if($isCalculationColumn) 
    559598      { 
    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'); 
    561600      } 
    562601      else 
     
    736775        break; 
    737776      default: 
    738         throw new Exception('{sfPropelFinder} whereXXX can only be called with one or two arguments'); 
     777        throw new Exception('sfPropelFinder::whereXXX() can only be called with one or two arguments'); 
    739778    } 
    740779 
     
    795834        break; 
    796835      default: 
    797         throw new Exception('{sfPropelFinder} orderBy only accepts "asc" or "desc" as argument'); 
     836        throw new Exception('sfPropelFinder::orderBy() only accepts "asc" or "desc" as argument'); 
    798837    } 
    799838     
     
    842881    } 
    843882     
    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()'); 
    845884  } 
    846885   
     
    889928      } 
    890929    } 
    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)); 
    892931  } 
    893932   
     
    955994    catch (PropelException $e) 
    956995    { 
    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)); 
    958997    } 
    959998  } 
     
    10041043      return $this; 
    10051044    } 
    1006     throw new Exception(sprintf('{sfPropelFinder} Undefined method %s', $name)); 
     1045    throw new Exception(sprintf('Undefined method sfPropelFinder::%s()', $name)); 
    10071046  } 
    10081047} 
  • plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php

    r10065 r10068  
    6565ArticlePeer::doDeleteAll(); 
    6666 
    67 $t = new lime_test(132, new lime_output_color()); 
     67$t = new lime_test(139, new lime_output_color()); 
    6868 
    6969$t->diag('find()'); 
     
    842842$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'); 
    843843 
     844$t->diag('set()'); 
     845 
     846ArticlePeer::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 
     860ArticlePeer::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 
     875try 
     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} 
     880catch( Exception $e ) 
     881{ 
     882  $t->pass('set() throws an exception when called on a finder with join()'); 
     883} 
     884 
    844885$t->diag('Debugging functions'); 
    845886