尝试使用synfony 2上传文件

时间:2018-01-02 15:38:50

标签: php symfony file-upload

我正在尝试使用symfony上传我的数据库中的文件,我修改了我的实体和我的表单。但是当我点击上传文档时,我没有任何错误,但没有任何附加内容(我在C:\ wamp64 \ www \ myproject \ web \ upload中没有新文档,而我的数据库字段,此处为support,为null)。这是我的实体的代码:

    <?php

  namespace Main\ConfigBundle\Entity;

  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;


/**
* Invoice
*/
class InvoiceCoach
{

/**
 * @var integer
 */
private $id;

/**
 * @var string
 */
private $reference_invoice;

/**
 * @var string
 */
private $status;

/**
 * @var string
 */
private $total = 0;

/**
 * @var string
 */
private $tva;

/**
 * @var string
 */
private $link;

/**
 * @var string
 */
private $paiement_method;

/**
 * @var \DateTime
 */
private $date_billing;

/**
 * @var \DateTime
 */
private $date_submission;

/**
 * @var \DateTime
 */
private $date_creation;

/**
 * @var \DateTime
 */
private $date_updated;

/**
 * @var string
 */
private $commentaire;

/**
 * @var boolean
 */
private $valid_paul = 0;

/**
 * @var boolean
 */
private $valid_corinne = 0;

/**
 * @var boolean
 */
private $valid_lise = 0;

/**
 * @var \Doctrine\Common\Collections\Collection
 */
private $invoice;

/**
 * @var \Doctrine\Common\Collections\Collection
 */
private $element;

/**
 * @var \Main\ConfigBundle\Entity\Coach
 */
private $coachs;


/**
 * @var File
 */
private $file;

/**
 * Constructor
 */
public function __construct()
{
    $this->invoice = new \Doctrine\Common\Collections\ArrayCollection();
    $this->element = new \Doctrine\Common\Collections\ArrayCollection();
}

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

/**
 * Set referenceInvoice
 *
 * @param string $referenceInvoice
 *
 * @return InvoiceCoach
 */
public function setReferenceInvoice($referenceInvoice)
{
    $this->reference_invoice = $referenceInvoice;

    return $this;
}

/**
 * Get referenceInvoice
 *
 * @return string
 */
public function getReferenceInvoice()
{
    return $this->reference_invoice;
}

/**
 * Set status
 *
 * @param string $status
 *
 * @return InvoiceCoach
 */
public function setStatus($status)
{
    $this->status = $status;

    return $this;
}

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

/**
 * Set total
 *
 * @param string $total
 *
 * @return InvoiceCoach
 */
public function setTotal($total)
{
    $this->total = $total;

    return $this;
}

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

/**
 * Set tva
 *
 * @param string $tva
 *
 * @return InvoiceCoach
 */
public function setTva($tva)
{
    $this->tva = $tva;

    return $this;
}

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

/**
 * Set link
 *
 * @param string $link
 *
 * @return InvoiceCoach
 */
public function setLink($link)
{
    $this->link = $link;

    return $this;
}

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

/**
 * Set paiementMethod
 *
 * @param string $paiementMethod
 *
 * @return InvoiceCoach
 */
public function setPaiementMethod($paiementMethod)
{
    $this->paiement_method = $paiementMethod;

    return $this;
}

/**
 * Get paiementMethod
 *
 * @return string
 */
public function getPaiementMethod()
{
    return $this->paiement_method;
}

/**
 * Set dateBilling
 *
 * @param \DateTime $dateBilling
 *
 * @return InvoiceCoach
 */
public function setDateBilling($dateBilling)
{
    $this->date_billing = $dateBilling;

    return $this;
}

/**
 * Get dateBilling
 *
 * @return \DateTime
 */
public function getDateBilling()
{
    return $this->date_billing;
}

/**
 * Set dateSubmission
 *
 * @param \DateTime $dateSubmission
 *
 * @return InvoiceCoach
 */
public function setDateSubmission($dateSubmission)
{
    $this->date_submission = $dateSubmission;

    return $this;
}

/**
 * Get dateSubmission
 *
 * @return \DateTime
 */
public function getDateSubmission()
{
    return $this->date_submission;
}

/**
 * Set dateCreation
 *
 * @param \DateTime $dateCreation
 *
 * @return InvoiceCoach
 */
public function setDateCreation($dateCreation)
{
    $this->date_creation = $dateCreation;

    return $this;
}

/**
 * Get dateCreation
 *
 * @return \DateTime
 */
public function getDateCreation()
{
    return $this->date_creation;
}

/**
 * Set dateUpdated
 *
 * @param \DateTime $dateUpdated
 *
 * @return InvoiceCoach
 */
public function setDateUpdated($dateUpdated)
{
    $this->date_updated = $dateUpdated;

    return $this;
}

/**
 * Get dateUpdated
 *
 * @return \DateTime
 */
public function getDateUpdated()
{
    return $this->date_updated;
}

/**
 * Set commentaire
 *
 * @param string $commentaire
 *
 * @return InvoiceCoach
 */
public function setCommentaire($commentaire)
{
    $this->commentaire = $commentaire;

    return $this;
}

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

/**
 * Set validPaul
 *
 * @param boolean $validPaul
 *
 * @return InvoiceCoach
 */
public function setValidPaul($validPaul)
{
    $this->valid_paul = $validPaul;

    return $this;
}

/**
 * Get validPaul
 *
 * @return boolean
 */
public function getValidPaul()
{
    return $this->valid_paul;
}

/**
 * Set validCorinne
 *
 * @param boolean $validCorinne
 *
 * @return InvoiceCoach
 */
public function setValidCorinne($validCorinne)
{
    $this->valid_corinne = $validCorinne;

    return $this;
}

/**
 * Get validCorinne
 *
 * @return boolean
 */
public function getValidCorinne()
{
    return $this->valid_corinne;
}

/**
 * Set validLise
 *
 * @param boolean $validLise
 *
 * @return InvoiceCoach
 */
public function setValidLise($validLise)
{
    $this->valid_lise = $validLise;

    return $this;
}

/**
 * Get validLise
 *
 * @return boolean
 */
public function getValidLise()
{
    return $this->valid_lise;
}

/**
 * Add invoice
 *
 * @param \Main\ConfigBundle\Entity\InvoiceBox $invoice
 *
 * @return InvoiceCoach
 */
public function addInvoice(\Main\ConfigBundle\Entity\InvoiceBox $invoice)
{
    $this->invoice[] = $invoice;

    return $this;
}

/**
 * Remove invoice
 *
 * @param \Main\ConfigBundle\Entity\InvoiceBox $invoice
 */
public function removeInvoice(\Main\ConfigBundle\Entity\InvoiceBox $invoice)
{
    $this->invoice->removeElement($invoice);
}

/**
 * Get invoice
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getInvoice()
{
    return $this->invoice;
}

/**
 * Add element
 *
 * @param \Main\ConfigBundle\Entity\InvoiceElement $element
 *
 * @return InvoiceCoach
 */
public function addElement(\Main\ConfigBundle\Entity\InvoiceElement $element)
{
    $this->element[] = $element;

    return $this;
}

/**
 * Remove element
 *
 * @param \Main\ConfigBundle\Entity\InvoiceElement $element
 */
public function removeElement(\Main\ConfigBundle\Entity\InvoiceElement $element)
{
    $this->element->removeElement($element);
}

/**
 * Get element
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getElement()
{
    return $this->element;
}

/**
 * Set coachs
 *
 * @param \Main\ConfigBundle\Entity\Coach $coachs
 *
 * @return InvoiceCoach
 */
public function setCoachs(\Main\ConfigBundle\Entity\Coach $coachs = null)
{
    $this->coachs = $coachs;

    return $this;
}

/**
 * Get coachs
 *
 * @return \Main\ConfigBundle\Entity\Coach
 */
public function getCoachs()
{
    return $this->coachs;
}
/**
 * @var string
 */
private $support;


/**
 * Set support
 *
 * @param string $support
 *
 * @return InvoiceCoach
 */
public function setSupport($support)
{
    $this->support = $support;

    return $this;
}

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

/**
 * Set file
 *
 * @param File $file
 * @return InvoiceCoach
 */
public function setFile($file)
{
    $this->file = $file;

    return $this;
}

/**
 * Get file
 *
 * @return File
 */
public function getFile()
{
    return $this->file;
}


// File Upload Method

public function getAbsolutePath()
{
    return null === $this->support ? null : $this->getUploadRootDir().'/'.$this->support;
}

public function getPath()
{
    return null === $this->support ? null : $this->getUploadDir().'/'.$this->support;
}

protected function getUploadRootDir()
{
    return __DIR__.'/../../../../web/'.$this->getUploadDir();
}

protected function getUploadDir()
{
    return 'upload';
}




// Doctrine Specific Method

public function __persist()
{

    if($this->file !== null)
        $this->support = sha1(uniqid(mt_rand(), true)).'.'.$this->file->guessExtension();

}

public function __postPersist()
{
    if($this->file === null)
        return;


    $this->file->move($this->getUploadRootDir(), $this->support);

    unset($this->file);
    $this->file = null;
}

public function __flush()
{
    if($this->file === null)
        return;

    $this->__remove();
    $this->__persist();
    $this->__postPersist();
}

public function __remove()
{
    if($file = $this->getAbsolutePath())
        if(file_exists($file))
            unlink($file);
}

}

THX很多

编辑:这是我的控制器的代码(创建表单时)以及我在树枝上调用的地方:

$em = $this->getDoctrine()->getManager();

$invoice = $em->getRepository("MainConfigBundle:InvoiceCoach")->find($id);


$formUpload = $this->createForm('invoicesupport', $invoice)
    ->add('save', 'submit', array('label' => 'Enregistrer') );

    if($request->getMethod() == "POST"){
        $formUpload->handleRequest($request);

        if($formUpload->isValid()){

            $em->persist($invoice);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('coach_paid_edit',  array('id' => $id) ));
    }

树枝:

 <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="exampleModalLongTitle">Upload facture</h5>
                        </div>
                        <div class="modal-body">
                            {{ form_start(form, {'attr': {'novalidate': 'novalidate', 'data-parsley-validate':'data-parsley-validate'}}) }}
                            {{ form_row(formUpload.file) }}
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Fermer</button>
                            {{ form_row(formUpload.save) }}
                            {{ form_end(form) }}
                        </div>
                    </div>

1 个答案:

答案 0 :(得分:0)

首先也是最重要的 - 你是否测试过#34; Doctrine Specific Method&#34;正在开火?我的猜测是他们没有开火(老实说,我看到这样的第一次接近 - 而且我不知道为什么它会起作用)

即使 - 我认为这种方法很糟糕,这段代码应该转移到事件监听器/订阅。它不应该在实体文件中。

查看http://symfony.com/doc/current/doctrine/event_listeners_subscribers.html

在这里,您可以链接到我的旧上传解决方案,其中Trait已连接到实体 https://gist.github.com/poznet/e3955a48fea01d8640dafbd966c53a83