Development

Changeset 10074

You must first sign up to be able to contribute.

Changeset 10074

Show
Ignore:
Timestamp:
07/02/08 18:20:57 (5 months ago)
Author:
francois
Message:

sfPropelFinderPlugin Tweaked set() method

Files:

Legend:

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

    r10068 r10074  
    408408  where('Title', 'updated title')-> 
    409409  count(); // 1 
     410 
     411// Beware that set() updates all records found in a signle row 
     412// And bypasses any behavior registered on the save() hooks 
     413// You can force a one-by-one update by setting the second parameter to true 
     414sfPropelFinder::from('Article')-> 
     415  set(array('Title' => 'updated title'), true); 
     416// Beware that it may take a long time 
    410417}}} 
    411418 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r10070 r10074  
    373373  } 
    374374   
    375   public function set($values, $con = null, $reinitCriteria = true ) 
     375  public function set($values, $forceIndividualSaves = false, $con = null, $reinitCriteria = true ) 
    376376  { 
    377377    if (!is_array($values)) 
     
    379379      throw new Exception('sfPropelFinder::set() expects an array as first argument'); 
    380380    } 
    381    
    382     $find = $this->getCriteria(); 
    383     if (count($find->getJoins())) 
    384     { 
    385       throw new Exception('sfPropelFinder::set() does not support multitable updates, please do not use join'); 
    386     } 
    387     if($find->equals(new Criteria())) 
    388     { 
    389       // doUpdate will delete nothing when passed an empty criteria 
    390       // while it should, in fact, update all 
    391       $find = $this->addTrueCondition($find); 
    392     } 
    393      
    394     $set = new Criteria(); 
    395     foreach ($values as $columnName => $value) 
    396     { 
    397       $set->add($this->getColName($columnName), $value); 
    398     } 
    399      
    400     if(is_null($con)) $con = Propel::getConnection(); 
    401     $ret = BasePeer::doUpdate($find, $set, $con); 
    402     $this->updateLatestQuery($con); 
    403     if($reinitCriteria) 
    404     { 
    405       $this->reinitCriteria(); 
    406     } 
    407      
    408     return $ret; 
     381    if($forceIndividualSaves) 
     382    { 
     383      $objects = $this->find(null, $con, $reinitCriteria); 
     384      foreach ($objects as $object) 
     385      { 
     386        foreach ($values as $key => $value) 
     387        { 
     388          $object->setByName($key, $value); 
     389        } 
     390        $object->save(); 
     391      } 
     392      return count($objects); 
     393    } 
     394    else 
     395    { 
     396      $find = $this->getCriteria(); 
     397      if (count($find->getJoins())) 
     398      { 
     399        throw new Exception('sfPropelFinder::set() does not support multitable updates, please do not use join'); 
     400      } 
     401      if($find->equals(new Criteria())) 
     402      { 
     403        // doUpdate will delete nothing when passed an empty criteria 
     404        // while it should, in fact, update all 
     405        $find = $this->addTrueCondition($find); 
     406      } 
     407       
     408      $set = new Criteria(); 
     409      foreach ($values as $columnName => $value) 
     410      { 
     411        $set->add($this->getColName($columnName), $value); 
     412      } 
     413       
     414      if(is_null($con)) $con = Propel::getConnection(); 
     415      $ret = BasePeer::doUpdate($find, $set, $con); 
     416      $this->updateLatestQuery($con); 
     417      if($reinitCriteria) 
     418      { 
     419        $this->reinitCriteria(); 
     420      } 
     421       
     422      return $ret; 
     423    } 
    409424  } 
    410425   
  • plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php

    r10070 r10074  
    6565ArticlePeer::doDeleteAll(); 
    6666 
    67 $t = new lime_test(140, new lime_output_color()); 
     67$t = new lime_test(142, new lime_output_color()); 
    6868 
    6969$t->diag('find()'); 
     
    878878$t->is(sfPropelFinder::from('Article')->where('Title', 'updated title')->count(), 1, 'set() updates only the records found based on the array of values'); 
    879879 
     880$finder = sfPropelFinder::from('Article')->where('Title', 'bar'); 
     881$t->is($finder->set(array('Title' => 'updated title'), true), 1, 'set() returns the number of updated rows, even with $forceIndividualSaves set to true'); 
     882$t->is(sfPropelFinder::from('Article')->where('Title', 'updated title')->count(), 2, 'set() updates only the records found based on the array of values'); 
     883 
    880884try 
    881885{