| | 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 | /** |
|---|