Rey*_*rPM 2 php symfony vichuploaderbundle
我正在使用此捆绑包通过表单上传文件.这就是我所做的.在我正在使用捆绑包的Twig模板中
<form action="{{ path('guardar-natural') }}" method="POST" class="form-horizontal" role="form" id="registroNatural" {{ form_enctype(form) }}>
    {{ form_widget(form.documento_rif) }}
    <button type="submit" class="btn btn-primary" id="btnEnviarRegistro">{{ 'registration.submit'|trans }}</button>
</form>
在表单类中:
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
            ->add('documento_rif', 'vich_file', array(
                'required'      => true,
                'mapping'       => 'recaudos',
                'allow_delete'  => true,
                'download_link' => true,
                'label' => false,
            ))
            ->add('usuario', new RegistrationForm());
}
在字段所在的实体中:
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
 * @Vich\Uploadable
 */
class Natural
{
    /**
     * @Vich\UploadableField(mapping="recaudos", fileNameProperty="documentoRIF")
     * @var File $imageFile
     */
    protected $imageFile;
    /**
     * @ORM\Column(type="string", length=255, name="documento_rif", nullable=true)
     * @var string $documentoRIF
     */
    protected $documentoRIF;
    /**
     * @ORM\Column(type="datetime", nullable=true)
     * @var \DateTime $updatedAt
     */
    protected $updatedAt;
    /**
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
     */
    public function setImageFile(File $image = null)
    {
        $this->imageFile = $image;
        if ($image) {
            $this->updatedAt = new \DateTime('now');
        }
    }
    /**
     * @return File
     */
    public function getImageFile()
    {
        return $this->imageFile;
    }
    /**
     * @param string $documentoRIF
     */
    public function setDocumentoRIF($documentoRIF)
    {
        $this->documentoRIF = $documentoRIF;
    }
    /**
     * @return string
     */
    public function getDocumentoRIF()
    {
        return $this->documentoRIF;
    }
}
在config.yml档案:
vich_uploader:
    db_driver: orm # or mongodb or propel or phpcr
    storage:    vich_uploader.storage.file_system
    mappings:
        recaudos:
            uri_prefix:         /recaudos
            upload_destination: %kernel.root_dir%/../web/uploads/recaudos
            namer:              vich_uploader.namer_uniqid
            delete_on_update:   true
            delete_on_remove:   true
由于我没有收到任何错误,因此启用了捆绑包,目录具有正确的权限(0755).在DB级别,当在documento_rif列中创建任何记录时,我得到此字符串/tmp/php1fbjiZ而不是文件名,我的代码有什么问题?
当然,我有更多的代码,但只是在这里写相关的部分.
小智 5
在表单类型中,您应该引用uploadable字段(imageFile)而不是filename列(documento_rif).
没有链接到您的错误但会导致问题:uri_prefix映射中定义的不正确,应该是/uploads/recaudos.
其余的应该没问题.
| 归档时间: | 
 | 
| 查看次数: | 1695 次 | 
| 最近记录: |