1

symfony2 のフォームに問題があり、ユーザーの背景画像とプロフィール画像をアップロードしたいと考えています。問題は、フォームがファイルを正しくアップロードし、プロファイルの作成時にプロファイルに設定することですが、プロファイルを編集したい場合 (たとえば、ユーザー名とプロファイルの写真を変更する) は編集は保存されますが、アップロードは保存されません。

私は symfony2 doc に従ってアップロードを管理しました。これが私の構造です:

クラスプロフィール{

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

}

class ProfileType extends 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';
}

}

class DocumentType extends 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