3

我是论坛的新手,所以我会尽力尊重论坛规则并保持清晰。

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

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

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

谁能帮助我或告诉我应该如何进行?

谢谢你。

编辑

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

这是我的实体“页面”的代码:

<?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;
}
}

这是我的实体“图像”的代码:

<?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
        ))*/
    ;
}

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

我想在我的实体“页面”中添加“临时”变量(我不会保留的变量),无论我填写哪个,我都会把它放在变量图像中。但我不知道我要怎么走。我正在努力。

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

4

2 回答 2

0

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

于 2016-05-06T19:23:47.147 回答
0

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

于 2015-11-17T20:42:58.910 回答