Symfony2,上传新图像或选择现有图像(后台)

时间:2013-10-29 11:25:16

标签: php forms symfony file-upload doctrine-orm

我是论坛的新手,所以我会尽力尊重论坛规则并明确。

我从symfony开始,我发现了表单和实体,以及它们的用途。我想显示一个表单,提示用户添加新图像或选择现有图像。

我有两个实体,一个实体“页面”和一个实体“图像”。在添加页面的形式中,我希望有一个嵌套来添加图片和一个下拉列表来选择图像表单。

经过几个小时的研究和测试后,我仍面临同样的问题,我不知道如何解决。

任何人都可以帮助我或告诉我应该如何进行吗?

谢谢。

修改

哦,对不起,这不是我的意图 Udan

以下是我的实体“page”的代码:

<?php

namespace Test\AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Pages
 *
 * @ORM\Table(name="app_pages")
 * @ORM\Entity
 */
class Pages
{
/**
 * @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=90, nullable=true)
 */
private $title;

/**
 * @var string
 *
 * @ORM\Column(name="page_title", type="string", length=90)
 */
private $pageTitle;

/**
 * @var string
 *
 * @ORM\Column(name="page_name", type="string", length=90)
 */
private $pageName;

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

/**
 * @ORM\OneToOne(targetEntity="Test\AdminBundle\Entity\Image", cascade={"persist"})
 * @ORM\JoinColumn(nullable=false)
 */
private $image;


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

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

    return $this;
}

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

/**
 * Set pageTitle
 *
 * @param string $pageTitle
 * @return Pages
 */
public function setPageTitle($pageTitle)
{
    $this->pageTitle = $pageTitle;

    return $this;
}

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

/**
 * Set pageName
 *
 * @param string $pageName
 * @return Pages
 */
public function setPageName($pageName)
{
    $this->pageName = $pageName;

    return $this;
}

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

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

    return $this;
}

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

/**
 * Set image
 *
 * @param \Test\AdminBundle\Entity\Image $image
 * @return Pages
 */
public function setImage(\Test\AdminBundle\Entity\Image $image = null)
{
    $this->image = $image;

    return $this;
}

/**
 * Get image
 *
 * @return \Test\AdminBundle\Entity\Image 
 */
public function getImage()
{
    return $this->image;
}
}

以下是我的实体“image”的代码:

<?php

namespace Test\AdminBundle\Entity;

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

/**
 * Group
 *
 * @ORM\Table(name="app_images")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class Image
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

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

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

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

/**
 * @Assert\Image(maxSize="2M")
 */
private $file;

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

/**
 * Set name
 *
 * @param string $name
 * @return Image
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

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

/**
 * Set file.
 *
 * @param UploadedFile $file
 * @return Image
 */
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';
    }

    return $this;
}

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

/**
 * @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()
{
    if ($file = $this->getAbsolutePath()) {
        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
    // images 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/images';
}

/**
 * Set alt
 *
 * @param string $alt
 * @return Image
 */
public function setAlt($alt)
{
    $this->alt = $alt;

    return $this;
}

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

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

    return $this;
}

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

pagesType(buildForm函数):

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('title', 'text', array('required' => false))
        ->add('pageTitle', 'text')
        ->add('pageName', 'text')
        ->add('content', 'textarea', array('required' => false))
        ->add('comment', 'text', array('required' => false))
        /* Add new image */
        ->add('image', new ImageType())
        /* Select an existing image */
        /*->add('image', 'entity', array(
            'class' => 'TestAdminBundle:Image',
            'property' => 'name',
            'multiple' => false
        ))*/
    ;
}

现在我可以放置一个嵌套表单或下拉列表。除了我会两个。

我想在我的实体“页面”中添加“临时”变量(我不会坚持的变量),无论我填充哪一个,我都会把它放在变量图像中。但我不知道我将如何服用。我正在努力。

我希望有人可以帮助我,我很抱歉我的英语不好。

2 个答案:

答案 0 :(得分:0)

您需要使用捆绑包进行图片上传。我使用Sonata Media Bundle进行后端开发,但还有其他人可以满足您的需求。

答案 1 :(得分:0)

我认为最好的方法是添加一个文本字段而不是上传文本字段,并附加一个javascript小部件,您可以从该小部件上传或选择现有文件。在选择它时,它将使用所选文件的路径填充文本字段。