Development

Changeset 28018

You must first sign up to be able to contribute.

Changeset 28018

Show
Ignore:
Timestamp:
02/14/10 06:01:32 (3 years ago)
Author:
annis
Message:

added sfFormDoctrine's embedRelation method over for the time being (to experiment and log without touching Doctrine Core code)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/ahDoctrineEasyEmbeddedRelationsPlugin/trunk/lib/form/ahBaseFormDoctrine.class.php

    r27899 r28018  
    6969   
    7070  /** 
     71   * Embed a Doctrine_Collection relationship in to a form 
     72   * 
     73   *     [php] 
     74   *     $userForm = new UserForm($user); 
     75   *     $userForm->embedRelation('Groups AS groups'); 
     76   * 
     77   * @param  string $relationName  The name of the relation and an optional alias 
     78   * @param  string $formClass     The name of the form class to use 
     79   * @param  array  $formArguments Arguments to pass to the constructor (related object will be shifted onto the front) 
     80   * 
     81   * @throws InvalidArgumentException If the relationship is not a collection 
     82   */ 
     83  public function embedRelation($relationName, $formClass = null, $formArgs = array()) 
     84  { 
     85    if (false !== $pos = stripos($relationName, ' as ')) 
     86    { 
     87      $fieldName = substr($relationName, $pos + 4); 
     88      $relationName = substr($relationName, 0, $pos); 
     89    } 
     90    else 
     91    { 
     92      $fieldName = $relationName; 
     93    } 
     94 
     95    $relation = $this->getObject()->getTable()->getRelation($relationName); 
     96 
     97    $r = new ReflectionClass(null === $formClass ? $relation->getClass().'Form' : $formClass); 
     98 
     99    if ($relation->isOneToOne()) 
     100    { 
     101      $relationInformation = $relation->toArray(); 
     102      $relatedPk = $relation->getLocalFieldName(); 
     103      //sfContext::getInstance()->getLogger()->info(print_r($relation->__toString(), true)); 
     104      //sfContext::getInstance()->getLogger()->info(get_class($relation)); 
     105      if (get_class($relation) == 'Doctrine_Relation_LocalKey') 
     106      { 
     107        //$relatedObject = $relation->fetchRelatedFor($this->getObject()); 
     108        $relatedObject = $relation->getTable()->find($this->object[$relation->getLocalColumnName()]); 
     109         
     110        //sfContext::getInstance()->getLogger()->info(print_r($this->getObject()->$relationName->toArray(false), true)); 
     111        sfContext::getInstance()->getLogger()->info($relation->getLocalColumnName()); 
     112        sfContext::getInstance()->getLogger()->info(print_r($this->object->toArray(), true)); 
     113        $this->embedForm($fieldName, $r->newInstanceArgs(array_merge(array($relatedObject), $formArgs))); 
     114      } else 
     115      { 
     116        $this->embedForm($fieldName, $r->newInstanceArgs(array_merge(array($this->getObject()->$relationName), $formArgs))); 
     117      } 
     118    } 
     119    else 
     120    { 
     121      $subForm = new sfForm(); 
     122 
     123      foreach ($this->getObject()->$relationName as $index => $childObject) 
     124      { 
     125        $form = $r->newInstanceArgs(array_merge(array($childObject), $formArgs)); 
     126 
     127        $subForm->embedForm($index, $form); 
     128        $subForm->getWidgetSchema()->setLabel($index, (string) $childObject); 
     129      } 
     130 
     131      $this->embedForm($fieldName, $subForm); 
     132    } 
     133  } 
     134   
     135  /** 
    71136   * Here we just drop the embedded creation forms if no value has been provided for them (this simulates a non-required embedded form), 
    72137   * please provide the fields for the related embedded form in the call to $this->embedRelations() so we don't throw validation errors