Development

Changeset 7555

You must first sign up to be able to contribute.

Changeset 7555

Show
Ignore:
Timestamp:
02/20/08 16:33:33 (9 months ago)
Author:
hartym
Message:

sfPropelImpersonatorPlugin : added support for init/set/addXxx(s)RelatedBySomethingElseId? use with an optional related_by= parameter appendable to propel class names in ObjectPeerImpersonator? constructor.

Files:

Legend:

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

    r7392 r7555  
    3737    $connection, 
    3838    $objects, 
     39    $objectsParameters, 
    3940    $classToIndex, 
    4041    $relations, 
     
    5556    $args = func_get_args(); 
    5657    $this->objects = array(); 
     58    $this->objectsParameters = array(); 
    5759 
    5860    if (count($args)) 
     
    349351        $startcol = $rowObjects[$index]->hydrate($resultset, $startcol); 
    350352 
    351         // if the object was only made of null values, we consider it's inconsistent and forget it. 
    352 /*        if (!$this->testConsistence($rowObjects[$index]->getPrimaryKey())) 
     353       /* // if the object was only made of null values, we consider it's inconsistent and forget it. 
     354        if (!$this->testConsistence($rowObjects[$index]->getPrimaryKey())) 
    353355        { 
    354356          unset($rowObjects[$index]); 
    355357          continue; 
    356358        } 
    357         for now, let's say we keep it 
     359       /* for now, let's say we keep it 
    358360 
    359361          reason is: if we dont populate an empty propel object, propel will think we don't know it does not exists, and 
     
    413415                $foreignClass = $relation['classFrom']; 
    414416                $foreignObject = $rowObjects[$this->getIndexByClass($foreignClass)]; 
     417                $relatedBy = (null === ($_relatedBy=$this->getParameter($index, 'related_by')))?'':'RelatedBy'.sfInflector::camelize($_relatedBy); 
    415418 
    416419                if ($isNewObject) 
     
    421424                  } 
    422425 
    423                   $currentObject->{'init'.$foreignClass.'s'}(); 
     426 
     427                  $currentObject->{'init'.$foreignClass.'s'.$relatedBy}(); 
    424428                } 
    425429 
    426                 $currentObject->{'add'.$foreignClass}($foreignObject); 
    427                 $foreignObject->{'set'.$relation['classTo']}($currentObject); 
     430                $currentObject->{'add'.$foreignClass.$relatedBy}($foreignObject); 
     431                $foreignObject->{'set'.$relation['classTo'].$relatedBy}($currentObject); 
    428432 
    429433                $linkedRelationsCounter++; 
     
    473477                  $foreignObject = $rowObjects[$classToIndex]; 
    474478 
     479                  $relatedBy = (null === ($_relatedBy=$this->getParameter($classToIndex, 'related_by')))?'':'RelatedBy'.sfInflector::camelize($_relatedBy); 
     480                  // @todo: bug correction, this does not work if related_by is used this way, it tries to call setFieldRelatedByFieldId 
     481                  // instead of setClassNameRelatedByFieldId, and sometimes (depending on order) it uses the wrong Field, as second one 
     482                  // override first one in relations 
     483 
    475484                  // local *---- foreign (local object has one foreign object) 
    476                   $rowObjects[$index]->{'set'.str_replace('Id','',$relation['local'])}($foreignObject); 
     485                  $rowObjects[$index]->{'set'.str_replace('Id','',$relation['local']).$relatedBy}($foreignObject); 
    477486 
    478487                  // foreign ----* local (foreign object has many local objects) 
     
    480489                  // @todo: find a way not to call initXxxXxxs() on every passes, but only on first addition for each object. 
    481490 
    482                   $foreignObject->{'init'.$relation['classFrom'].'s'}($rowObjects[$index]); 
    483                   $foreignObject->{'add'.$relation['classFrom']}($rowObjects[$index]); 
     491                  $foreignObject->{'init'.$relation['classFrom'].'s'.$relatedBy}($rowObjects[$index]); 
     492                  $foreignObject->{'add'.$relation['classFrom'].$relatedBy}($rowObjects[$index]); 
    484493 
    485494                  $linkedRelationsCounter++; 
     
    527536   * 
    528537   * @param mixed $key 
     538   * 
    529539   * @return boolean 
    530540   */ 
     
    554564 
    555565  /** 
     566   * Merge a new parameters set with existing one for object indexed by $objectIndex 
     567   * 
     568   * @param integer $objectIndex 
     569   * @param array   $parameters 
     570   */ 
     571  protected function setParameters($objectIndex, array $parameters) 
     572  { 
     573    if (!isset($this->objectsParameters[$objectIndex])) 
     574    { 
     575      $this->objectsParameters[$objectIndex] = array(); 
     576    } 
     577 
     578    $this->objectsParameters[$objectIndex] = array_merge($this->objectsParameters[$objectIndex], $parameters); 
     579  } 
     580 
     581  /** 
     582   * Retrieve a parameter for an object 
     583   * 
     584   * @param mixed  $objectIndexOrName 
     585   * @param string $parameterName 
     586   * @param mixed  $defaultValue 
     587   * 
     588   * @return mixed 
     589   */ 
     590  public function getParameter($objectIndexOrName, $parameterName, $defaultValue=null) 
     591  { 
     592    if (!is_numeric($objectIndexOrName)) 
     593    { 
     594      $objectIndexOrName = $this->getIndexByClass($objectIndexOrName); 
     595    } 
     596 
     597    if (null===$objectIndexOrName || !isset($this->objects[$objectIndexOrName])) 
     598    { 
     599      throw new Exception('Tried to retrievea parameter for an unknown object name or index.'); 
     600    } 
     601 
     602    if (!isset($this->objectsParameters[$objectIndexOrName]) || !isset($this->objectsParameters[$objectIndexOrName][$parameterName])) 
     603    { 
     604      return $defaultValue; 
     605    } 
     606    else 
     607    { 
     608      return $this->objectsParameters[$objectIndexOrName][$parameterName]; 
     609    } 
     610  } 
     611 
     612  /** 
    556613   * Adds an object instance to the current objects array 
    557614   */ 
    558   protected function addObject($instance
     615  protected function addObject($instance, $parameters=null
    559616  { 
    560617    $this->objects[] = $instance; 
     618 
     619    if (null!==$parameters) 
     620    { 
     621      $this->setParameters(count($this->objects)-1, $parameters); 
     622    } 
    561623  } 
    562624 
     
    564626  protected function addObjectByClassName($className) 
    565627  { 
     628    // find whether or not we have parameters set for this object 
     629    if (false!==strpos($className, ' ')) 
     630    { 
     631      // separate classname from parameters 
     632      $_parameters = explode(' ', $className); 
     633      $className = array_shift($_parameters); 
     634 
     635      // transform parameters into useable array 
     636      $_parameters = array_map(create_function('$v', 'return explode(\'=\', $v);'), $_parameters); 
     637      $parameters = array(); 
     638 
     639      foreach ($_parameters as $parameter) 
     640      { 
     641        if (count($parameter)==2) 
     642        { 
     643          $parameters[$parameter[0]] = $parameter[1]; 
     644        } 
     645        else 
     646        { 
     647          throw new Exception('Invalid parameter given'); 
     648        } 
     649      } 
     650    } 
     651 
     652    // add object 
    566653    if (class_exists($className, true)) 
    567654    { 
    568       $this->addObject(new $className()); 
     655      if (isset($parameters)) 
     656      { 
     657        $this->addObject(new $className(), $parameters); 
     658      } 
     659      else 
     660      { 
     661        $this->addObject(new $className()); 
     662      } 
    569663    } 
    570664    else 
    571665    { 
    572       throw new Exception('sfPropelObjectPeerImpersonator::initialize: Unknown object class given "'.$className.'"'); 
     666      throw new Exception('sfPropelObjectPeerImpersonator::initialize(): Unknown object class given "'.$className.'"'); 
    573667    } 
    574668  }