Symfony多文件上载重置以前的字段

时间:2017-05-13 13:10:16

标签: php symfony doctrine event-listener

或者,如果FileTypes为空,问题是如何不更新现有字段。

我使用this article使用Doctrine Listener在实体Household中的两个字段(Eaudit和Tspec)上进行文件上传。 只有我没有使用Uploader Service,例如,upload()存在于同一个类中,而field文件存储目录路径作为服务参数传递。

问题是如果我再次提交表单,它会清除以前的字段数据 (例如,我已提交Eaudit,页面重新加载后试图提交Tspecification,并注意Eaudit为空,否则)。

尽管在postLoad()上,Form显示空的FileTypes。另外,我使用eauditName / tspecificationName字段来存储UploadedFile原始名称。

修改 注意到如果删除包含

数据的字段
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
    $household = $event->getData();
    $form = $event->getForm();
    if (null !== $household->getEaudit()) {
        $form->remove('eaudit');
    }
    if (null !== $household->getTspecification()) {
        $form->remove('tspecification');
    }
});

表单不显示form_rest(),并且在提交数据更新后所有相同的字段都在DB中,但是使用我的真实路径,例如C:\用户\管理员\ FILE.DOC

Symfony 2.8.19,请帮助解决我的问题,谢谢你的关注。

class DocumentsUploadListener {
    private $eaudit;
    private $tspec;

    public function __construct($eaudit, $tspec) {
        $this->eaudit = $eaudit;
        $this->tspec = $tspec;
    }

    public function prePersist(LifecycleEventArgs $args) {
        $entity = $args->getEntity();
        $this->uploadFile($entity);
    }

    public function preUpdate(PreUpdateEventArgs $args) {
        $entity = $args->getEntity();
        $this->uploadFile($entity);
    }

    private function uploadFile($entity) {
        if (!$entity instanceof Household) {
            return;
        }
        $file1 = $entity->getEaudit();
        // only upload new files
        if ($file1 instanceof UploadedFile) {
            $fileName1 = $this->upload($this->eaudit, $file1);
            $entity->setEaudit($fileName1);
            $entity->setEauditName($file1->getClientOriginalName());
        }

        $file2 = $entity->getTspecification();
        if ($file2 instanceof UploadedFile) {
            $fileName2 = $this->upload($this->tspec, $file2);
            $entity->setTspecification($fileName2);
            $entity->setTspecificationName($file2->getClientOriginalName());
        }
    }

    public function postLoad(LifecycleEventArgs $args) {
        $entity = $args->getEntity();
        if (!$entity instanceof Household) {
            return;
        }
        if ($fileName1 = $entity->getEaudit()) {
            $entity->setEaudit(new File($this->eaudit.'/'.$fileName1));
        }

        if ($fileName2 = $entity->getTspecification()) {
            $entity->setTspecification(new File($this->tspec.'/'.$fileName2));
        }
    }

    public function upload($dest, UploadedFile $file) {
        $fileName = md5(uniqid()).'.'.$file->guessExtension();
        $file->move($dest, $fileName);
        return $fileName;
    }
}

表格类:

class HouseholdDocumentsType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
            ->add('eaudit', FileType::class, [
                'required' => false,
            ])
            ->add('tspecification', FileType::class, [
                'required' => false,
            ])
            ->add('save', SubmitType::class)
        ;
    }

    public function configureOptions(OptionsResolver $resolver) {
        $resolver
            ->setDefaults([
                'data_class' => Household::class
            ]);
    }

    public function getBlockPrefix() {
        return 'app_bundle_household_documents_type';
    }
}

实体类:

/**
 * Household
 *
 * @ORM\Table(name="households")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\HouseholdRepository")
 */
class Household {
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", nullable=true)
     * @Assert\File(mimeTypes={"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"})
     */
    protected $eaudit;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $eauditName;

    /**
     * @ORM\Column(type="string", nullable=true)
     * @Assert\File(mimeTypes={"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"})
     */
    protected $tspecification;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $tspecificationName;
    /**
     * Get id
     *
     * @return int
     */
    public function getId() {
        return $this->id;
    }

    /**
     * @return mixed
     */
    public function getEaudit() {
        return $this->eaudit;
    }

    /**
     * @param mixed $eaudit
     */
    public function setEaudit($eaudit) {
        $this->eaudit = $eaudit;
    }

    /**
     * @param string $tspecification
     * @return Household
     */
    public function setTspecification($tspecification) {
        $this->tspecification = $tspecification;
        return $this;
    }

    /**
     * @return string
     */
    public function getTspecification() {
        return $this->tspecification;
    }

    /**
     * @param string $eauditName
     * @return Household
     */
    public function setEauditName($eauditName) {
        $this->eauditName = $eauditName;
        return $this;
    }

    /**
     * @return string
     */
    public function getEauditName() {
        return $this->eauditName;
    }

    /**
     * @param string $tspecificationName
     * @return Household
     */
    public function setTspecificationName($tspecificationName) {
        $this->tspecificationName = $tspecificationName;
        return $this;
    }

    /**
     * @return string
     */
    public function getTspecificationName() {
        return $this->tspecificationName;
    }
}

0 个答案:

没有答案