/**
  * return the value related to FieldDescription, if the associated object does no
  * exists => a temporary one is created
  *
  * @param object $object
  * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  * @param array $params
  * @return mixed
  */
 public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription, array $params = array())
 {
     if (isset($params['loop']) && $object instanceof \ArrayAccess) {
         throw new \RuntimeException('remove the loop requirement');
     }
     $value = $fieldDescription->getValue($object);
     // no value defined, check if the fieldDescription point to an association
     // if so, create an empty object instance
     // fixme: not sure this is the best place to do that
     if (!$value && $fieldDescription->getAssociationAdmin()) {
         $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
     }
     return $value;
 }
 /**
  * return the value related to FieldDescription, if the associated object does no
  * exists => a temporary one is created.
  *
  * @param object                                              $object
  * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  *
  * @return mixed
  */
 public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription)
 {
     $value = null;
     if (!$object) {
         return $value;
     }
     try {
         $value = $fieldDescription->getValue($object);
     } catch (NoValueException $e) {
         if ($fieldDescription->getAssociationAdmin()) {
             $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
         }
     }
     return $value;
 }
 /**
  * return the value related to FieldDescription, if the associated object does no
  * exists => a temporary one is created.
  *
  * @param object                    $object
  * @param FieldDescriptionInterface $fieldDescription
  * @param array                     $params
  *
  * @throws \RuntimeException
  *
  * @return mixed
  */
 public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription, array $params = array())
 {
     if (isset($params['loop']) && $object instanceof \ArrayAccess) {
         throw new \RuntimeException('remove the loop requirement');
     }
     $value = null;
     try {
         $value = $fieldDescription->getValue($object);
     } catch (NoValueException $e) {
         if ($fieldDescription->getAssociationAdmin()) {
             $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
         }
     }
     return $value;
 }
示例#4
0
 /**
  * Add a new field type into the provided FormBuilder
  *
  * @param \Symfony\Component\Form\FormBuilder $formBuilder
  * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
  * @return void
  */
 public function addField(FormBuilder $formBuilder, FieldDescriptionInterface $fieldDescription)
 {
     // There is a bug in the GraphWalker, so for now we always load related associations
     // for more information : https://github.com/symfony/symfony/pull/1056
     if ($formBuilder->getData() && in_array($fieldDescription->getType(), array(ClassMetadataInfo::ONE_TO_MANY, ClassMetadataInfo::MANY_TO_MANY, ClassMetadataInfo::MANY_TO_ONE, ClassMetadataInfo::ONE_TO_ONE))) {
         $value = $fieldDescription->getValue($formBuilder->getData());
         $infos = $fieldDescription->getAssociationMapping();
         if ($value instanceof $infos['targetEntity'] && $value instanceof \Doctrine\ORM\Proxy\Proxy) {
             $relatedId = 'get' . current($fieldDescription->getAdmin()->getModelManager()->getIdentifierFieldNames($infos['targetEntity']));
             $value->{$relatedId}();
             // force to load the lazy loading method __load in the proxy methode
         }
     }
     switch ($fieldDescription->getType()) {
         case ClassMetadataInfo::ONE_TO_MANY:
             $this->getOneToManyField($formBuilder, $fieldDescription);
             break;
         case ClassMetadataInfo::MANY_TO_MANY:
             $this->defineManyToManyField($formBuilder, $fieldDescription);
             break;
         case ClassMetadataInfo::MANY_TO_ONE:
         case ClassMetadataInfo::ONE_TO_ONE:
             $this->defineOneToOneField($formBuilder, $fieldDescription);
             break;
         default:
             list($type, $default_options) = $this->getFormTypeName($fieldDescription);
             $formBuilder->add($fieldDescription->getFieldName(), $type, array_merge($default_options, $fieldDescription->getOption('form_field_options', array())));
     }
 }