Changeset 10123
- Timestamp:
- 07/04/08 19:00:48 (5 months ago)
- Files:
-
- plugins/sfPropelFinderPlugin/README (modified) (3 diffs)
- plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php (modified) (2 diffs)
- plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelFinderPlugin/README
r10120 r10123 487 487 }}} 488 488 489 === Finding Objects From A Primary Key === 490 491 {{{ 492 #!php 493 <?php 494 $article = sfPropelFinder::from('Article')->findPk(123); 495 // is equivalent to 496 $article = ArticlePeer::retrieveByPk(123); 497 498 // But it's longer to write so what's the point? 499 // You can hydrate related objects by using with() 500 // So you need a single query to retrieve an object and its related objects 501 $article = sfPropelFinder::from('Article')-> 502 with('Category', 'I18n')-> 503 findPk(123); 504 505 // Also works for objects with composite primary keys 506 $articleI18n = sfPropelFinder::from('ArticleI18n')->findPk(array(123, 'fr')); 507 }}} 508 489 509 === Hacking the finder === 490 510 … … 517 537 * Put as a parent class in the PeerBuilder so that every Peer class can be a finder 518 538 * Merge with sfPropelImpersonatorPlugin! 519 * Add support for `with()` in `findPk()` (and document the method)520 539 * Handle complex queries with And and Or 521 540 * Add a `__toString()` method which returns a var_export() of the results, or a description of the conditions if not yet executed … … 527 546 === 2008-07-04 | Trunk === 528 547 548 * francois: Added support for `with()` in `findPk()` (and documented the method) 529 549 * francois: Added the ability to do left, right, and inner joins in a simple way 530 550 * francois: Made `join()` useless if there is an explicit `where()` on the table afterwards plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php
r10120 r10123 330 330 public function findPk($pk, $con = null) 331 331 { 332 if(is_array($pk)) 333 { 334 $ret = call_user_func(array($this->getPeerClass(), 'retrieveByPks'), $pk, $con); 332 $tableMap = call_user_func(array($this->peerClass, 'getTableMap')); 333 $pkColumns = array(); 334 foreach ($tableMap->getColumns() as $column) 335 { 336 if($column->isPrimaryKey()) 337 { 338 $pkColumns []= $column->getFullyQualifiedName(); 339 } 340 } 341 if(($count = count($pkColumns)) > 1) 342 { 343 // composite primary key 344 if(!is_array($pk)) 345 { 346 throw new Exception(sprintf('Class %s has a composite primary key and expects %s parameters to retrieve a record by pk', $this->class, join(', ', $pkColumns))); 347 } 348 else if (is_array($count[0])) 349 { 350 // array of arrays 351 // sorry the finder can't do that on objects with composte primary keys 352 throw new Exception('Impossible to find a list of Pks on an objects with composite primary keys'); 353 } 354 for ($i=0; $i < $count; $i++) 355 { 356 $this->criteria->add($pkColumns[$i], $pk[$i]); 357 } 358 return $this->findOne(); 335 359 } 336 360 else 337 361 { 338 $ret = call_user_func(array($this->getPeerClass(), 'retrieveByPk'), $pk, $con); 339 } 340 $this->updateLatestQuery($con); 341 342 return $ret; 362 // simple primary kay 363 if(is_array($pk)) 364 { 365 $this->criteria->add($pkColumns[0], $pk, Criteria::IN); 366 return $this->find(); 367 } 368 else 369 { 370 $this->criteria->add($pkColumns[0], $pk); 371 return $this->findOne(); 372 } 373 } 343 374 } 344 375 … … 1059 1090 return $this->join(substr($name, 4), $arguments); 1060 1091 } 1061 if(strpos($name, 'Join') > 0) 1062 { 1063 $pos = strpos($name, 'Join'); 1092 if(($pos = strpos($name, 'Join')) > 0) 1093 { 1064 1094 $joinType = strtoupper(substr($name, 0, $pos)) . ' JOIN'; 1065 1095 array_push($arguments, $joinType); plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php
r10120 r10123 65 65 ArticlePeer::doDeleteAll(); 66 66 67 $t = new lime_test(10 2, new lime_output_color());67 $t = new lime_test(105, new lime_output_color()); 68 68 69 69 $t->diag('find()'); … … 226 226 $t->is(count($articles), 2, 'findPk() returns the objects with the primary keys matching the arguments'); 227 227 228 $article = $finder->with('Category')->findPk($article2->getId()); 229 $t->cmp_ok(strpos($finder->getLatestQuery(), 'SELECT article.ID, article.VERSION, article.TITLE, article.CATEGORY_ID, category.ID, category.NAME FROM article, category'), '===', 0, 'findPk() is compatible with with()'); 230 231 ArticlePeer::doDeleteAll(); 232 233 $article1 = new Article(); 234 $article1->setTitle('foo'); 235 $article1->setCulture('fr'); 236 $article1->setContent('Bar'); 237 $article1->save(); 238 $articlei18n1 = $article1->getCurrentArticleI18n(); 239 try 240 { 241 $articleI18n = sfPropelFinder::from('ArticleI18n')->findPk($articlei18n1->getId()); 242 $t->fail('findPk() expects an array of values for objects with composite primary keys'); 243 } 244 catch(Exception $e) 245 { 246 $t->pass('findPk() expects an array of values for objects with composite primary keys'); 247 } 248 $articleI18n = sfPropelFinder::from('ArticleI18n')->findPk(array($articlei18n1->getId(), $articlei18n1->getCulture())); 249 $t->ok($articleI18n->equals($articlei18n1), 'findPk() retrieves objects with composite primary keys based on an array of Pks'); 250 228 251 $t->diag('Instanciation possibilities'); 252 253 ArticlePeer::doDeleteAll(); 254 255 $article1 = new Article(); 256 $article1->setTitle('foo'); 257 $article1->save(); 258 259 $article2 = new Article(); 260 $article2->setTitle('foo2'); 261 $article2->save(); 262 263 $article3 = new Article(); 264 $article3->setTitle('foo3'); 265 $article3->save(); 229 266 230 267 $finder = new sfPropelFinder('Article');