您好,我是symfony的新手,我必须和他一起对产品进行CRUD。 我必须将此信息发送到数据库:
问题:
肥胖:
我的实体:
/**
* @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
*/
class Product {
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="text", length=4000, nullable=true)
*/
private $description;
/**
* @ORM\Column(type="blob")
*/
private $image;
/**
* @ORM\Column(type="integer")
*/
private $stock;
我的表单的生成者:
$product = new Product();
$form = $this->createFormBuilder($product)
->add('title',TextType::class, [
'required' => true,
'label' => 'Titulo: ',
'attr' => ['minlength' => 6, 'id' => 'title_product'],
])
->add('description', TextareaType::class,[
'required' => false,
'label' => 'Descrição: ',
'attr' => ['maxlength' => 4000, 'id' => 'description_product'],
])
->add('image', FileType::class, [
'required' => true,
'attr' => ['accept' => 'image/jpeg, image/png, image/gif'],
'label' => 'Imagem do produto(JPG, PNG ou GIF): ',
'help' => 'A imagem deve ter um peso maximo de 5 MBs.',
])
->add('stock', IntegerType::class, [
'required' => true,
'label' => 'Quantidade em estoque: ',
])
->add('save', SubmitType::class, [
'label' => 'Criar Produto',
'attr' => ['class' => 'btn btn-success']
])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$product = $form->getData();
if ($this->validateFormProduct($product)){
$this->createProduct($product);
return $this->redirectToRoute('index_products');
}
}
return $this->render('product/new.html.twig', [
'form' => $form->createView()
]);
验证功能:
private function invalidImageType($img) {
$permitedTypes = array(IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF);
$detectedType= exif_imagetype($img);
return !in_array($detectedType, $permitedTypes);
}
private function invalidImageSize($img) {
return filesize($img) > 5000000;
}
private function validateFormProduct($form){
if ($this->invalidImageType($form->getImage())){
$this->addFlash(
'warning',
'Tipo de imagem invalido!'
);
return false;
}
if ($this->invalidImageSize($form->getImage())){
$this->addFlash(
'warning',
'Este arquivo excede o tamanho maximo de 5mb!'
);
return false;
}
return true;
}
功能记录:
private function encriptImage($img) {
$normalizer = new DataUriNormalizer();
return $normalizer->normalize(new \SplFileObject($img));
}
答案 0 :(得分:0)
您需要在php.ini
配置文件中增加最大上传大小。
upload_max_filesize
和post_max_size
确保第二个大于或等于第一个。
答案 1 :(得分:0)
您需要更新php.ini中的upload_max_filesize和post_max_size的值:(配置文件)
; Maximum allowed size for uploaded files.
upload_max_filesize = 1M
; Must be greater than or equal to upload_max_filesize
post_max_size = 1M
请参见说明php.ini。