我使用symfony 3并且我创建了一个简单的表单来获取公司信息,包括公司徽标。
我可以创建表单,上传徽标图片以及其他值,将其保存在我的数据库中并将其移动到我的特定上传文件夹。但是当我回到表单上时(在刷新数据之后),除了徽标值之外,我得到了所有的Entity值。我解释得更好,因为我在这个过程中遇到了两个问题,但是在我要显示我的代码之前。
这是我的实体CompanyProfile.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use function PHPSTORM_META\type;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
/**
* CompanyProfile
*
* @ORM\Table(name="company_profile")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CompanyProfileRepository")
*/
class CompanyProfile
{
// properties ...
/**
* @var string
*
* @ORM\Column(name="logo", type="string", length=255, nullable=true)
* @Assert\Image(
* minWidth = 50,
* maxWidth = 800,
* minHeight = 50,
* maxHeight = 800,
* allowLandscape = false,
* allowPortrait = false
* )
*/
private $logo;
// other method
/**
* Set logo
*
* @param string $logo
*
* @return CompanyProfile
*/
public function setLogo($logo)
{
$this->logo = $logo;
return $this;
}
/**
* Get logo
*
* @return string
*/
public function getLogo()
{
return $this->logo;
}
}
这是我的行动
public function companyProfileAction(Request $request, FileUploader $fileUploader)
{
$fileUploader->setTargetDir($this->getParameter('company_profile_dir'));
$companyProfile = $this->getDoctrine()->getRepository(CompanyProfile::class)->getCompany();
if ($fileName = $companyProfile->getLogo()) {
try {
$companyProfile->setLogo(new File($fileUploader->getTargetDir() . '/' . $fileName));
} catch (FileNotFoundException $fileNotFoundException) {
dump($fileNotFoundException->getMessage());
}
}
$companyProfileForm = $this->createForm(CompanyProfileType::class, $companyProfile);
$companyProfileForm->handleRequest($request);
if ($companyProfileForm->isSubmitted() && $companyProfileForm->isValid()) {
$companyProfile = $companyProfileForm->getData();
/**
* @var \Symfony\Component\HttpFoundation\File\UploadedFile $logo
*/
$logo = $companyProfile->getLogo();
if (!is_null($logo)) {
// Generate a unique name for the file before saving it
$fileName = $fileUploader->upload($logo);
$companyProfile->setLogo($fileName);
}
$em = $this->getDoctrine()->getManager();
$em->persist($companyProfile);
$em->flush();
$this->addFlash('success', $this->get('translator')->trans('Information are saved'));
return $this->redirectToRoute('homepage');
} elseif ($companyProfileForm->isSubmitted() && !$companyProfileForm->isValid()) {
$this->addFlash('error', $this->get('translator')->trans('Some errors are accured'));
}
return $this->render('setting/companyProfile.html.twig', array(
'companyProfileForm' => $companyProfileForm->createView()
));
}
此处我的上传服务(参数设置在config.yml
和service.yml
上)
<?php
namespace AppBundle\Service;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class FileUploader
{
private $targetDir;
public function __construct($targetDir)
{
$this->targetDir = $targetDir;
}
/**
* @param mixed $targetDir
*/
public function setTargetDir($targetDir)
{
$this->targetDir = $targetDir;
}
public function upload(UploadedFile $file)
{
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$file->move($this->getTargetDir(), $fileName);
return $fileName;
}
public function getTargetDir()
{
return $this->targetDir;
}
}
现在,我已经向您展示了我的代码,我可以解释我的问题:
$companyProfile->setLogo($file)
并在操作开始时更改我的数据库上的值(设置完整路径而不是全名)执行实体的persist
和flush
,因为我只在表单提交时才调用它们$('#<id>').[0]files
中为空)任何解决方案?