2016-07-23 40 views
0

如何在我的更新表单中检索存储在数据库中的图像?Symfony - 如何检索我的更新表单中存储在数据库中的图像?

我已成立了一个档案实体那样:

<?php 

namespace Krown\DashboardBundle\Entity; 
use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 
use FOS\UserBundle\Model\User as BaseUser; 
use Symfony\Component\Validator\Constraints as Assert; 
use Krown\DashboardBundle\Entity\Status; 
use Krown\DashboardBundle\Entity\Picture; 
use Symfony\Component\HttpFoundation\File\File; 
use Vich\UploaderBundle\Mapping\Annotation as Vich; 

/** 
* Profile 
* 
* @ORM\Table(name="profile") 
* @ORM\Entity(repositoryClass="Krown\DashboardBundle\Repository\ProfileRepository") 
* @Vich\Uploadable 
*/ 
class Profile 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * NOTE: This is not a mapped field of entity metadata, just a simple property. 
    * 
    * @Vich\UploadableField(mapping="product_image", fileNameProperty="imageName") 
    * 
    * @var File 
    */ 
    private $imageFile; 

    /** 
    * @ORM\Column(type="string", length=255, nullable=true) 
    * 
    * @var string 
    */ 
    private $imageName; 

    /** 
    * @ORM\Column(type="datetime", nullable=true) 
    * 
    * @var \DateTime 
    */ 
    private $updatedAt; 

    /** 
    * @var boolean 
    * 
    * @ORM\Column(name="avocat", type="boolean") 
    */ 
    private $avocat; 


    /** 
    * @var string 
    * 
    * @ORM\Column(name="breve_description", type="string", nullable=true) 
    */ 
    protected $breve_description; 

    /** 
    * @var array 
    * 
    * @ORM\Column(name="fields_of_law", type="array", nullable=true) 
    */ 
    protected $fields_of_law; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="registre_ou_bareau", type="string", nullable=true) 
    */ 
    protected $registre_ou_bareau; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="canton_actif", type="string", nullable=true) 
    */ 
    protected $canton_actif; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="annee_obtention_brevet", type="string", nullable=true) 
    */ 
    protected $annee_obtention_brevet; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="titre_supplementaire", type="string", nullable=true) 
    */ 
    protected $titre_supplementaire; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="years_of_experience", type="string", nullable=true) 
    */ 
    protected $years_of_experience; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="langues_de_travail", type="string", nullable=true) 
    */ 
    protected $langues_de_travail; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="email_contact", type="string", nullable=true) 
    */ 
    protected $email_contact; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="numero_telephone", type="string", nullable=true) 
    */ 
    protected $numero_telephone; 

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

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

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

    /** 
    * @ORM\OneToOne(targetEntity="User") 
    * @ORM\JoinColumn(name="user_id", referencedColumnName="id") 
    */ 
    protected $user; 

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

    /** 
    * Set title 
    * 
    * @param string $title 
    * 
    * @return Profile 
    */ 
    public function setTitle($title) 
    { 
     $this->title = $title; 

     return $this; 
    } 

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

    /** 
    * Set body 
    * 
    * @param string $body 
    * 
    * @return Profile 
    */ 
    public function setBody($body) 
    { 
     $this->body = $body; 

     return $this; 
    } 

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

    /** 
    * Set createdDate 
    * 
    * @param \DateTime $createdDate 
    * 
    * @return Profile 
    */ 
    public function setCreatedDate($createdDate) 
    { 
     $this->createdDate = $createdDate; 

     return $this; 
    } 

    /** 
    * Get createdDate 
    * 
    * @return \DateTime 
    */ 
    public function getCreatedDate() 
    { 
     return $this->createdDate; 
    } 

    /** 
    * Set user 
    * 
    * @param \Krown\DashboardBundle\Entity\User $user 
    * 
    * @return Profile 
    */ 
    public function setUser(\Krown\DashboardBundle\Entity\User $user = null) 
    { 
     $this->user = $user; 

     return $this; 
    } 

    /** 
    * Get user 
    * 
    * @return \Krown\DashboardBundle\Entity\User 
    */ 
    public function getUser() 
    { 
     return $this->user; 
    } 

    /** 
    * If manually uploading a file (i.e. not using Symfony Form) ensure an instance 
    * of 'UploadedFile' is injected into this setter to trigger the update. If this 
    * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter 
    * must be able to accept an instance of 'File' as the bundle will inject one here 
    * during Doctrine hydration. 
    * 
    * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image 
    * 
    * @return Product 
    */ 

    public function setImageFile(File $image = null) 
    { 
     $this->imageFile = $image; 

     if ($image) { 
      // It is required that at least one field changes if you are using doctrine 
      // otherwise the event listeners won't be called and the file is lost 
      $this->updatedAt = new \DateTime('now'); 
     } 

     return $this; 
    } 

    /** 
    * @return File 
    */ 
    public function getImageFile() 
    { 
     return $this->imageFile; 
    } 

    /** 
    * Set imageName 
    * 
    * @param string $imageName 
    * 
    * @return Profile 
    */ 
    public function setImageName($imageName) 
    { 
     $this->imageName = $imageName; 

     return $this; 
    } 

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

    /** 
    * Set updatedAt 
    * 
    * @param \DateTime $updatedAt 
    * 
    * @return Profile 
    */ 
    public function setUpdatedAt($updatedAt) 
    { 
     $this->updatedAt = $updatedAt; 

     return $this; 
    } 

    /** 
    * Get updatedAt 
    * 
    * @return \DateTime 
    */ 
    public function getUpdatedAt() 
    { 
     return $this->updatedAt; 
    } 

    /** 
    * Set avocat 
    * 
    * @param boolean $avocat 
    * 
    * @return Profile 
    */ 
    public function setAvocat($avocat) 
    { 
     $this->avocat = $avocat; 

     return $this; 
    } 

    /** 
    * Get avocat 
    * 
    * @return boolean 
    */ 
    public function getAvocat() 
    { 
     return $this->avocat; 
    } 

    /** 
    * Set breveDescription 
    * 
    * @param string $breveDescription 
    * 
    * @return Profile 
    */ 
    public function setBreveDescription($breveDescription) 
    { 
     $this->breve_description = $breveDescription; 

     return $this; 
    } 

    /** 
    * Get breveDescription 
    * 
    * @return string 
    */ 
    public function getBreveDescription() 
    { 
     return $this->breve_description; 
    } 

    /** 
    * Set fieldsOfLaw 
    * 
    * @param array $fieldsOfLaw 
    * 
    * @return Profile 
    */ 
    public function setFieldsOfLaw($fieldsOfLaw) 
    { 
     $this->fields_of_law = $fieldsOfLaw; 

     return $this; 
    } 

    /** 
    * Get fieldsOfLaw 
    * 
    * @return array 
    */ 
    public function getFieldsOfLaw() 
    { 
     return $this->fields_of_law; 
    } 

    /** 
    * Set registreOuBareau 
    * 
    * @param string $registreOuBareau 
    * 
    * @return Profile 
    */ 
    public function setRegistreOuBareau($registreOuBareau) 
    { 
     $this->registre_ou_bareau = $registreOuBareau; 

     return $this; 
    } 

    /** 
    * Get registreOuBareau 
    * 
    * @return string 
    */ 
    public function getRegistreOuBareau() 
    { 
     return $this->registre_ou_bareau; 
    } 

    /** 
    * Set cantonActif 
    * 
    * @param string $cantonActif 
    * 
    * @return Profile 
    */ 
    public function setCantonActif($cantonActif) 
    { 
     $this->canton_actif = $cantonActif; 

     return $this; 
    } 

    /** 
    * Get cantonActif 
    * 
    * @return string 
    */ 
    public function getCantonActif() 
    { 
     return $this->canton_actif; 
    } 

    /** 
    * Set anneeObtentionBrevet 
    * 
    * @param string $anneeObtentionBrevet 
    * 
    * @return Profile 
    */ 
    public function setAnneeObtentionBrevet($anneeObtentionBrevet) 
    { 
     $this->annee_obtention_brevet = $anneeObtentionBrevet; 

     return $this; 
    } 

    /** 
    * Get anneeObtentionBrevet 
    * 
    * @return string 
    */ 
    public function getAnneeObtentionBrevet() 
    { 
     return $this->annee_obtention_brevet; 
    } 

    /** 
    * Set titreSupplementaire 
    * 
    * @param string $titreSupplementaire 
    * 
    * @return Profile 
    */ 
    public function setTitreSupplementaire($titreSupplementaire) 
    { 
     $this->titre_supplementaire = $titreSupplementaire; 

     return $this; 
    } 

    /** 
    * Get titreSupplementaire 
    * 
    * @return string 
    */ 
    public function getTitreSupplementaire() 
    { 
     return $this->titre_supplementaire; 
    } 

    /** 
    * Set yearsOfExperience 
    * 
    * @param string $yearsOfExperience 
    * 
    * @return Profile 
    */ 
    public function setYearsOfExperience($yearsOfExperience) 
    { 
     $this->years_of_experience = $yearsOfExperience; 

     return $this; 
    } 

    /** 
    * Get yearsOfExperience 
    * 
    * @return string 
    */ 
    public function getYearsOfExperience() 
    { 
     return $this->years_of_experience; 
    } 

    /** 
    * Set languesDeTravail 
    * 
    * @param string $languesDeTravail 
    * 
    * @return Profile 
    */ 
    public function setLanguesDeTravail($languesDeTravail) 
    { 
     $this->langues_de_travail = $languesDeTravail; 

     return $this; 
    } 

    /** 
    * Get languesDeTravail 
    * 
    * @return string 
    */ 
    public function getLanguesDeTravail() 
    { 
     return $this->langues_de_travail; 
    } 

    /** 
    * Set emailContact 
    * 
    * @param string $emailContact 
    * 
    * @return Profile 
    */ 
    public function setEmailContact($emailContact) 
    { 
     $this->email_contact = $emailContact; 

     return $this; 
    } 

    /** 
    * Get emailContact 
    * 
    * @return string 
    */ 
    public function getEmailContact() 
    { 
     return $this->email_contact; 
    } 

    /** 
    * Set numeroTelephone 
    * 
    * @param string $numeroTelephone 
    * 
    * @return Profile 
    */ 
    public function setNumeroTelephone($numeroTelephone) 
    { 
     $this->numero_telephone = $numeroTelephone; 

     return $this; 
    } 

    /** 
    * Get numeroTelephone 
    * 
    * @return string 
    */ 
    public function getNumeroTelephone() 
    { 
     return $this->numero_telephone; 
    } 
} 

而一个档案更新形式,这样的:

public function editAction($id, Request $request) 
    { 
     $em = $this->getDoctrine()->getManager(); 
     $profile = $em->getRepository('KrownDashboardBundle:Profile')->find($id); 
     if (!$profile) { 
     throw $this->createNotFoundException(
       'No news found for id ' . $id 
     ); 
     } 

     $form = $this->createFormBuilder($profile) 
      ->add('title', 'text') 
      ->add('breve_description', TextareaType::class, array(
      'attr' => array('class' => 'tinymce'), 
      )) 
      ->add('fields_of_law', 'choice', array(
       'choices' => array(
        'Droit du travail' => "Droit du travail", 
        'Droit de la famille' => "Droit de la famille", 
        'Contrats commerciaux' => "Contrats commerciaux", 
      ), 
       'choices_as_values' => true, 
       'multiple' => true, 
     )) 
      ->add('imageFile', FileType::class, array(
       'label' => 'Photo', 
       'data' => $profile->getImageFile(), 
       'required'  => false, 
      ) 
      ) 
      ->add('registre_ou_bareau') 
      ->add('canton_actif') 
      ->add('annee_obtention_brevet') 
      ->add('titre_supplementaire') 
      ->add('years_of_experience') 
      ->add('langues_de_travail') 
      ->add('email_contact') 
      ->add('numero_telephone') 
      ->add('avocat') 
      ->getForm(); 

     $form->handleRequest($request); 

     if ($form->isValid()) { 
      $em->flush(); 
      return new Response('Profile updated successfully'); 
     } 

     $build['form'] = $form->createView(); 

     return $this->render('KrownDashboardBundle:Profile:profiles_edit_add.html.twig', $build); 
    } 

在这种形式下,我可以更新和检索任何除了与FileType field完全一致。

当我提交表单,文件$镜像文件正确上传,文件名$ imageName存储在数据库中。问题是我无法附加已加载到表单上的文件。

我可以得到$简介 - > getImageName()存储在数据库中的文件名,但是当我做$简介 - > getImageFile(),我得到一个空值?

我错过了什么吗?

EDIT

我也试图手动加载文件与这样的文件名。

$profile->setImageFile(
    new File(__DIR__.'/../../../../web/images/products/'.$profile->getImageName()) 
); 

现在,当我做$简介 - > getImageFile()我可以看到该文件,但它没有连接到表格。

回答

0

为了安全起见,您不能将文件附加到文件输入,这是不可能的。但是,相反,您可以使用一些jQuery库来显示/上传文件(可能是bluimp),或者只显示带有文件路径的图像标记,允许将用户更改为文件。

+0

真的吗?听到这个我真的很惊讶!关于bluimp,你的意思是jQuery文件上传?实际上,这是下一步,我打算整合诸如jQuery File Upload或Dropzone.js;但确切地说,如果我想玩它,我将需要以我的更新形式加载图像文件。例如,如果我想用Jcrop裁剪图像(或者在编辑配置文件时更改裁剪),我需要获取图像文件,对吧?如果我们无法以更新的形式加载图像,这太奇怪了!我正在考虑使用这个第三方软件包:ComurImageBundle。你怎么看待这件事? – 118218

相关问题