Development

Changeset 10118

You must first sign up to be able to contribute.

Changeset 10118

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

sfPropelFinderPlugin Cleaning up:

  • Added a prove.php test file to launch all tests at once in a test harness
  • Moved utility methods as static methods of a third-party class to take some weight off the main class
  • Expanded TODO a bit
Files:

Legend:

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

    r10112 r10118  
    473473}}} 
    474474 
    475 == TODO == 
     475== TODO / Ideas == 
    476476 
    477477 * Put as a parent class in the PeerBuilder so that every Peer class can be a finder 
    478478 * Merge with sfPropelImpersonatorPlugin! 
    479479 * Add support for `with()` in `findPk()` (and document the method) 
     480 * Add the ability to do left, right, inner joins, etc. 
     481 * Make `join()` useless if there is an explicit `where()` on the table afterwards 
     482 * Change `with()` and `join()` to accept only one relation at a time, but with the ability to set the local and foreign parts of the relation. maybe replace the current multiple ability of these methods by `joins()` and `withs()` 
     483 * Handle complex queries with And and Or 
     484 * Add a `__toString()` method which returns a var_export() of the results, or a description of the conditions if not yet executed 
     485 * Implement iterator interface? That way, the query is only executed upon a foreach or an array access... And the finder can be seen as a collection 
     486 * Column finder, which provides an easy interface to Creole (and PDO) for retrieval of columns instead of objects? 
    480487 
    481488== Changelog == 
    482489 
    483 === 2008-07-03 | Trunk === 
    484  
     490=== 2008-07-04 | Trunk === 
     491 
     492 * francois: Added a `prove.php` test file to launch all tests at once in a test harness 
     493 * francois: Moved utility methods as static methods of a third-party class to take some weight off the main class 
    485494 * francois: Preferring `ClassName.ColumnName` over `ClassName.ColumnName` for complete column names 
    486495 * francois: Added Propel 1.3 compatibility 
  • plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php

    r10112 r10118  
    423423    } 
    424424  } 
    425    
    426425   
    427426  public function doFind($criteria, $con = null) 
     
    497496            } 
    498497          } 
    499           $this->relateObjects($withObj, $objectsInJoin, $isNewObject); 
     498          sfPropelFinderUtils::relateObjects($withObj, $objectsInJoin, $isNewObject); 
    500499          $objectsInJoin []= $withObj; 
    501500          if ($isNewObject) 
     
    555554      call_user_func(array($tempClass->getPeer(), 'addSelectColumns'), $c); 
    556555      // if join() wasn't called previously on this class, do a simple join 
    557       if(!in_array($this->getPeerClassFromClass($className), $this->relations)) 
     556      if(!in_array(sfPropelFinderUtils::getPeerClassFromClass($className), $this->relations)) 
    558557      { 
    559558        list($column1, $column2) = $this->getRelation($className); 
    560559        $c->addJoin($column1, $column2); 
    561         $this->relations[]= $this->getPeerClassFromClass($className); 
     560        $this->relations[]= sfPropelFinderUtils::getPeerClassFromClass($className); 
    562561      } 
    563562    } 
     
    570569      if($peerClass && !in_array($peerClass, $this->relations)) 
    571570      { 
    572         list($column1, $column2) = $this->getRelation($this->getClassFromPeerClass($peerClass)); 
     571        list($column1, $column2) = $this->getRelation(sfPropelFinderUtils::getClassFromPeerClass($peerClass)); 
    573572        $c->addJoin($column1, $column2); 
    574573        $this->relations[]= $peerClass; 
     
    578577     
    579578    return $c; 
    580   } 
    581    
    582   protected function relateObjects($new, $existingObjects, $isNew) 
    583   { 
    584     // brute force (to be optimized later) 
    585     foreach ($existingObjects as $existingObject) 
    586     { 
    587       $methodName = 'add'.get_class($existingObject); 
    588       if(method_exists($new, $methodName)) 
    589       { 
    590         if($isNew) 
    591         { 
    592           call_user_func(array($new, 'init'.get_class($existingObject).'s')); 
    593         } 
    594         call_user_func(array($new, $methodName), $existingObject); 
    595         break; 
    596       } 
    597     } 
    598579  } 
    599580   
     
    701682      array_shift($arguments); 
    702683    }  
    703     list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments); 
     684    list($value, $comparison) = sfPropelFinderUtils::getValueAndComparisonFromArguments($arguments); 
    704685 
    705686    $this->addCondition('and', $column, $value, $comparison ); 
     
    728709      array_shift($arguments); 
    729710    }  
    730     list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments); 
     711    list($value, $comparison) = sfPropelFinderUtils::getValueAndComparisonFromArguments($arguments); 
    731712 
    732713    $this->addCondition('and', $column, $value, $comparison); 
     
    755736      array_shift($arguments); 
    756737    }  
    757     list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments); 
     738    list($value, $comparison) = sfPropelFinderUtils::getValueAndComparisonFromArguments($arguments); 
    758739    $this->addCondition('or', $column, $value, $comparison); 
    759740     
     
    798779  } 
    799780   
    800   protected function getValueAndComparisonFromArguments($arguments = array()) 
    801   { 
    802     $comparison = Criteria::EQUAL; 
    803     switch (count($arguments)) 
    804     { 
    805       case 0: 
    806         $value = true; 
    807         break; 
    808       case 1: 
    809         $value = array_shift($arguments); 
    810         break; 
    811       case 2: 
    812         $comparison = array_shift($arguments); 
    813         $comparisonUp = trim(strtoupper($comparison)); 
    814         if(in_array($comparisonUp, array('LIKE', 'NOT LIKE', 'ILIKE', 'NOT ILIKE', 'IN', 'NOT IN', 'IS NULL', 'IS NOT NULL'))) 
    815         { 
    816           $comparison = ' '.$comparisonUp.' '; 
    817         } 
    818         $value = array_shift($arguments); 
    819         break; 
    820       default: 
    821         throw new Exception('sfPropelFinder::whereXXX() can only be called with one or two arguments'); 
    822     } 
    823  
    824     return array($value, $comparison); 
    825   } 
    826    
    827781  /** 
    828782   * Finder fluid method to restrict results to a related object 
     
    834788  { 
    835789    $relatedObjectTableName = $object->getPeer()->getTableMap()->getName(); 
    836     foreach ($this->getColumnsForPeerClass($this->getPeerClass()) as $c) 
     790    foreach (sfPropelFinderUtils::getColumnsForPeerClass($this->getPeerClass()) as $c) 
    837791    { 
    838792      if($c->getRelatedTableName() == $relatedObjectTableName) 
     
    912866  { 
    913867    $columnNames = array(); 
    914     foreach ($this->getColumnsForPeerClass($this->getPeerClass()) as $c) 
     868    foreach (sfPropelFinderUtils::getColumnsForPeerClass($this->getPeerClass()) as $c) 
    915869    { 
    916870      $columnNames []= $c->getPhpName(); 
     
    941895  { 
    942896    list($column1, $column2) = $this->getRelation($relatedClass); 
    943     $this->relations[]= $this->getPeerClassFromClass($relatedClass); 
     897    $this->relations[]= sfPropelFinderUtils::getPeerClassFromClass($relatedClass); 
    944898    if(!is_array($arguments)) 
    945899    { 
     
    968922      // try to find one to many relationship 
    969923      if($relation = $this->findRelation( 
    970         $this->getClassFromPeerClass($peerClass), 
    971         $this->getPeerClassFromClass($phpName))) 
     924        sfPropelFinderUtils::getClassFromPeerClass($peerClass), 
     925        sfPropelFinderUtils::getPeerClassFromClass($phpName))) 
    972926      { 
    973927        return array_reverse($relation); 
     
    979933  protected function findRelation($phpName, $peerClass) 
    980934  { 
    981     foreach ($this->getColumnsForPeerClass($peerClass) as $c) 
     935    foreach (sfPropelFinderUtils::getColumnsForPeerClass($peerClass) as $c) 
    982936    { 
    983937      if ($c->isForeignKey()) 
     
    985939        if(!$this->databaseMap->containsTable($c->getRelatedTableName())) 
    986940        { 
    987           $mapBuilder = call_user_func(array($this->getPeerClassFromClass($phpName), 'getMapBuilder'));  
     941          $mapBuilder = call_user_func(array(sfPropelFinderUtils::getPeerClassFromClass($phpName), 'getMapBuilder'));  
    988942          $mapBuilder->doBuild(); 
    989943        } 
     
    1001955  } 
    1002956   
    1003   protected function getPeerClassFromClass($class) 
    1004   { 
    1005     $tmp = new $class(); 
    1006     return get_class($tmp->getPeer()); 
    1007     $tmp->__destroy(); 
    1008   } 
    1009  
    1010   protected function getClassFromPeerClass($peerClass) 
    1011   { 
    1012     $omClass = call_user_func(array($peerClass, 'getOMClass')); 
    1013     return substr('.'.$omClass, strrpos('.'.$omClass, '.') + 1); 
    1014   } 
    1015    
    1016957  /** 
    1017958   * Behavior-like supplementary getter for supplementary columns added by way of withColumn() 
     
    1039980  } 
    1040981   
    1041   protected function getColumnsForPeerClass($peerClass) 
    1042   { 
    1043     if(class_exists($peerClass)) 
    1044     { 
    1045       $tableMap = call_user_func(array($peerClass, 'getTableMap')); 
    1046       return $tableMap->getColumns(); 
    1047     } 
    1048     return false; 
    1049   } 
    1050    
    1051982  protected function getColName($phpName, $peerClass = null, $withPeerClass = false) 
    1052983  { 
     
    1059990      // Table.Column 
    1060991      list($class, $phpName) = explode('.', $phpName); 
    1061       $peerClass = $this->getPeerClassFromClass($class); 
     992      $peerClass = sfPropelFinderUtils::getPeerClassFromClass($class); 
    1062993    } 
    1063994    else if(strpos($phpName, '_') !== false) 
     
    1067998      $class = substr($phpName, 0, $limit); 
    1068999      $phpName = substr($phpName, $limit + 1); 
    1069       $peerClass = $this->getPeerClassFromClass($class); 
     1000      $peerClass = sfPropelFinderUtils::getPeerClassFromClass($class); 
    10701001    } 
    10711002    if(!$peerClass) 
  • plugins/sfPropelFinderPlugin/package.xml

    r10047 r10118  
    2929        <file name="sfPropelFinder.php" role="data"/> 
    3030        <file name="sfPropelFinderPager.php" role="data"/> 
     31        <file name="sfPropelFinderUtils.php" role="data"/> 
    3132      </dir> 
    3233    </dir>