我用的是symfony 4.1,我很难找到我想要的相对/完整的工作路径。
在我的数据库中,我有一个客户实体,具有一个名为photo的属性。
<?php
namespace App\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Entity\CustomerRepository")
* @ORM\Table("Customer")
*/
class Customer {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", nullable=true)
*
* @Assert\File(mimeTypes={ "image/png","image/jpeg" })
*/
private $photo;
public function getPhoto(): ?string
{
return $this->photo;
}
public function setPhoto(?string $photo): self
{
$this->photo = $photo;
return $this;
}
这是有意义的,当我将客户与照片上载一起保存时,它会像我预期的那样将照片保存在数据库和文件系统中。
在数据库中,照片列将设置为“010925C8C427BDCA9020197212B64AF.png”。
这就是我想要的,所以一切都很好。
当我试图更新现有客户实体时,出现了这个问题。客户->getphoto()将返回相对路径文件名“010925C8C427BDCA9020197212B64AF.png”。
但是表单没有通过验证,它说这个文件不存在。
$em = $this->getDoctrine()->getManager();
$custRepo = $em->getRepository('App:Customer');
$customer = $custRepo->findOneById($id);
$custForm = $this->createForm(CustomerType::class, $customer);
$custForm->handleRequest($request);
if ($custForm->isSubmitted() && $custForm->isValid()) {
$em->flush();
}
验证失败,因为验证不在照片目录中。
这是我的解决方案,它确实有效,但似乎太刻薄了。我不想知道是否有人对此有更优雅的态度。
$em = $this->getDoctrine()->getManager();
$custRepo = $em->getRepository('App:Customer');
$customer = $custRepo->findOneById($id);
$customer->setPhoto(new File($this->getParameter('photos_dir') .'/' . $customer->getPhoto()));
$custForm = $this->createForm(CustomerType::class, $customer);
$custForm->handleRequest($request);
if ($custForm->isSubmitted() && $custForm->isValid()) {
$photoPathParts = explode('/', $customer->getPhoto());
$customer->setPhoto(array_pop($photoPathParts));
$em->flush();
}
我正在获取照片的完整路径,并更新当前正在处理的实体。这将通过表单验证,但如果我只保存它,数据库中的路径将更新为照片的完整路径。这不是我想要的,所以我将照片重置为相对路径文件名。
/**
* @ORM\Column(type="string", nullable=true)
*
* @Assert\File(mimeTypes={ "image/png","image/jpeg" })
*/
private $photo;