Development

Changeset 7938

You must first sign up to be able to contribute.

Changeset 7938

Show
Ignore:
Timestamp:
03/17/08 15:46:34 (9 months ago)
Author:
hartym
Message:

sfPropelImpersonatorPlugin: added flexibility for custom queries, doQuery is now separated in a doRawQuery returning a creole ResultSet? and the old doQuery behaving the same as always, but using doRawQuery to get the matching resultset.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfPropelImpersonatorPlugin/trunk/lib/sfPropelCriteriaImpersonator.class.php

    r7857 r7938  
    77class sfPropelCriteriaImpersonator 
    88{ 
     9  /** 
     10   * Removes select columns not used by a doSelect method (ie every select_columns but 
     11   * _not_ the AS columns. Criteria::clearSelectColumns clears both. 
     12   * 
     13   * @param Criteria $criteria 
     14   * 
     15   * @return Criteria 
     16   */ 
     17  static public function clearSelectColumns(Criteria $criteria) 
     18  { 
     19    $criteria = clone $criteria; 
     20    $asColumns = $criteria->getAsColumns(); 
     21    $criteria->clearSelectColumns()->clearOrderByColumns(); 
     22 
     23    // hack against criteria private policy 
     24    foreach ($asColumns as $key => $value) 
     25    { 
     26      $criteria->addAsColumn($key, $value); 
     27    } 
     28 
     29    return $criteria; 
     30  } 
     31 
    932  /** 
    1033   * Returns an SQL equality using Criteria::IN or Criteria::EQUAL, depending on parameter type (array or int/string) 
  • plugins/sfPropelImpersonatorPlugin/trunk/lib/sfPropelObjectPeerImpersonator.class.php

    r7857 r7938  
    9090    $currentStartColumnForImpersonatedObjects = 0, 
    9191    $classToIndex, 
    92     $relations, 
    9392    $currentIndex; 
     93 
     94  /** 
     95   * Relations directory extracted from Propel database map 
     96   */ 
     97  protected $relations; 
    9498 
    9599  /** 
     
    125129    { 
    126130      throw new Exception('No parameters given to constructor, aborting.'); 
    127     } 
    128   } 
    129  
    130   /** 
    131    * Constructor parameters parser 
    132    * 
    133    * Takes a list of strings (shortcut for array('object', 'PropelObjectClassName')), array(type, name) and array(arrays). 
    134    */ 
    135   protected function parseConstructorParameters($args, &$customFields, $finalize=false) 
    136   { 
    137     foreach ($args as $arg) 
    138     { 
    139       if (is_array($arg)) 
    140       { 
    141         // ignore empty arrays 
    142  
    143         if (isset($arg[0])) 
    144         { 
    145           if (is_array($arg[0])) 
    146           { 
    147             // Array of array, recursion 
    148  
    149             $this->parseConstructorParameters($arg, $customFields); 
    150           } 
    151           elseif ('object'==$arg[0]) 
    152           { 
    153             // Propel object, long version 
    154  
    155             if (!isset($arg[1])) 
    156             { 
    157               throw new Exception('Invalid constructor item found: An object must take the object class name as parameter'); 
    158             } 
    159             else 
    160             { 
    161               $this->addCustomObjectIfApplicable($customFields); 
    162               $this->addObjectByClassName($arg[1]); 
    163             } 
    164           } 
    165           else 
    166           { 
    167             // Pure custom field 
    168  
    169             array_push($customFields, $arg); 
    170           } 
    171         } 
    172       } 
    173       else 
    174       { 
    175         // Propel object, short version. Will only be applicable on first level, deeper levels must use the 
    176         // long version (ie array('object', 'PropelObjectClassName')) 
    177  
    178         $this->addCustomObjectIfApplicable($customFields); 
    179         $this->addObjectByClassName($arg); 
    180       } 
    181     } 
    182  
    183     // If we're in top recursion level, add remaining custom objects if any 
    184     if ($finalize) 
    185     { 
    186       $this->addCustomObjectIfApplicable($customFields); 
    187131    } 
    188132  } 
     
    372316   * Propel impersonating doCount ^^ 
    373317   */ 
    374   public function doCount(Criteria $criteria, $distinct = false, $con=null) 
    375   { 
    376     $criteria = clone $criteria; 
    377     $asColumns = $criteria->getAsColumns(); 
    378     $criteria->clearSelectColumns()->clearOrderByColumns(); 
    379  
    380     // hack against criteria private policy 
    381     foreach ($asColumns as $key => $value) 
    382     { 
    383       $criteria->addAsColumn($key, $value); 
    384     } 
     318  public function doCount(Criteria $criteria, $distinct = false, $con=null, $usePeerClassSelectResultSetMethod=true) 
     319  { 
     320    $criteria = sfPropelCriteriaImpersonator::clearSelectColumns($criteria); 
    385321 
    386322    $peerClass = get_class($this->objects[0]).'Peer'; 
     
    400336    } 
    401337 
    402     $rs = call_user_func(array($peerClass, 'doSelectRS'), $criteria, $con); 
     338    if ($usePeerClassSelectResultSetMethod) 
     339    { 
     340      // kept for compatibility, but maybe useless. Problem with this is that doSelectRS can modify 
     341      // criteria, especially because of propel behaviours. That should be ok, but doSelect do not use 
     342      // it, so you can't use same criteria for both. 
     343      // 
     344      // for now, $usePeerClassSelectResultSetMethod is true by default 
     345      $rs = call_user_func(array($peerClass, 'doSelectRS'), $criteria, $con); 
     346    } 
     347    else 
     348    { 
     349      $rs = BasePeer::doSelect($criteria, $con); 
     350    } 
    403351 
    404352    if ($rs->next()) 
     
    414362  /** 
    415363   * Custom query select 
     364   * 
     365   * @param mixed 
     366   * @param mixed 
     367   *   ... 
     368   * @param mixed 
     369   * 
     370   * @return array 
    416371   */ 
    417372  public function doQuery() 
     373  { 
     374    return $this->populateObjects($this->doRawQuery(func_get_args())); 
     375  } 
     376 
     377  /** 
     378   * Custom query 
     379   * 
     380   * @param array $arguments 
     381   * 
     382   * @return ResultSet 
     383   */ 
     384  public function doRawQuery(array $arguments=array()) 
    418385  { 
    419386    if ($this->query) 
     
    427394      $this->currentIndex = 1; 
    428395 
    429       foreach(func_get_args() as $argument) 
     396      foreach($arguments as $argument) 
    430397      { 
    431398        $this->setPreparedParameter($statement, $argument); 
    432399      } 
    433400 
    434       $resultset = $statement->executeQuery(ResultSet::FETCHMODE_NUM); 
    435       return $this->populateObjects($resultset); 
     401      return $statement->executeQuery(ResultSet::FETCHMODE_NUM); 
    436402    } 
    437403    else 
     
    948914    } 
    949915  } 
     916 
     917  /** 
     918   * Constructor parameters parser 
     919   * 
     920   * Takes a list of strings (shortcut for array('object', 'PropelObjectClassName')), array(type, name) and array(arrays). 
     921   */ 
     922  protected function parseConstructorParameters($args, &$customFields, $finalize=false) 
     923  { 
     924    foreach ($args as $arg) 
     925    { 
     926      if (is_array($arg)) 
     927      { 
     928        // ignore empty arrays 
     929 
     930        if (isset($arg[0])) 
     931        { 
     932          if (is_array($arg[0])) 
     933          { 
     934            // Array of array, recursion 
     935 
     936            $this->parseConstructorParameters($arg, $customFields); 
     937          } 
     938          elseif ('object'==$arg[0]) 
     939          { 
     940            // Propel object, long version 
     941 
     942            if (!isset($arg[1])) 
     943            { 
     944              throw new Exception('Invalid constructor item found: An object must take the object class name as parameter'); 
     945            } 
     946            else 
     947            { 
     948              $this->addCustomObjectIfApplicable($customFields); 
     949              $this->addObjectByClassName($arg[1]); 
     950            } 
     951          } 
     952          else 
     953          { 
     954            // Pure custom field 
     955 
     956            array_push($customFields, $arg); 
     957          } 
     958        } 
     959      } 
     960      else 
     961      { 
     962        // Propel object, short version. Will only be applicable on first level, deeper levels must use the 
     963        // long version (ie array('object', 'PropelObjectClassName')) 
     964 
     965        $this->addCustomObjectIfApplicable($customFields); 
     966        $this->addObjectByClassName($arg); 
     967      } 
     968    } 
     969 
     970    // If we're in top recursion level, add remaining custom objects if any 
     971    if ($finalize) 
     972    { 
     973      $this->addCustomObjectIfApplicable($customFields); 
     974    } 
     975  } 
    950976}