Development

Changeset 7566

You must first sign up to be able to contribute.

Changeset 7566

Show
Ignore:
Timestamp:
02/21/08 12:03:19 (9 months ago)
Author:
hartym
Message:

[sfPropelImpersonatorPlugin] added full support for adding extra custom fields in any propel objects, using the ->addAsColumn criteria method

Files:

Legend:

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

    r7218 r7566  
    1717  public function __construct($columns) 
    1818  { 
     19    foreach($columns as $index=>$column) 
     20    { 
     21      if (count($column) == 2) 
     22      { 
     23        $columns[$index][2] = trim(strtolower(preg_replace('/[^a-z]/i', '_', $column[1])), '_'); 
     24      } 
     25    } 
     26 
    1927    $this->columns = $columns; 
    2028  } 
     
    3139      foreach($this->columns as $_array) 
    3240      { 
    33         list($type, $name) = $_array; 
     41        list($type, $field, $name) = $_array; 
    3442        $this->$name = $rs->{'get'.ucfirst($type)}($startcol); 
    3543        $startcol++; 
     
    6472  } 
    6573 
     74  public function addSelectColumns(Criteria &$c) 
     75  { 
     76    foreach ($this->columns as $index => $column) 
     77    { 
     78      $c->addAsColumn($column[2], $column[1]); 
     79    } 
     80  } 
     81 
    6682  public function getPeer() 
    6783  { 
    6884    return null; 
     85  } 
     86 
     87  public function getColumns() 
     88  { 
     89    return $this->columns; 
    6990  } 
    7091 
  • plugins/sfPropelImpersonatorPlugin/trunk/lib/sfPropelObjectPeerImpersonator.class.php

    r7559 r7566  
    3636    $query, 
    3737    $connection, 
    38     $objects, 
    39     $objectsParameters, 
    40     $objectsStartColumns, 
     38    $objects = array(), 
     39    $objectsParameters = array(), 
     40    $objectsStartColumns = array(), 
     41    $currentStartColumnForPropelObjects = 1, 
     42    $currentStartColumnForImpersonatedObjects = 0, 
    4143    $classToIndex, 
    4244    $relations, 
     
    5658  { 
    5759    $args = func_get_args(); 
    58     $this->objects = array(); 
    59     $this->objectsParameters = array(); 
    6060 
    6161    if (count($args)) 
     
    109109    foreach($this->objects as $object) 
    110110    { 
    111       $peer = get_class($object).'Peer'; 
    112  
    113       call_user_func(array($peer, 'addSelectColumns'), $c); 
     111      if (self::isPropelObject($object)) 
     112      { 
     113        call_user_func(array(get_class($object).'Peer', 'addSelectColumns'), $c); 
     114      } 
     115      else 
     116      { 
     117        $object->addSelectColumns($c); 
     118      } 
    114119    } 
    115120  } 
     
    310315  { 
    311316    $this->initializeRelations(); 
    312     $this->objectsStartColumns = array(); 
    313  
     317 
     318    // debug block 
    314319    if (self::DEBUG && self::DEBUG_POPULATE) 
    315320    { 
     
    344349      } 
    345350 
    346       $startcol = 1; 
    347351      $rowObjects = array(); 
     352      $currentImpersonatedObjectsStartColumn = 0; 
    348353 
    349354      // for each subcomponent we have to hydrate in each record.... 
     
    356361 
    357362        $rowObjects[$index] = clone $object; 
    358         $startcol = $rowObjects[$index]->hydrate($resultset, $startcol); 
     363 
     364        if (self::isPropelObject($object)) 
     365        { 
     366          $rowObjects[$index]->hydrate($resultset, $this->objectsStartColumns[$index]); 
     367        } 
     368        else 
     369        { 
     370          $rowObjects[$index]->hydrate($resultset, $this->currentStartColumnForPropelObjects + $currentImpersonatedObjectsStartColumn); 
     371          $currentImpersonatedObjectsStartColumn += $this->objectsStartColumns[$index]; 
     372        } 
    359373 
    360374       /* // if the object was only made of null values, we consider it's inconsistent and forget it. 
     
    381395        $isNewObject = true; 
    382396 
    383         if (get_class($this->objects[$index])!='sfPropelObjectImpersonator'
     397        if (self::isPropelObject($this->objects[$index])
    384398        { 
    385399          foreach ($allObjects[$index] as $otherObject) 
     
    404418        { 
    405419          // normal relation fetching (ie Propel BaseObjcet children) 
    406           if (get_class($this->objects[$index])!='sfPropelObjectImpersonator'
     420          if (self::isPropelObject($this->objects[$index])
    407421          { 
    408422            $linkedRelationsCounter = 0; 
     
    527541          { 
    528542            assert($index-1>=0); 
    529             $rowObject[$index-1]->extra = $rowObject[$index]; 
     543            $rowObjects[$index-1]->extra = $rowObjects[$index]; 
    530544          } 
    531545        } 
     
    626640  } 
    627641 
     642  static public function isPropelObject($object) 
     643  { 
     644    return $object instanceof BaseObject; 
     645  } 
     646 
    628647  /** 
    629648   * Adds an object instance to the current objects array 
     
    631650  protected function addObject($instance, $parameters=null) 
    632651  { 
    633     $this->objects[] = $instance; 
     652    $index = count($this->objects); 
     653 
     654    $this->objects[$index] = $instance; 
     655 
     656    if (self::isPropelObject($instance)) 
     657    { 
     658      $this->objectsStartColumns[$index] = $this->currentStartColumnForPropelObjects; 
     659      $this->currentStartColumnForPropelObjects += count($instance->toArray()); 
     660    } 
     661    else 
     662    { 
     663      $this->objectsStartColumns[$index] = $this->currentStartColumnForImpersonatedObjects; 
     664      $this->currentStartColumnForImpersonatedObjects += count($instance->getColumns()); 
     665    } 
    634666 
    635667    if (null!==$parameters) 
    636668    { 
    637       $this->setParameters(count($this->objects)-1, $parameters); 
     669      $this->setParameters($index, $parameters); 
    638670    } 
    639671  } 
     
    680712    else 
    681713    { 
    682       throw new Exception('sfPropelObjectPeerImpersonator::initialize(): Unknown object class given "'.$className.'"'); 
     714      throw new Exception(__CLASS__.'::initialize(): Unknown object class given "'.$className.'"'); 
    683715    } 
    684716  }