Changeset 10655
- Timestamp:
- 08/04/08 21:50:47 (4 months ago)
- Files:
-
- plugins/sfPropelFinderPlugin/README (modified) (4 diffs)
- plugins/sfPropelFinderPlugin/lib/DbFinder.php (modified) (6 diffs)
- plugins/sfPropelFinderPlugin/lib/sfDoctrineFinder.php (modified) (9 diffs)
- plugins/sfPropelFinderPlugin/lib/sfDoctrineFinderListener.php (modified) (1 diff)
- plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php (modified) (23 diffs)
- plugins/sfPropelFinderPlugin/lib/sfPropelFinderPager.php (modified) (4 diffs)
- plugins/sfPropelFinderPlugin/test/unit/sfDoctrineFinderTest.php (modified) (4 diffs)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderInternalsTest.php (modified) (1 diff)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderPagerTest.php (modified) (1 diff)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderRelationsTest.php (modified) (2 diffs)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelFinderPlugin/README
r10606 r10655 514 514 === Writing your own business logic into a finder === 515 515 516 You can create a new finder for your objects, with custom methods. The only prerequisites are to extend `sfPropelFinder` and to define a protected `$ peerClass` property. Then, the object has access to a protected `$criteria` property, which is a Propel Criteria that can be augmented in the usual way. Don't forget to return the current object (`$this`) in the new methods.516 You can create a new finder for your objects, with custom methods. The only prerequisites are to extend `sfPropelFinder` and to define a protected `$class` property. Then, the object has access to a protected `$criteria` property, which is a Propel Criteria that can be augmented in the usual way. Don't forget to return the current object (`$this`) in the new methods. 517 517 518 518 {{{ … … 522 522 class ArticleFinder extends sfPropelFinder 523 523 { 524 protected $ peerClass = 'ArticlePeer';524 protected $class = 'Article'; 525 525 526 526 public function recent() … … 596 596 597 597 == TODO / Ideas == 598 598 599 * Fix connection issue (defined at initialization with Doctrine, at the end with Propel) 599 600 * Allow i18n hydration of related objects (#3897) 600 601 * Allow `between` as a `where()` operator for simplicity … … 611 612 == Changelog == 612 613 613 === 2008-08-02 | Trunk === 614 614 === 2008-08-04 | Trunk === 615 616 * francois: Implemented `sfDoctrineFinder::count()` 617 * francois: [BC Break] Replaced `sfPropelFinder::setPeerClass()` by `sfPropelFinder::setClass()` (will break classes extending sfPropelFinder) 618 * francois: Refactored connection management, query reinitialization, and simplified executers signature 615 619 * francois: Implemented `sfDoctrineFinder::fromArray()`, and `sfDoctrineFinder::getLatestQuery()` 616 620 * francois: Added `DbFinderAdminGenerator` (WIP) plugins/sfPropelFinderPlugin/lib/DbFinder.php
r10606 r10655 10 10 */ 11 11 12 class DbFinder12 abstract class DbFinder 13 13 { 14 14 const … … 18 18 protected $relations = array(); 19 19 20 public function __construct($class = '', $connection = null) 21 { 22 $this->connection = $connection; 23 $class = $class ? $class : $this->class; 24 if($class) 25 { 26 list($class, $alias) = $this->getClassAndAlias($class); 27 $this->setClass($class, $alias); 28 } 29 } 30 20 31 // Finder Initializers 21 32 … … 25 36 * 26 37 * @param mixed $from The data to initialize the finder with 38 * @param mixed $connection Optional connection object 39 * 27 40 * @return DbFinder a finder object 28 41 * @throws Exception If the data is neither a classname nor an array 29 42 */ 30 public static function from($from )43 public static function from($from, $connection = null) 31 44 { 32 45 if (is_string($from)) 33 46 { 34 return self::fromClass($from );47 return self::fromClass($from, $connection); 35 48 } 36 49 if (is_array($from) || $from instanceof Doctrine_Collection) … … 41 54 } 42 55 43 /**56 /** 44 57 * Class initializer 45 58 * 46 59 * @param string $from Model classname on which the search will be done 60 * @param mixed $connection Optional connection object 61 * 47 62 * @return DbFinder a finder object 48 63 */ 49 public static function fromClass($class )64 public static function fromClass($class, $connection = null) 50 65 { 51 66 if(strpos($class, ' ') !== false) … … 60 75 if($tmp instanceof BaseObject) 61 76 { 62 $finder = new sfPropelFinder($class );77 $finder = new sfPropelFinder($class, $connection); 63 78 } 64 79 else if($tmp instanceof Doctrine_Record) 65 80 { 66 $finder = new sfDoctrineFinder($class );81 $finder = new sfDoctrineFinder($class, $connection); 67 82 } 68 83 else … … 202 217 } 203 218 219 // Finder accessors 220 221 abstract public function getClass(); 222 abstract public function setClass($class, $alias = ''); 223 abstract public function getConnection(); 224 abstract public function setConnection($connection); 225 226 // Finder executers 227 228 abstract public function count($distinct = false); 229 abstract public function find($limit = null); 230 abstract public function findOne(); 231 abstract public function findLast($column = null); 232 abstract public function findFirst($column = null); 233 abstract public function findBy($columnName, $value, $limit = null); 234 abstract public function findOneBy($columnName, $value); 235 abstract public function findPk($pk); 236 abstract public function delete($forceIndividualDeletes = false); 237 abstract public function paginate($page = 1, $maxPerPage = 10); 238 abstract public function set($values, $forceIndividualSaves = false); 239 240 // Finder utilities 241 242 abstract public function getLatestQuery(); 243 abstract protected function cleanup(); 244 204 245 // Finder Outputters 205 246 plugins/sfPropelFinderPlugin/lib/sfDoctrineFinder.php
r10606 r10655 13 13 { 14 14 protected 15 $connection = null, 15 16 $class = null, 17 $alias = null, 16 18 $object = null, 19 $reinit = true, 17 20 $queryListener = null, 18 21 $withClasses = array(), 19 22 $withColumns = array(), 20 23 $culture = null; 21 22 public function __construct($class = '')23 {24 if($class)25 {26 list($class, $alias) = $this->getClassAndAlias($class);27 $this->setClass($class, $alias);28 }29 elseif($this->class)30 {31 list($class, $alias) = $this->getClassAndAlias($this->class);32 $this->setClass($class, $alias);33 }34 }35 24 36 25 public function getClass() … … 38 27 return $this->class; 39 28 } 40 29 30 /** 31 * Caution: reinitializes the finder's Doctrine_Query object 32 */ 41 33 public function setClass($class, $alias = '') 42 34 { 43 35 $this->addRelation($class, $alias); 44 36 $this->class = $class; 37 $this->alias = $alias; 45 38 $this->object = new $class(); 46 $this->query = Doctrine_Query::create()->from($class . ' ' . $alias); 39 $this->initialize($this->connection, $class, $alias); 40 41 return $this; 42 } 43 44 public function getConnection() 45 { 46 return $this->connection; 47 } 48 49 /** 50 * Caution: reinitializes the finder's Doctrine_Query object 51 */ 52 public function setConnection($connection) 53 { 54 $this->connection = $connection; 55 if($this->class) 56 { 57 $this->initialize($connection, $this->class, $this->alias); 58 } 59 60 return $this; 61 } 62 63 public function initialize($connection, $class, $alias) 64 { 65 $this->query = Doctrine_Query::create($connection)->from($class . ' ' . $alias); 47 66 $this->queryListener = sfDoctrineFinderListener::getInstance(); 48 49 67 50 68 return $this; … … 62 80 } 63 81 64 public function getLatestQuery( $con = null)82 public function getLatestQuery() 65 83 { 66 84 return $this->queryListener->getLatestQuery(); 67 85 } 68 86 69 public function updateLatestQuery( $con = null)87 public function updateLatestQuery() 70 88 { 71 89 throw new Exception('This method is not yet implemented'); … … 105 123 // Finder Executers 106 124 107 public function count($con = null, $reinitCriteria = true) 108 { 109 throw new Exception('This method is not yet implemented'); 110 } 111 112 public function find($limit = null, $con = null, $reinitCriteria = false) 125 public function count($distinct = false) 126 { 127 $res = $this->query->count(); 128 $this->cleanup(); 129 130 return $res; 131 } 132 133 public function find($limit = null) 113 134 { 114 135 if($limit) … … 116 137 $this->query->limit($limit); 117 138 } 118 return $this->query->execute(); 119 } 120 121 public function findOne($con = null, $reinitCriteria = true) 122 { 123 $coll = $this->find(1, $con, $reinitCriteria); 124 125 return isset($coll[0]) ? $coll[0] : null; 126 } 127 128 public function findLast($column = null, $con = null, $reinitCriteria = true) 139 $res = $this->query->execute(); 140 $this->cleanup(); 141 142 return $res; 143 } 144 145 public function findOne() 146 { 147 $records = $this->find(1); 148 149 return isset($records[0]) ? $records[0] : null; 150 } 151 152 public function findLast($column = null) 129 153 { 130 154 if($column) … … 140 164 } 141 165 142 public function findFirst($column = null , $con = null, $reinitCriteria = true)166 public function findFirst($column = null) 143 167 { 144 168 if($column) … … 154 178 } 155 179 156 public function findBy($columnName, $value, $limit = null , $con = null, $reinitCriteria = false)180 public function findBy($columnName, $value, $limit = null) 157 181 { 158 182 $column = $this->getColName($columnName); 159 183 $this->where($column, '=', $value); 160 184 161 return $this->find($limit , $con, $reinitCriteria);162 } 163 164 public function findOneBy($columnName, $value , $con = null, $reinitCriteria = false)185 return $this->find($limit); 186 } 187 188 public function findOneBy($columnName, $value) 165 189 { 166 190 $column = $this->getColName($columnName); 167 191 $this->where($column, '=', $value); 168 192 169 return $this->findOne( $con, $reinitCriteria);170 } 171 172 public function findPk($pk , $con = null)193 return $this->findOne(); 194 } 195 196 public function findPk($pk) 173 197 { 174 198 $pkColumns = $this->getObject()->getTable()->getIdentifierColumnNames(); … … 208 232 } 209 233 210 public function delete($con = null, $reinitCriteria = true) 211 { 212 throw new Exception('This method is not yet implemented'); 213 } 214 215 public function paginate($page = 1, $maxPerPage = 10, $con = null) 216 { 217 throw new Exception('This method is not yet implemented'); 218 } 219 220 public function set($values, $forceIndividualSaves = false, $con = null, $reinitCriteria = true ) 221 { 222 throw new Exception('This method is not yet implemented'); 223 } 224 225 public function doFind($con = null) 234 public function delete($forceIndividualDeletes = false) 235 { 236 $this->cleanup(); 237 throw new Exception('This method is not yet implemented'); 238 } 239 240 public function paginate($page = 1, $maxPerPage = 10) 241 { 242 throw new Exception('This method is not yet implemented'); 243 } 244 245 public function set($values, $forceIndividualSaves = false) 246 { 247 $this->cleanup(); 248 throw new Exception('This method is not yet implemented'); 249 } 250 251 protected function cleanup() 252 { 253 if($this->reinit) 254 { 255 $this->initialize($this->connection, $this->class, $this->alias); 256 } 257 258 return $this; 259 } 260 261 262 public function doFind() 226 263 { 227 264 throw new Exception('This method is not yet implemented'); … … 466 503 } 467 504 505 public function keepQuery($keep = true) 506 { 507 $this->reinit = $keep; 508 509 return $this; 510 } 511 468 512 protected function hasRelation($peerName) 469 513 { plugins/sfPropelFinderPlugin/lib/sfDoctrineFinderListener.php
r10607 r10655 52 52 public function getLatestQuery() 53 53 { 54 return $this->queries[count($this->queries)-1];54 return ($count = count($this->queries)) ? $this->queries[$count - 1] : ''; 55 55 } 56 56 plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php
r10592 r10655 12 12 class sfPropelFinder extends DbFinder 13 13 { 14 protected $class = null; 15 protected $peerClass = null; 16 protected $databaseMap = null; 17 protected $criteria = null; 18 protected $latestQuery = ''; 19 protected $criterions = array(); 20 protected $withClasses = array(); 21 protected $withColumns = array(); 22 protected $culture = null; 23 24 public function __construct($class = '') 25 { 26 $this->criteria = new Criteria(); 27 if($class) 28 { 29 // Instanciation with a class 30 list($class, $alias) = $this->getClassAndAlias($class); 31 $this->class = $class; 32 $tmp = new $class; 33 $this->setPeerClass(get_class($tmp->getPeer()), $alias); 34 } 35 else if($this->peerClass) 36 { 37 // Instanciation for a child of sfPropelFinder 38 $this->addRelation($this->peerClass); 39 } 40 if($this->peerClass) 41 { 42 $this->initialize(); 43 } 44 } 45 14 protected 15 $class = null, 16 $peerClass = null, 17 $object = null, 18 $databaseMap = null, 19 $connection = null, 20 $criteria = null, 21 $reinit = true, 22 $latestQuery = '', 23 $criterions = array(), 24 $withClasses = array(), 25 $withColumns = array(), 26 $culture = null; 27 28 public function getClass() 29 { 30 return $this->class; 31 } 32 33 public function setClass($class, $alias = '') 34 { 35 $this->class = $class; 36 $this->object = new $class(); 37 $this->peerClass = get_class($this->object->getPeer()); 38 $this->addRelation($this->peerClass, $alias); 39 $this->initialize(); 40 41 return $this; 42 } 43 44 public function getConnection() 45 { 46 if(is_null($this->connection)) 47 { 48 $name = $this->peerClass ? constant($this->peerClass.'::DATABASE_NAME') : ''; 49 $this->connection = Propel::getConnection($name); 50 } 51 52 return $this->connection; 53 } 54 55 public function setConnection($connection) 56 { 57 $this->connection = $connection; 58 59 return $this; 60 } 61 46 62 public function initialize() 47 63 { 64 $this->reinitCriteria(); 48 65 $mapBuilder = call_user_func(array($this->peerClass, 'getMapBuilder')); 49 66 $mapBuilder->doBuild(); … … 51 68 } 52 69 53 public function getPeerClass()54 {55 return $this->peerClass;56 }57 58 public function setPeerClass($peerClass, $alias = '')59 {60 $this->addRelation($peerClass, $alias);61 $this->peerClass = $peerClass;62 63 return $this;64 }65 66 70 protected function addRelation($peerClass, $alias = '') 67 71 { … … 88 92 } 89 93 90 public function getLatestQuery($con = null) 91 { 92 if(is_null($con)) $con = Propel::getConnection(); 93 if(method_exists($con, 'getLastExecutedQuery')) 94 public function getLatestQuery() 95 { 96 if(method_exists($this->getConnection(), 'getLastExecutedQuery')) 94 97 { 95 98 return $this->latestQuery; … … 101 104 } 102 105 103 public function updateLatestQuery($con = null) 104 { 105 if(is_null($con)) $con = Propel::getConnection(); 106 if(method_exists($con, 'getLastExecutedQuery')) 107 { 108 $this->latestQuery = $con->getLastExecutedQuery(); 106 public function updateLatestQuery() 107 { 108 if(method_exists($this->getConnection(), 'getLastExecutedQuery')) 109 { 110 $this->latestQuery = $this->getConnection()->getLastExecutedQuery(); 109 111 } 110 112 } … … 143 145 // Finder Executers 144 146 145 public function count($con = null, $reinitCriteria = true) 146 { 147 $ret = call_user_func(array($this->getPeerClass(), 'doCount'), $this->getCriteria(), $con); 148 $this->updateLatestQuery($con); 149 if($reinitCriteria) 150 { 151 $this->reinitCriteria(); 152 } 147 public function count($distinct = false) 148 { 149 $ret = call_user_func(array($this->peerClass, 'doCount'), $this->getCriteria(), $distinct, $this->getConnection()); 150 $this->cleanup(); 153 151 154 152 return $ret; 155 153 } 156 154 157 public function find($limit = null , $con = null, $reinitCriteria = false)155 public function find($limit = null) 158 156 { 159 157 if($limit) … … 161 159 $this->criteria->setLimit($limit); 162 160 } 163 $ret = $this->doFind($con); 164 $this->updateLatestQuery($con); 165 if($reinitCriteria) 166 { 167 $this->reinitCriteria(); 168 } 161 $ret = $this->doFind(); 162 $this->cleanup(); 169 163 170 164 return $ret; 171 165 } 172 166 173 public function findOne( $con = null, $reinitCriteria = true)167 public function findOne() 174 168 { 175 169 $this->criteria->setLimit(1); 176 $ret = $this->doFind($con); 177 $this->updateLatestQuery($con); 178 if($reinitCriteria) 179 { 180 $this->reinitCriteria(); 181 } 182 if ($ret) 183 { 184 return $ret[0]; 185 } 186 return null; 187 } 188 189 public function findLast($column = null, $con = null, $reinitCriteria = true) 170 $ret = $this->doFind(); 171 $this->cleanup(); 172 173 return isset($ret[0]) ? $ret[0] : null; 174 } 175 176 public function findLast($column = null) 190 177 { 191 178 if($column) … … 198 185 } 199 186 200 return $this->findOne( $con, $reinitCriteria);201 } 202 203 public function findFirst($column = null , $con = null, $reinitCriteria = true)187 return $this->findOne(); 188 } 189 190 public function findFirst($column = null) 204 191 { 205 192 if($column) … … 212 199 } 213 200 214 return $this->findOne( $con, $reinitCriteria);215 } 216 217 public function findBy($columnName, $value, $limit = null , $con = null, $reinitCriteria = false)201 return $this->findOne(); 202 } 203 204 public function findBy($columnName, $value, $limit = null) 218 205 { 219 206 $column = $this->getColName($columnName); 220 207 $this->addCondition('and', $column, $value, Criteria::EQUAL); 221 208 222 return $this->find($limit , $con, $reinitCriteria);223 } 224 225 public function findOneBy($columnName, $value , $con = null, $reinitCriteria = false)209 return $this->find($limit); 210 } 211 212 public function findOneBy($columnName, $value) 226 213 { 227 214 $column = $this->getColName($columnName); 228 215 $this->addCondition('and', $column, $value, Criteria::EQUAL); 229 216 230 return $this->findOne( $con, $reinitCriteria);231 } 232 233 public function findPk($pk , $con = null)217 return $this->findOne(); 218 } 219 220 public function findPk($pk) 234 221 { 235 222 $tableMap = call_user_func(array($this->peerClass, 'getTableMap')); … … 283 270 * 284 271 * @param Boolean $forceIndividualDeletes If false (default), the resulting call is a BasePeer::doDelete(), ortherwise it is a series of delete() calls on all the found objects 285 * @param Connection $con optional connection object286 * @param Boolean $reinitCriteria If true (default), the finder Criteria objects is replaced by a new one after deletion287 272 * 288 273 * @return Integer Number of deleted rows 289 274 */ 290 public function delete($forceIndividualDeletes = false , $con = null, $reinitCriteria = true)275 public function delete($forceIndividualDeletes = false) 291 276 { 292 277 $deleteCriteria = $this->getCriteria(); 293 278 if($forceIndividualDeletes) 294 279 { 295 $objects = $this->find( null, $con, $reinitCriteria);280 $objects = $this->find(); 296 281 foreach($objects as $object) 297 282 { 298 $object->delete( );283 $object->delete($this->getConnection()); 299 284 } 300 285 $ret = count($objects); … … 308 293 $deleteCriteria = $this->addTrueCondition($deleteCriteria); 309 294 } 310 $ret = call_user_func(array($this->getPeerClass(), 'doDelete'), $deleteCriteria, $con); 311 } 312 $this->updateLatestQuery($con); 313 if($reinitCriteria) 314 { 315 $this->reinitCriteria(); 316 } 295 $ret = call_user_func(array($this->peerClass, 'doDelete'), $deleteCriteria, $this->getConnection()); 296 } 297 $this->cleanup(); 317 298 318 299 return $ret; 319 300 } 320 301 321 public function paginate($page = 1, $maxPerPage = 10 , $con = null)302 public function paginate($page = 1, $maxPerPage = 10) 322 303 { 323 304 // Children of sfPropelPager don't have a $class property, so we need to guess it 324 $class = isset($this->class) ? $this->class : sfPropelFinderUtils::getClassFromPeerClass($this->peerClass); 325 $pager = new sfPropelFinderPager($class, $maxPerPage); 305 $pager = new sfPropelFinderPager($this->class, $maxPerPage); 326 306 $pager->setFinder($this); 327 $pager->setConnection($con);328 307 $pager->setPage($page); 329 308 $pager->init(); … … 339 318 * @param Array $values Associative array of keys and values to replace 340 319 * @param Boolean $forceIndividualSaves If false (default), the resulting call is a BasePeer::doUpdate(), ortherwise it is a series of save() calls on all the found objects 341 * @param Connection $con optional connection object342 * @param Boolean $reinitCriteria If true (default), the finder Criteria objects is replaced by a new one after update343 320 * 344 321 * @return Integer Number of deleted rows 345 322 */ 346 public function set($values, $forceIndividualSaves = false , $con = null, $reinitCriteria = true)323 public function set($values, $forceIndividualSaves = false) 347 324 { 348 325 if (!is_array($values)) … … 352 329 if($forceIndividualSaves) 353 330 { 354 $objects = $this->find( null, $con, $reinitCriteria);331 $objects = $this->find(); 355 332 foreach ($objects as $object) 356 333 { … … 383 360 } 384 361 385 if(is_null($con)) $con = Propel::getConnection(); 386 $ret = BasePeer::doUpdate($find, $set, $con); 387 } 388 $this->updateLatestQuery($con); 389 if($reinitCriteria) 362 $ret = BasePeer::doUpdate($find, $set, $this->getConnection()); 363 } 364 $this->cleanup(); 365 366 return $ret; 367 } 368 369 protected function cleanup() 370 { 371 if($this->reinit) 390 372 { 391 373 $this->reinitCriteria(); 392 374 } 393 394 return $ret; 395 } 396 397 public function doFind($con = null) 375 $this->updateLatestQuery(); 376 377 return $this; 378 } 379 380 public function doFind() 398 381 { 399 382 $this->addMissingJoins(); … … 401 384 { 402 385 $c = $this->prepareCompositeCriteria(); 403 if(method_exists($this-> getPeerClass(), 'doSelectRS'))404 { 405 $resultSet = call_user_func(array($this-> getPeerClass(), 'doSelectRS'), $c, $con);386 if(method_exists($this->peerClass, 'doSelectRS')) 387 { 388 $resultSet = call_user_func(array($this->peerClass, 'doSelectRS'), $c, $this->getConnection()); 406 389 $propelVersion = '1.2'; 407 390 $nextFunction = 'next'; … … 410 393 else 411 394 { 412 $resultSet = call_user_func(array($this-> getPeerClass(), 'doSelectStmt'), $c, $con);395 $resultSet = call_user_func(array($this->peerClass, 'doSelectStmt'), $c, $this->getConnection()); 413 396 $propelVersion = '1.3'; 414 397 $nextFunction = 'fetch'; … … 417 400 418 401 // Hydrate the objects based on the resultset 419 $omClass = call_user_func(array($this-> getPeerClass(), 'getOMClass'));402 $omClass = call_user_func(array($this->peerClass, 'getOMClass')); 420 403 $cls = substr('.'.$omClass, strrpos('.'.$omClass, '.') + 1); 421 404 $objects = array(); … … 529 512 { 530 513 // No 'with', so we use the native Propel doSelect() 531 return call_user_func(array($this-> getPeerClass(), 'doSelect'), $this->buildCriteria(), $con);514 return call_user_func(array($this->peerClass, 'doSelect'), $this->buildCriteria(), $this->getConnection()); 532 515 } 533 516 } … … 566 549 $c->clearSelectColumns(); 567 550 // First come the columns of the main class 568 call_user_func(array($this-> getPeerClass(), 'addSelectColumns'), $c);551 call_user_func(array($this->peerClass, 'addSelectColumns'), $c); 569 552 // Then the related classes added by way of 'with' 570 553 foreach ($this->getWithClasses() as $className) … … 840 823 { 841 824 $relatedObjectTableName = $object->getPeer()->getTableMap()->getName(); 842 foreach (sfPropelFinderUtils::getColumnsForPeerClass($this-> getPeerClass()) as $c)825 foreach (sfPropelFinderUtils::getColumnsForPeerClass($this->peerClass) as $c) 843 826 { 844 827 if($c->getRelatedTableName() == $relatedObjectTableName) … … 939 922 { 940 923 $columnNames = array(); 941 foreach (sfPropelFinderUtils::getColumnsForPeerClass($this-> getPeerClass()) as $c)924 foreach (sfPropelFinderUtils::getColumnsForPeerClass($this->peerClass) as $c) 942 925 { 943 926 $columnNames []= $c->getPhpName(); … … 1006 989 } 1007 990 991 public function keepQuery($keep = true) 992 { 993 $this->reinit = $keep; 994 995 return $this; 996 } 997 1008 998 protected function hasRelation($peerName) 1009 999 { … … 1085 1075 protected function addTrueCondition(Criteria $c) 1086 1076 { 1087 $fieldNames = call_user_func(array($this-> getPeerClass(), 'getFieldNames'), BasePeer::TYPE_COLNAME);1077 $fieldNames = call_user_func(array($this->peerClass, 'getFieldNames'), BasePeer::TYPE_COLNAME); 1088 1078 $firstFieldName = $fieldNames[0]; 1089 1079 $c->add($firstFieldName, true, Criteria::BINARY_OR); … … 1142 1132 * Cloning instance should clone its components. 1143 1133 * 1144 * Solves problems with sfPropelFinderPager. 1134 * Solves problems with sfPropelFinderPager. 1145 1135 */ 1146 1136 public function __clone() plugins/sfPropelFinderPlugin/lib/sfPropelFinderPager.php
r10542 r10655 3 3 { 4 4 protected 5 $finder, 6 $connection; 7 5 $finder; 6 8 7 /** 9 8 * __construct … … 41 40 42 41 /** 43 * Set finder object for the pager44 *45 * @param sfPropelFinder $finder46 * @return void47 */48 public function setConnection($connection)49 {50 $this->connection = $connection;51 }52 53 /**54 * Get the connection for the pager55 *56 * @return Connection $connection57 */58 public function getConnection()59 {60 return $this->connection;61 }62 63 /**64 42 * init pager 65 43 * 66 44 * @return void 67 45 */ 68 public function init( $con = null)46 public function init() 69 47 { 70 48 $hasMaxRecordLimit = ($this->getMaxRecordLimit() !== false); … … 76 54 setLimit(0)-> 77 55 clearGroupByColumns()-> 78 count( $con ? $con : $this->getConnection());56 count(); 79 57 80 58 $this->setNbResults($hasMaxRecordLimit ? min($count, $maxRecordLimit) : $count); … … 135 113 * @return Array $array of BaseObject instances 136 114 */ 137 public function getResults( $con = null)115 public function getResults() 138 116 { 139 return $this->getFinder()->find( $con ? $con : $this->getConnection());117 return $this->getFinder()->find(); 140 118 } 141 119 plugins/sfPropelFinderPlugin/test/unit/sfDoctrineFinderTest.php
r10606 r10655 71 71 Doctrine_Query::create()->delete()->from('DArticle')->execute(); 72 72 73 $t = new lime_test( 47, new lime_output_color());73 $t = new lime_test(53, new lime_output_color()); 74 74 75 75 $t->diag('find()'); … … 278 278 $finder = new sfDoctrineFinder('DArticle'); 279 279 $t->is($finder->getClass(), 'DArticle', 'Record Class can be set during instanciation, in which case the finder is automatically initialized'); 280 $article s = $finder->find();281 $ article = $articles[0];280 $article = $finder->findOne(); 281 $t->isa_ok($article, 'DArticle', 'A finder instanciated directly with a Record class returns the correct objects'); 282 282 $t->is($article->getTitle(), 'foo', 'A finder instanciated directly with a Record class returns the correct objects'); 283 283 … … 285 285 $finder->setClass('DArticle'); 286 286 $t->is($finder->getClass(), 'DArticle', 'setClass() and getClass() are accesors to the protected $class property'); 287 $article s = $finder->find();288 $ article = $articles[0];287 $article = $finder->findOne(); 288 $t->isa_ok($article, 'DArticle', 'A finder instanciated directly with a Record class returns the correct objects'); 289 289 $t->is($article->getTitle(), 'foo', 'A finder can be instanciated without parameter, and initialized later after defining its class'); 290 290 … … 335 335 $articles = sfDoctrineFinder::from($category1->getArticles())->where('Title', 'aaaaa')->find(); 336 336 $t->is(count($articles), 1, 'A finder initialized from an array accepts further conditions'); 337 338 $t->diag('count()'); 339 340 Doctrine_Query::create()->delete()->from('DArticle')->execute(); 341 $finder = new sfDoctrineFinder('DArticle'); 342 $nbArticles = $finder->count(); 343 $t->isa_ok($nbArticles, 'integer', 'count() returns an integer'); 344 $t->is($nbArticles, 0, 'count() returns 0 on empty tables'); 345 $article2 = new DArticle(); 346 $article2->setTitle('foo2'); 347 $article2->save(); 348 $article3 = new DArticle(); 349 $article3->setTitle('foo3'); 350 $article3->save(); 351 $nbArticles = $finder->count(); 352 $t->is($nbArticles, 2, 'count() returns the number of records matching the condition'); 353 $nbArticles = $finder->whereTitle('foo2')->count(); 354 $t->is($nbArticles, 1, 'count() returns the number of records matching the condition'); plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderInternalsTest.php
r10342 r10655 60 60 $databaseManager->initialize(); 61 61 62 $con = Propel::getConnection();63 64 62 // cleanup database 65 63 ArticlePeer::doDeleteAll(); plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderPagerTest.php
r10546 r10655 140 140 class ArticleFinder extends sfPropelFinder 141 141 { 142 protected $ peerClass = 'ArticlePeer';142 protected $class = 'Article'; 143 143 } 144 144 $finder = new ArticleFinder(); plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderRelationsTest.php
r10592 r10655 77 77 $databaseManager->initialize(); 78 78 79 $con = Propel::getConnection();80 81 79 $t = new lime_test(82, new lime_output_color()); 82 80 … … 472 470 class ArticleFinder extends sfPropelFinder 473 471 { 474 protected $ peerClass = 'ArticlePeer';472 protected $class = 'Article'; 475 473 } 476 474 plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php
r10545 r10655 60 60 $databaseManager->initialize(); 61 61 62 $con = Propel::getConnection();63 64 62 // cleanup database 65 63 ArticlePeer::doDeleteAll(); 66 64 67 $t = new lime_test(12 0, new lime_output_color());65 $t = new lime_test(122, new lime_output_color()); 68 66 69 67 $t->diag('find()'); … … 266 264 267 265 $finder = new sfPropelFinder('Article'); 268 $t->is($finder->get PeerClass(), 'ArticlePeer', 'PeerClass can be set during instanciation, in which case the finder is automatically initialized');269 $article s = $finder->find();270 $ article = $articles[0];266 $t->is($finder->getClass(), 'Article', 'Class can be set during instanciation, in which case the finder is automatically initialized'); 267 $article = $finder->findOne(); 268 $t->isa_ok($article, 'Article', 'A finder instanciated directly with a class returns the correct objects'); 271 269 $t->is($article->getTitle(), 'foo', 'A finder instanciated directly with a class returns the correct objects'); 272 270 273 271 $finder = new sfPropelFinder(); 274 $finder->setPeerClass('ArticlePeer'); 275 $t->is($finder->getPeerClass(), 'ArticlePeer', 'setPeerClass() and getPeerClass() are accesors to the protected $peerClass property'); 276 $finder->initialize(); 277 $articles = $finder->find(); 278 $article = $articles[0]; 272 $finder->setClass('Article'); 273 $t->is($finder->getClass(), 'Article', 'setClass() and getClass() are accesors to the protected $class property'); 274 $article = $finder->findOne(); 275 $t->isa_ok($article, 'Article', 'A finder instanciated directly with a class returns the correct objects'); 279 276 $t->is($article->getTitle(), 'foo', 'A finder can be instanciated without parameter, and initialized later after defining its peer class'); 280 277 … … 284 281 class ArticleFinder extends sfPropelFinder 285 282 { 286 protected $ peerClass = 'ArticlePeer';283 protected $class = 'Article'; 287 284 } 288 285 $finder = new ArticleFinder(); 289 286 $article = $finder->findOne(); 290 $t->isa_ok($article, 'Article', 'A finder extending sfPropelFinder can be used directly if defining the $ peerClass property');287 $t->isa_ok($article, 'Article', 'A finder extending sfPropelFinder can be used directly if defining the $class property'); 291 288 $finder = new ArticleFinder(); 292 289 $articles = $finder->find(); 293 $t->is(count($articles), 3, 'A finder extending sfPropelFinder can be used directly if defining the $ peerClass property');290 $t->is(count($articles), 3, 'A finder extending sfPropelFinder can be used directly if defining the $class property'); 294 291 295 292 ArticlePeer::doDeleteAll();