Development

Changeset 10123

You must first sign up to be able to contribute.

Changeset 10123

Show
Ignore:
Timestamp:
07/04/08 19:00:48 (5 months ago)
Author:
francois
Message:

sfPropelFinderPlugin Added support for with() in findPk() (and documented the method)

Files:

Legend:

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

    r10120 r10123  
    487487}}} 
    488488 
     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 
    489509=== Hacking the finder === 
    490510 
     
    517537 * Put as a parent class in the PeerBuilder so that every Peer class can be a finder 
    518538 * Merge with sfPropelImpersonatorPlugin! 
    519  * Add support for `with()` in `findPk()` (and document the method) 
    520539 * Handle complex queries with And and Or 
    521540 * Add a `__toString()` method which returns a var_export() of the results, or a description of the conditions if not yet executed 
     
    527546=== 2008-07-04 | Trunk === 
    528547 
     548 * francois: Added support for `with()` in `findPk()` (and documented the method) 
    529549 * francois: Added the ability to do left, right, and inner joins in a simple way 
    530550 * francois: Made `join()` useless if there is an explicit `where()` on the table afterwards 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r10120 r10123  
    330330  public function findPk($pk, $con = null) 
    331331  { 
    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(); 
    335359    } 
    336360    else 
    337361    { 
    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    }  
    343374  } 
    344375   
     
    10591090      return $this->join(substr($name, 4), $arguments); 
    10601091    } 
    1061     if(strpos($name, 'Join') > 0) 
    1062     { 
    1063       $pos = strpos($name, 'Join'); 
     1092    if(($pos = strpos($name, 'Join')) > 0) 
     1093    { 
    10641094      $joinType = strtoupper(substr($name, 0, $pos)) . ' JOIN'; 
    10651095      array_push($arguments, $joinType); 
  • plugins/sfPropelFinderPlugin/test/unit/sfPropelFinderTest.php

    r10120 r10123  
    6565ArticlePeer::doDeleteAll(); 
    6666 
    67 $t = new lime_test(102, new lime_output_color()); 
     67$t = new lime_test(105, new lime_output_color()); 
    6868 
    6969$t->diag('find()'); 
     
    226226$t->is(count($articles), 2, 'findPk() returns the objects with the primary keys matching the arguments'); 
    227227 
     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 
     231ArticlePeer::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(); 
     239try 
     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} 
     244catch(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 
    228251$t->diag('Instanciation possibilities'); 
     252 
     253ArticlePeer::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(); 
    229266 
    230267$finder = new sfPropelFinder('Article');