Synfony 2 文件上传器与 Doctrine 2

发布于 2024-12-01 23:35:30 字数 4718 浏览 1 评论 0原文

我想用 Symfony 2 和 Doctrine 2 制作一个简单的文件上传器。 我已经按照这个教程进行操作: http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html 还有这个: http://leny-bernard .com/fr/afficher/article/creer-un-site-facilement-en-symfony2-partie-4

这是我的实体类:

namespace Luna\KBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;

/**
* Luna\KBundle\Entity\Media
*
* @ORM\Entity
*/
class Media
{
/**
 * @var integer $id
 */
private $id;

/**
 * @var string $title
 */
private $title;

/**
 * @var text $description
 */
private $description;

/**
 * @var string $author
 */
private $author;

/**
 * @var string $source
 */
private $source;

/**
 * @Assert\File(maxSize="6000000")
 */
private $paths;

/**
 * @var string $type
 */
private $type;

/**
 * @var Luna\KBundle\Entity\object
 */
private $idobject;
/***********************************METHODS***********************************/

/**
 * Set idobject
 *
 * @param Luna\KBundle\Entity\Object $idobject
 */
public function setIdobject(\Luna\KBundle\Entity\object $idobject)
{
    $this->idObject = $idObject;
}

/**
 * Get idObject
 *
 * @return Luna\KBundle\Entity\Object 
 */
public function getIdObject()
{
    return $this->idObject;
}

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

/**
 * Set title
 *
 * @param string $title
 */
public function setTitle($title)
{
    $this->title = $title;
}

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

/**
 * Set description
 *
 * @param text $description
 */
public function setDescription($description)
{
    $this->description = $description;
}

/**
 * Get description
 *
 * @return text 
 */
public function getDescription()
{
    return $this->description;
}

/**
 * Set author
 *
 * @param string $author
 */
public function setAuthor($author)
{
    $this->author = $author;
}

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

/**
 * Set source
 *
 * @param string $source
 */
public function setSource($source)
{
    $this->source = $source;
}

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

/**
 * Set paths
 *
 * @param string $paths
 */
public function setPaths($paths)
{
    $this->paths = $paths;
}

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

/**
 * Set type
 *
 * @param string $type
 */
public function setType($type)
{
    $this->type = $type;
}

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


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

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

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 when displaying uploaded doc/image in the view.
    return 'uploads/mediaobject';
}

事实

是 @Assert\File(maxSize="6000000") 不起作用:我没有文件上传器,只有一个简单的文本字段?!

我怎样才能使它正常工作?

问候大家:)

编辑:这是我的表单生成器

namespace Luna\KBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class MediaInit extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
    $builder
        ->add('title')
        ->add('description')
        ->add('author')
        ->add('source')
        ->add('paths')
        ->add('type')
        ->add('idObject')            
    ;
}

是我的树枝模板:

{% extends '::layout.html.twig' %}
{####################################### MEDIA INIT###########################}
{% block content %}


<h1>Creer un Media</h1>

Entrez les informations de votre media ici

<form action="{{ path('media_init') }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<p>
    <button type="submit">Creer</button>
</p>
</form>


{% endblock %}

I want to make a simple file uploader with Symfony 2 and Doctrine 2.
I've follow this tutorial :
http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html
and this one :
http://leny-bernard.com/fr/afficher/article/creer-un-site-facilement-en-symfony2-partie-4

Here is my Entity class :

namespace Luna\KBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;

/**
* Luna\KBundle\Entity\Media
*
* @ORM\Entity
*/
class Media
{
/**
 * @var integer $id
 */
private $id;

/**
 * @var string $title
 */
private $title;

/**
 * @var text $description
 */
private $description;

/**
 * @var string $author
 */
private $author;

/**
 * @var string $source
 */
private $source;

/**
 * @Assert\File(maxSize="6000000")
 */
private $paths;

/**
 * @var string $type
 */
private $type;

/**
 * @var Luna\KBundle\Entity\object
 */
private $idobject;
/***********************************METHODS***********************************/

/**
 * Set idobject
 *
 * @param Luna\KBundle\Entity\Object $idobject
 */
public function setIdobject(\Luna\KBundle\Entity\object $idobject)
{
    $this->idObject = $idObject;
}

/**
 * Get idObject
 *
 * @return Luna\KBundle\Entity\Object 
 */
public function getIdObject()
{
    return $this->idObject;
}

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

/**
 * Set title
 *
 * @param string $title
 */
public function setTitle($title)
{
    $this->title = $title;
}

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

/**
 * Set description
 *
 * @param text $description
 */
public function setDescription($description)
{
    $this->description = $description;
}

/**
 * Get description
 *
 * @return text 
 */
public function getDescription()
{
    return $this->description;
}

/**
 * Set author
 *
 * @param string $author
 */
public function setAuthor($author)
{
    $this->author = $author;
}

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

/**
 * Set source
 *
 * @param string $source
 */
public function setSource($source)
{
    $this->source = $source;
}

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

/**
 * Set paths
 *
 * @param string $paths
 */
public function setPaths($paths)
{
    $this->paths = $paths;
}

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

/**
 * Set type
 *
 * @param string $type
 */
public function setType($type)
{
    $this->type = $type;
}

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


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

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

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 when displaying uploaded doc/image in the view.
    return 'uploads/mediaobject';
}

}

The fact is that @Assert\File(maxSize="6000000") is not working : I don't have a file uploader but just a simple texte field ?!

How can I make this works correctly ?

Regards Guys :)

EDIT : Here my Form builder

namespace Luna\KBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class MediaInit extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
    $builder
        ->add('title')
        ->add('description')
        ->add('author')
        ->add('source')
        ->add('paths')
        ->add('type')
        ->add('idObject')            
    ;
}

}

And Here my twig template :

{% extends '::layout.html.twig' %}
{####################################### MEDIA INIT###########################}
{% block content %}


<h1>Creer un Media</h1>

Entrez les informations de votre media ici

<form action="{{ path('media_init') }}" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<p>
    <button type="submit">Creer</button>
</p>
</form>


{% endblock %}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美煞众生 2024-12-08 23:35:30

当您想上传文件时,您需要声明路径字段和路径字段。虚拟文件字段。所以,你的类需要看起来像:

class Media
{
    private $path;

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

和你的形式:

class MediaInit extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('file');
    }
}

When you want to upload a file you need to declare a path field & a virtual file field. So, your class needs to look like :

class Media
{
    private $path;

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

And your form:

class MediaInit extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('file');
    }
}
岁月无声 2024-12-08 23:35:30

MediaInit 类中的 $builder 变量似乎未正确初始化(使用“Media”类型)。

It seems that $builder variable in MediaInit class is not initialized properly (with "Media" type).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文