Hellow Everybody
我是symfony2世界的新手,我正在开发一个电子商务项目,我需要修改产品的图像,我使用doctrine生命周期函数来上传图像和liipimaginebundle来处理到thmbnails。我的代码是
Article.php
<?php
namespace StoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\validator\Constraints as Assert;
/**
* Image
*
* @ORM\Table(name="image")
* @ORM\Entity(repositoryClass="StoreBundle\Repository\ImageRepository")
* @ORM\HasLifecycleCallbacks
*/
class Image
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="updated_at",type="datetime", nullable=true)
*/
private $updateAt;
/**
* @ORM\PostLoad()
*/
public function postLoad()
{
$this->updateAt = new \DateTime();
}
/**
* @ORM\Column(type="string",length=255, nullable=true)
*/
private $path;
public $file;
public function getUploadRootDir()
{
return __dir__.'/../../../public_html/uploads';
}
public function getAbsolutePath()
{
return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
}
public function getAssetPath()
{
return 'uploads/'.$this->path;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
$this->tempFile = $this->getAbsolutePath();
$this->oldFile = $this->getPath();
$this->updateAt = new \DateTime();
if (null !== $this->file)
$this->path = sha1(uniqid(mt_rand(),true)).'.'.$this->file->guessExtension();
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null !== $this->file) {
$this->file->move($this->getUploadRootDir(),$this->path);
unset($this->file);
if ($this->oldFile != null) unlink($this->tempFile);
}
}
/**
* @ORM\PreRemove()
*/
public function preRemoveUpload()
{
$this->tempFile = $this->getAbsolutePath();
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if (file_exists($this->tempFile)) unlink($this->tempFile);
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set updateAt
*
* @param \DateTime $updateAt
* @return Image
*/
public function setUpdateAt($updateAt)
{
$this->updateAt = $updateAt;
return $this;
}
/**
* Get updateAt
*
* @return \DateTime
*/
public function getUpdateAt()
{
return $this->updateAt;
}
/**
* 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;
}
}
ArticleImageType.php
<?php
namespace StoreBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use StoreBundle\Form\ImageType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
class ArticleImageType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('image', new ImageType())
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'StoreBundle\Entity\Article'
));
}
}
在我的控制器里,我把:
public function imageEditAction(Request $request, Article $article)
{
$editForm = $this->createForm('StoreBundle\Form\ArticleImageType', $article);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($article);
$em->flush();
return $this->redirectToRoute('admin_article_show', array('id' => $article->getId()));
}
return $this->render('AdminBundle:article:imageEdit.html.twig', array(
'article' => $article,
'edit_form' => $editForm->createView(),
));
}
当我尝试修改图像时。没有被修改,旧图像出现在文章中。我需要你的帮助,并提前感谢