1

我在 symfony2 上的表单有问题我想为用户上传一些背景图像和配置文件图像。问题是表单正确上传文件并将它们设置为我的配置文件创建配置文件但如果我想编辑配置文件(例如我更改用户名和配置文件图片)任何编辑都会保存但不上传。

我按照 symfony2 文档管理我的上传,这是我的结构:

类简介 {

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

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

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

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

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

/**
 * @ORM\OneToOne(targetEntity="Adel\MpsnBundle\Entity\Document", cascade={"persist"})
 */
private $profilPicture;

/**
 * @ORM\OneToOne(targetEntity="Adel\MpsnBundle\Entity\Document", cascade={"persist"})
 */
private $backgroundPicture;


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

/**
 * Constructor
 */
public function __toString() {
    return $this->surname;
}

/**
 * Constructor
 */
public function __construct() {
    }

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

/**
 * Set firstname
 *
 * @param string $firstname
 * @return Profil
 */
public function setFirstname($firstname) {
    $this->firstname = $firstname;

    return $this;
}

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

/**
 * Set lastname
 *
 * @param string $lastname
 * @return Profil
 */
public function setLastname($lastname) {
    $this->lastname = $lastname;

    return $this;
}

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

/**
 * Set surname
 *
 * @param string $surname
 * @return Profil
 */
public function setSurname($surname) {
    $this->surname = $surname;

    return $this;
}

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

/**
 * Set description
 *
 * @param string $description
 * @return Profil
 */
public function setDescription($description) {
    $this->description = $description;

    return $this;
}

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



/**
 * Set profilPicture
 *
 * @param \Adel\MpsnBundle\Entity\Document $profilPicture
 * @return Profil
 */
public function setProfilPicture(\Adel\MpsnBundle\Entity\Document $profilPicture = null)
{
    $this->profilPicture = $profilPicture;

    return $this;
}

/**
 * Get profilPicture
 *
 * @return \Adel\MpsnBundle\Entity\Document 
 */
public function getProfilPicture()
{
    return $this->profilPicture;
}

/**
 * Set backgroundPicture
 *
 * @param \Adel\MpsnBundle\Entity\Document $backgroundPicture
 * @return Profil
 */
public function setBackgroundPicture(\Adel\MpsnBundle\Entity\Document $backgroundPicture = null)
{
    $this->backgroundPicture = $backgroundPicture;

    return $this;
}

/**
 * Get backgroundPicture
 *
 * @return \Adel\MpsnBundle\Entity\Document 
 */
public function getBackgroundPicture()
{
    return $this->backgroundPicture;
}

}

/** * @ORM\HasLifecycleCallbacks * @ORM\Entity */ 类文档 {

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
public $id;

/**
 * @Assert\File(maxSize="6000000")
 */
public $file;

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

/**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
public function preUpload() {
    if (null !== $this->file) {
        // do whatever you want to generate a unique name
        $filename = sha1(uniqid(mt_rand(), true));
        $this->path = $filename . '.' . $this->file->guessExtension();
    }
}

/**
 * @ORM\PostPersist()
 * @ORM\PostUpdate()
 */
public function upload() {
    if (null === $this->file) {
        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->file->move($this->getUploadRootDir(), $this->path);

    unset($this->file);
}

/**
 * @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
    // documents 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/documents/images/';
}


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

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

    return $this;
}

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

}

类 ProfileType 扩展 AbstractType {

var $profilmanager;

public function __construct(\Adel\MpsnBundle\Service\ProfilManager $profilmanager) {

    $this->profilmanager = $profilmanager;
}

public function buildForm(FormBuilderInterface $builder, array $options) {


    //$profilManager = $this->container->get("Adel_mpsn.profilmanager");
    $builder->add('firstname', 'text')
            ->add('lastname', 'text')
            ->add('surname', 'text')
            ->add('description', 'textarea')
            ->add('profilPicture',new DocumentType())
            ->add('backgroundPicture', new DocumentType);
}

public function setDefaultOptions(OptionsResolverInterface $resolver) {
    $resolver->setDefaults(array(
        'data_class' => 'Adel\MpsnBundle\Entity\Profil'
    ));
}

public function getName() {
    return 'adel_mpsnbundle_profiltype';
}

}

类 DocumentType 扩展 AbstractType {

public function buildForm(FormBuilderInterface $builder, array $options) {


    //$profilManager = $this->container->get("Adel_mpsn.profilmanager");
    $builder->add('file');
}

public function setDefaultOptions(OptionsResolverInterface $resolver) {
    $resolver->setDefaults(array(
        'data_class' => 'Adel\MpsnBundle\Entity\Document'
    ));
}

public function getName() {
    return 'adel_mpsnbundle_documenttype';
}

}

最后在我的控制器中:

    $current_profil = $profilManager->getCurrentProfil(true);
    $form = $this->createForm(new ProfilType($profilManager), $current_profil);
    $request = $this->get('request');
    if ($request->getMethod() == 'POST') {
        // On fait le lien Requête <-> Formulaire
        $form->bind($request);

        // On vérifie que les valeurs rentrées sont correctes
        if ($form->isValid()) {

            // On l'enregistre notre objet $article dans la base de données
            $em = $this->getDoctrine()->getEntityManager();


            $em->persist($current_profil);

            $em->flush();
        }
    }

难道我做错了什么 ?

4

0 回答 0