public function preBind(FormEvent $event)
 {
     $form = $event->getForm();
     $propertyName = $form->getName();
     $obj = $form->getParent()->getData();
     //For oneToMany at SonataAdmin
     if (!$obj) {
         return;
     }
     $mapping = $this->mappingFactory->getMappingFromField($obj, $this->dataStorage->getReflectionClass($obj), $propertyName);
     if ($mapping) {
         /*        $form->add('file', 'file', array('required' => false))
                   ->add('delete', 'checkbox', array('required' => false));*/
         $this->transformer->setMapping($mapping, $mapping->getFileUploadPropertyName() == $propertyName ? FileDataTransformer::MODE_UPLOAD_FIELD : FileDataTransformer::MODE_FILEDATA_FIELD);
     }
 }
예제 #2
0
 /**
  * Creates the property mapping from the read annotation and configured mapping.
  *
  * @param  object                                          $obj   The object.
  * @param  \Iphp\FileStoreBundle\Mapping\Annotation\UploadableField $field The read annotation.
  * @return \Iphp\FileStoreBundle\Mapping\PropertyMapping     The property mapping.
  * @throws \InvalidArgumentException
  */
 protected function createMapping($obj, UploadableField $field)
 {
     $class = $this->dataStorage->getReflectionClass($obj);
     if (!array_key_exists($field->getMapping(), $this->mappings)) {
         throw new \InvalidArgumentException(sprintf('No mapping named "%s" configured.', $field->getMapping()));
     }
     $config = $this->mappings[$field->getMapping()];
     $mapping = new PropertyMapping($obj, $config, $this->container);
     $mapping->setProperty($class->getProperty($field->getPropertyName()));
     $mapping->setFileNameProperty($class->getProperty($field->getFileNameProperty()));
     $mapping->setMappingName($field->getMapping());
     return $mapping;
 }
 /**
  * Checks for for file to upload and store it for store at postFlush event
  *
  * @param \Doctrine\Common\EventArgs $args The event arguments.
  */
 public function prePersist(EventArgs $args)
 {
     $obj = $this->dataStorage->getObjectFromArgs($args);
     $mappings = $this->mappingFactory->getMappingsFromObject($obj, $this->dataStorage->getReflectionClass($obj));
     $curFiles = new \SplObjectStorage();
     foreach ($mappings as $mapping) {
         $file = $mapping->getFileUploadPropertyValue();
         if ($file instanceof File) {
             $curFiles[$mapping] = $file;
         }
         $mapping->setFileUploadPropertyValue(null);
     }
     //if ($curFiles) $this->deferredFiles [$mappings[0]->getObj()] = $curFiles;
     if (count($curFiles)) {
         $this->deferredFiles[$obj] = $curFiles;
     }
 }