Symfony2,编辑表格,上传图片 [英] Symfony2, Edit Form, Upload Picture

查看:242
本文介绍了Symfony2,编辑表格,上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im新的这个论坛和symfony。

问题:

我有一个问题问题在我的编辑窗体。创建窗体工作正常!我有我的项目的编辑表单。当我改变一些领域,如标题,然后提交。图片消失,因为我没有选择一个....

我必须每次选择我的当前图片,因为它不是预选。

我需要:
1.我需要在文件上传按钮上方预览当前图片。
2.当我更改我的编辑窗体的数据时,预览图片不应该改变!



有没有办法实现这个功能?我需要你的帮助,thx:)

My Form看起来像这样。

 <$ c $($)$ $  -  $ gt; $(&';'); $('''''''') b $ b'label'=>'Titel',
'class'=>'input-xxlarge'
)))
- > add('short','text ',array('attr'=> array(
'label'=>'Beschreibung',
'class'=>'input-xxlarge'
)))
- > add('content','text',array('attr'=> array(
'label'=>'Inhalt',
'class'=>' input-xxlarge'
)))
- > add('category','choice',array('choices'=>
array('Tuning'=>'Tuning ','嘎'''>'Gas','Reparatur'=>'Reparatur'),'required'=> true),array('attr'=> array(
'label'=> 'Kategorie',
'class'=> 'input-xxlarge'
)))
- > add('active','choice',array(
'choices'=> array('0'=>' Nein','1'=>'Ja'),
'preferred_choices'=>数组('Nein'),
'attr'=> array(
'label' ='aktivieren',
'class'=>'input-small'
)))
- > add('picture',NULL,array('label'=' >'bild','data_class'=> null,'required'=> false,
))
//需要我在这里解决...

}

编辑

这里是我的实体

  / ** 
* @ ORM\OneToMany(targetEntity =Pictures,mappedBy =project)
* /
保护$图片;

public function __construct(){
$ this-> pictures = new ArrayCollection();
$ this-> created = new \DateTime();

$ b $ **
* @var整数
*
* @ ORM\Column(name =id,type =integer )
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =AUTO)
* /
private $ id;
$ b / **
* @var字符串
*
* @ ORM\Column(name =title,type =string,length = 255 )
* /
private $ title;
$ b $ **
* @var字符串
*
* @ ORM \Column(name =short,type =text)
* /
私人$ short;
$ b $ **
* @var字符串
*
* @ ORM \Column(name =content,type =text)
* /
private $ content;
$ b / **
* @var字符串
*
* @ ORM\Column(name =category,type =string,length = 255 )
* /
私人$ category;
$ b / **
* @var字符串
*
* @ Assert\File(maxSize =1024k,mimeTypesMessage =请上传一张有效的图片 )
* @ ORM \Column(name =picture,type =string,length = 255)
* /
private $ picture;
$ b $ **
* @var整数
*
* @ ORM \Column(name =active,type =smallint)
* /
private $ active;
$ b / **
* @var \DateTime
*
* @ ORM\Column(name =created,type =datetime)
* /
私人$创建;
$ b $ **
*获取id
*
* @return integer
* /
public function getId(){
返回$ this-> id;

$ b $ **
*设置标题
*
* @param字符串$标题
* @return项目
* /
public function setTitle($ title){
$ this-> title = $ title;

返回$ this;

$ b / **
*获取标题
*
* @return string
* /
public function getTitle ){
return $ this-> title;

$ b $ **
*设置简称
*
* @param string $ short
* @return Project
* /
public function setShort($ short){
$ this-> short = $ short;

返回$ this;

$ b $ **


$ return b
$ b $ public function getShort( ){
return $ this-> short;

$ b $ **
*设置内容
*
* @param string $ content
* @return Project
* /
public function setContent($ content){
$ this-> content = $ content;

返回$ this;

$ b / **
*获取内容
*
* @return字符串
* /
public function getContent ){
返回$ this->内容;

$ b $ **
*设置类别
*
* @param字符串$类别
* @return项目
* /
public function setCategory($ category){
$ this-> category = $ category;

返回$ this;

$ b $ **
获取类别
*
* @return string
* /
public function getCategory( ){
返回$ this->类别;


/ **
*设置图片
*
* @param字符串$图片
* @return项目
* /
public function setPicture($ picture){
$ this-> picture = $ picture;

返回$ this;

$ b $ **
获取图片
*
* @return string
* /
public function getPicture ){
返回$ this->图片;

$ b $ **
*设置活动
*
* @param整数$活动
* @return项目
* /
public function setActive($ active){
$ this-> active = $ active;

返回$ this;

$ b / **
*获取活动
*
* @return整数
* /
public function getActive ){
返回$ this-> active;

$ b / **
*创建
*
* @param \DateTime $创建
* @return项目
* /
public function setCreated($ created){
$ this-> created = $ created;

返回$ this;

$ b / **
*创建
*
* @return \DateTime
* /
public function getCreated(){
return $ this-> created;


$ b / **
*添加图片
*
* @param \pspiess\ContentBundle\Entity\图片$图片
* @return项目
* /
公共功能addPicture(\pspiess\ContentBundle\Entity\Pictures $ pictures)
{
$ this->图片[] = $图片;
$ pictures-> setProject($ this);
返回$ this;

$ b $ **
*删除图片
*
* @param \pspiess\ContentBundle\Entity\Pictures $ pictures
* /
public function removePicture(\pspiess\ContentBundle\Entity\Pictures $ pictures)
{
$ this-> pictures-> removeElement($ pictures );
}

/ **
*获取图片
*
* @return \Doctrine\Common\Collections\Collection
* /
public function getPictures()
{
return $ this-> pictures;

$ b / **
*重写toString()方法返回项目标题的名称
* @return字符串标题
* /
public function __toString()
{
return $ this-> title;
}

public function getFullPicturePath(){
return null === $ this-> picture? null:$ this-> getUploadRootDir()。 $这 - >图片;


protected function getUploadRootDir(){
//上传文档的绝对目录路径
return $ this-> getTmpUploadRootDir() $ this-> getId()。 /;


保护函数getTmpUploadRootDir(){
//保存上传文档的绝对目录路径
return __DIR__。 /../../../../web/resources/images/project/;

$ b / **
* @ ORM\PrePersist()
* @ ORM\PreUpdate()
* /
public function uploadPicture(){
echo $ this-> picture;
//如果字段不是必需的,则file属性可以为空
if(null === $ this-> picture){
return; ($!$ this $> id){
$ this-> picture-> move($ this-> getTmpUploadRootDir(),$ this->图片(
) - > getClientOriginalName());
} else {
$ this-> picture-> move($ this-> getUploadRootDir(),$ this-> picture-> getClientOriginalName());
}
$ this-> setPicture($ this-> picture-> getClientOriginalName());

$ b $ ** b $ b * @ ORM\PostPersist()
* /
public function movePicture(){
if( null === $ this-> picture){
return;

if(!is_dir($ this-> getUploadRootDir())){
mkdir($ this-> getUploadRootDir());
}
copy($ this-> getTmpUploadRootDir()。$ this-> picture,$ this-> getFullPicturePath());
unlink($ this-> getTmpUploadRootDir()。$ this-> picture);

$ b $ ** b $ b * @ ORM\PreRemove()
* /
public function deletePicture(){
if( file_exists($ this-> getFullPicturePath())){
unlink($ this-> getFullPicturePath());

if(is_dir($ this-> getUploadRootDir())){
// rmdir($ this-> getUploadRootDir());



$ div $解析方案

应该检查出 http://symfony.com/doc/current/cookbook/doctrine/ file_uploads.html ,它看起来像你没有添加任何东西,所以它是值得你看看。



您的实体丢失一些需要的方法和属性,看起来你的表单定义也不是很正确。这些文档很好的解释了初学者的东西,所以只要确保你给他们看就可以了。


im new to this forum and to symfony. After hours of searching I don't found I solution for my problem.

Problem:

I have a problem in my edit form. The create Form works fine! I have an edit form for my projects. When I change some fields, like the title and then submit. The picture disappears, because I haven't pick one....

I have to select my current picture every time, because it is not pre selected.

What I need: 1. I need a preview of my current picture above the file upload button. 2. When I change data of my edit form, the preview picture shouldn't change!

Is there a way I can achieve this? I need your help, thx :)

My Form looks like this.

    public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('title', 'text', array('attr' => array(
                    'label' => 'Titel',
                    'class' => 'input-xxlarge'
        )))
            ->add('short', 'text', array('attr' => array(
                    'label' => 'Beschreibung',
                    'class' => 'input-xxlarge'
        )))
            ->add('content', 'text', array('attr' => array(
                    'label' => 'Inhalt',
                    'class' => 'input-xxlarge'
        )))
            ->add('category', 'choice', array('choices' =>
                array('Tuning' => 'Tuning', 'Gas' => 'Gas', 'Reparatur' => 'Reparatur'), 'required' => true), array('attr' => array(
                    'label' => 'Kategorie',
                    'class' => 'input-xxlarge'
        )))
            ->add('active', 'choice', array(
                'choices' => array('0' => 'Nein', '1' => 'Ja'),
                'preferred_choices' => array('Nein'),
                'attr' => array(
                    'label' => 'aktivieren',
                    'class' => 'input-small'
        )))
            ->add('picture', NULL, array('label' => 'Bild', 'data_class' => null, 'required' => false,
            ))
  //Need i workaround here...

}

EDIT

here is my entity

 /**
 * @ORM\OneToMany(targetEntity="Pictures", mappedBy="project")
 */
protected $pictures;

public function __construct() {
    $this->pictures = new ArrayCollection();
    $this->created = new \DateTime();
}

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="title", type="string", length=255)
 */
private $title;

/**
 * @var string
 *
 * @ORM\Column(name="short", type="text")
 */
private $short;

/**
 * @var string
 *
 * @ORM\Column(name="content", type="text")
 */
private $content;

/**
 * @var string
 *
 * @ORM\Column(name="category", type="string", length=255)
 */
private $category;

/**
 * @var string
 *
 * @Assert\File(maxSize = "1024k", mimeTypesMessage = "Please upload a valid Picture")
 * @ORM\Column(name="picture", type="string", length=255)
 */
private $picture;

/**
 * @var integer
 *
 * @ORM\Column(name="active", type="smallint")
 */
private $active;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="created", type="datetime")
 */
private $created;

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

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

    return $this;
}

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

/**
 * Set short
 *
 * @param string $short
 * @return Project
 */
public function setShort($short) {
    $this->short = $short;

    return $this;
}

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

/**
 * Set content
 *
 * @param string $content
 * @return Project
 */
public function setContent($content) {
    $this->content = $content;

    return $this;
}

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

/**
 * Set category
 *
 * @param string $category
 * @return Project
 */
public function setCategory($category) {
    $this->category = $category;

    return $this;
}

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

/**
 * Set picture
 *
 * @param string $picture
 * @return Project
 */
public function setPicture($picture) {
    $this->picture = $picture;

    return $this;
}

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

/**
 * Set active
 *
 * @param integer $active
 * @return Project
 */
public function setActive($active) {
    $this->active = $active;

    return $this;
}

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

/**
 * Set created
 *
 * @param \DateTime $created
 * @return Project
 */
public function setCreated($created) {
    $this->created = $created;

    return $this;
}

/**
 * Get created
 *
 * @return \DateTime 
 */
public function getCreated() {
    return $this->created;
}


/**
 * Add pictures
 *
 * @param \pspiess\ContentBundle\Entity\Pictures $pictures
 * @return Project
 */
public function addPicture(\pspiess\ContentBundle\Entity\Pictures $pictures)
{
    $this->pictures[] = $pictures;
    $pictures->setProject($this);
    return $this;
}

/**
 * Remove pictures
 *
 * @param \pspiess\ContentBundle\Entity\Pictures $pictures
 */
public function removePicture(\pspiess\ContentBundle\Entity\Pictures $pictures)
{
    $this->pictures->removeElement($pictures);
}

/**
 * Get pictures
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getPictures()
{
    return $this->pictures;
}

/**
 * Override toString() method to return the name of the project title
 * @return string title
 */
public function __toString()
{
    return $this->title;
}

public function getFullPicturePath() {
    return null === $this->picture ? null : $this->getUploadRootDir() . $this->picture;
}

protected function getUploadRootDir() {
    // the absolute directory path where uploaded documents should be saved
    return $this->getTmpUploadRootDir() . $this->getId() . "/";
}

protected function getTmpUploadRootDir() {
    // the absolute directory path where uploaded documents should be saved
    return __DIR__ . '/../../../../web/resources/images/project/';
}

/**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
public function uploadPicture() {
    echo $this->picture;
    // the file property can be empty if the field is not required
    if (null === $this->picture) {
        return;
    }
    if (!$this->id) {
        $this->picture->move($this->getTmpUploadRootDir(), $this->picture->getClientOriginalName());
    } else {
        $this->picture->move($this->getUploadRootDir(), $this->picture->getClientOriginalName());
    }
    $this->setPicture($this->picture->getClientOriginalName());
}

/**
 * @ORM\PostPersist()
 */
public function movePicture() {
    if (null === $this->picture) {
        return;
    }
    if (!is_dir($this->getUploadRootDir())) {
        mkdir($this->getUploadRootDir());
    }
    copy($this->getTmpUploadRootDir() . $this->picture, $this->getFullPicturePath());
    unlink($this->getTmpUploadRootDir() . $this->picture);
}

/**
 * @ORM\PreRemove()
 */
public function deletePicture() {
    if (file_exists($this->getFullPicturePath())) {
        unlink($this->getFullPicturePath());
    }
    if (is_dir($this->getUploadRootDir())) {
        //rmdir($this->getUploadRootDir());
    }
}

解决方案

You should check out http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html it looks like you've not added anything from in there, so it'd be worth you having a look.

Your entity is missing some of the methods and properties needed, and it looks like your form definition is not quite right yet either. The docs are very good at explaining the beginner stuff so just make sure you give them a look over.

这篇关于Symfony2,编辑表格,上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆