我对上传有一个symfony2问题。我正在使用doctrine并且有两个实体,第一个叫做“Issue”,有很多附件,第二个叫做“Attachment”。我正在使用动态数据解决方案,使人们可以在“问题”表单上上传许多附件,并为每个上传提供标题。当我保存“问题”时,相关的附件行也会保存,但只保存标题:(上传的实际文件永远不会被“上传”方法捕获。我正在关注symfony2 cookbook教程并想知道我是否错过了一个步骤?我的学习曲线有点迷失,很早就不知道如何调试这个问题。
我的附件实体内容如下:
namespace WebConfection\ServiceDeskBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @ORM\Table(name="webconfection_servicedesk_attachment")
* @ORM\Entity(repositoryClass="WebConfection\ServiceDeskBundle\Repository\AttachmentRepository")
* @ORM\HasLifecycleCallbacks()
*/
class Attachment
{
private $temp;
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=150)
*/
protected $title;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $path;
/**
* @Assert\File(maxSize="6000000")
*/
private $file;
/**
* @ORM\Column(type="datetime")
*/
protected $created;
/**
* @ORM\Column(type="datetime")
*/
protected $updated;
/**
* @ORM\ManyToOne(targetEntity="Issue", inversedBy="attachments")
* @ORM\JoinColumn(name="issue_id", referencedColumnName="id")
*/
protected $issue;
/**
* Constructor
*/
public function __construct()
{
$this->setCreated(new \DateTime());
$this->setUpdated(new \DateTime());
}
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
// check if we have an old image path
if (is_file($this->getAbsolutePath())) {
// store the old name to delete after the update
$this->temp = $this->getAbsolutePath();
} else {
$this->path = 'initial';
}
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->getFile()) {
$this->path = $this->getFile()->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->getFile()) {
return;
}
// check if we have an old image
if (isset($this->temp)) {
// delete the old image
unlink($this->temp);
// clear the temp image path
$this->temp = null;
}
// you must throw an exception here if the file cannot be moved
// so that the entity is not persisted to the database
// which the UploadedFile move() method does
$this->getFile()->move(
$this->getUploadRootDir(),
$this->id.'.'.$this->getFile()->guessExtension()
);
$this->setFile(null);
}
/**
* @ORM\PreRemove()
*/
public function storeFilenameForRemove()
{
$this->temp = $this->getAbsolutePath();
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if (isset($this->temp)) {
unlink($this->temp);
}
}
public function getAbsolutePath()
{
return null === $this->path
? null
: $this->getUploadRootDir().'/'.$this->id.'.'.$this->path;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Attachment
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set created
*
* @param \DateTime $created
* @return Attachment
*/
public function setCreated($created)
{
$this->created = $created;
return $this;
}
/**
* Get created
*
* @return \DateTime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set updated
*
* @param \DateTime $updated
* @return Attachment
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* @return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
}
答案 0 :(得分:2)
我有类似的问题,这就是我解决它的方法。
http://blog.icod.de/2013/04/17/symfony2-file-upload-with-related-entities/
不介意我在symfony2咆哮和咆哮,我需要偶尔在某个地方发泄;)
它可能不是您问题的具体答案,但也许它会帮助您
我正在进行控制器的移动,我当然可以将此功能移动到实体,但是,它可以工作,我只需要上传一次功能,这样可以减少内存使用量,而不是在实体中使用它
HTH