首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Sonata Admin和Doctirne上载文件错误

使用Sonata Admin和Doctirne上载文件错误
EN

Stack Overflow用户
提问于 2016-02-20 18:25:46
回答 2查看 492关注 0票数 1

我遵循关于处理原则上传和奏鸣曲管理的指南。

我只使用id、path和文件(图像)设置实体:

代码语言:javascript
复制
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * @ORM\Entity
 * @ORM\Table(name="viaggio_perfetto")
*/
class ViaggioPerfetto
{
    private $temp;

    /**
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue(strategy="AUTO")
    * @ORM\Id
    */
    protected $id;


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

    /**
     * @Assert\File(maxSize="6000000")
     */
    protected $file;

    /**
     * Get file.
     *
     * @return UploadedFile
     */
    public function getFile()
    {
        return $this->file;
    }

    /**
     * Sets file.
     *
     * @param UploadedFile $file
     */
    public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;
        // check if we have an old image path
        if (isset($this->path)) {
            // store the old name to delete after the update
            $this->temp = $this->path;
            $this->path = null;
        } else {
            $this->path = 'initial';
        }
    }

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->getFile()) {
            // do whatever you want to generate a unique name
            $filename = sha1(uniqid(mt_rand(), true));
            $this->path = $filename.'.'.$this->getFile()->guessExtension();
        }
    }

    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null === $this->getFile()) {
            return;
        }

        // if there is an error when moving the file, an exception will
        // be automatically thrown by move(). This will properly prevent
        // the entity from being persisted to the database on error
        $this->getFile()->move($this->getUploadRootDir(), $this->path);

        // check if we have an old image
        if (isset($this->temp)) {
            // delete the old image
            unlink($this->getUploadRootDir().'/'.$this->temp);
            // clear the temp image path
            $this->temp = null;
        }
        $this->file = null;
    }

    /**
     * @ORM\PostRemove()
     */
    public function removeUpload()
    {
        $file = $this->getAbsolutePath();
        if ($file) {
            unlink($file);
        }
    }

    public function getAbsolutePath()
    {
        return null === $this->path
            ? null
            : $this->getUploadRootDir().'/'.$this->path;
    }

    public function getWebPath()
    {
        return null === $this->path
            ? null
            : $this->getUploadDir().'/'.$this->path;
    }

    protected function getUploadRootDir()
    {
        // the absolute directory path where uploaded
        // documents should be saved
        return __DIR__.'/../../../../web/'.$this->getUploadDir();
    }

    protected function getUploadDir()
    {
        // get rid of the __DIR__ so it doesn't screw up
        // when displaying uploaded doc/image in the view.
        return 'uploads/documents';
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }


    /**
     * Set path
     *
     * @param string $path
     *
     * @return ViaggioPerfetto
     */
    public function setPath($path)
    {
        $this->path = $path;

        return $this;
    }

    /**
     * Get path
     *
     * @return string
     */
    public function getPath()
    {
        return $this->path;
    }
}

我删除了一些文件,以便更好地阅读实体。

对于这个实体,我遵循了symfony食谱。

网页/上传下的文件夹有777个权限。

然后我添加到奏鸣曲管理员:

代码语言:javascript
复制
protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper->add('title', 'text');
    $formMapper->add('text', 'text');
    $formMapper->add('startDate', 'date');
    $formMapper->add('endDate', 'date');
    $formMapper->add('active', 'checkbox', array('required' => false));
    $formMapper->add('file', 'file');
}

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper->add('text');
}

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper->addIdentifier('title');
    $listMapper->add('text');
}

现在我登录奏鸣曲管理页面,并尝试创建新的实体。

所有工作的权利,我在我的数据库所有字段的权利,除了文件。

web/上载/文档下没有文件,path中也没有路径在数据库中存档(如果没有设置路径,我在SetFile函数中设置了“初始”)。

我不知道我哪里错了。谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-20 19:01:16

问题是,您从不调用实体的upload方法。

将以下方法添加到管理类(奏鸣曲):

代码语言:javascript
复制
/**
 * Handles file upload on create.
 *
 * @param object $created The newly created entity
 */
public function prePersist($created)
{
    $created->upload();
}

/**
 * Handles file upload on update.
 *
 * @param object $updated The updated entity
 */
public function preUpdate($updated)
{
    $updated->upload();
}

而且它应该能工作。

这将在持久化/更新实体之前调用实体的upload方法。

参见奏鸣曲文件上传配方的相应部分。

票数 1
EN

Stack Overflow用户

发布于 2017-01-20 16:58:24

您必须添加

代码语言:javascript
复制
* @ORM\HasLifecycleCallbacks

堵住

代码语言:javascript
复制
/**
 * @ORM\Entity
 * @ORM\Table(name="viaggio_perfetto")
*/

供使用

代码语言:javascript
复制
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35527255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档