如何解决Vichupload 没有映射....?
我正在尝试使用 vichuploaderbundle。 严格按照以下步骤操作,我遇到了一个错误:
https://github.com/dustin10/VichUploaderBundle/blob/master/docs/usage.md
错误是
An exception occurred while executing 'INSERT INTO avatar (image_name,image_size,updated_at) VALUES (?,?,?)' with params [null,null,null]:
sqlSTATE[23000]: Integrity constraint violation: 1048 Column 'image_name' cannot be null
我有点失落
PHP bin/console vich:mapping:debug-class App\Entity\Avatar ** 好像没问题 **
Introspecting class App\Entity\Avatar:
Found field "imageFile",storing file name in "imageName" and using mapping "avatar_images"
PHP bin/console vich:mapping:debug avatar_images ** 好像没问题 **
Debug information for mapping avatar_images
uri_prefix: '/images/'
upload_destination: 'D:\\Laragon\\www\\Appli_Sympho2/public/images/'
namer: array (
'service' => NULL,'options' => NULL,)
directory_namer: array (
'service' => NULL,)
delete_on_remove: true
delete_on_update: true
inject_on_load: false
db_driver: 'orm'
PHP bin/console vich:mapping:list-classes ** 0 classes 这正常吗? **
Looking for uploadable classes.
Found 0 classes.
NOTE: Only classes configured using XML or YAML are displayed.
# config/packages/vich_uploader.yaml
vich_uploader:
db_driver: orm
mappings:
avatar_images:
uri_prefix: '%app.path.avatar_images%'
upload_destination: '%kernel.project_dir%/public%app.path.avatar_images%'
#config/services.yaml
parameters:
app.path.avatar_images: /images
实体头像
<?PHP
namespace App\Entity;
use App\Repository\AvatarRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass=AvatarRepository::class)
* @Vich\Uploadable
*/
class Avatar
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
*
* @Vich\UploadableField(mapping="avatar_images",fileNameProperty="imageName",size="imageSize")
*
* @var File|null
*/
private $imageFile;
/**
* @ORM\Column(type="string")
*
* @var string|null
*/
private $imageName;
/**
* @ORM\Column(type="integer")
*
* @var int|null
*/
private $imageSize;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTimeInterface|null
*/
private $updatedAt;
public function getId(): ?int
{
return $this->id;
}
/**
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
*/
public function setimageFile(?File $imageFile = null): void
{
$this->imageFile = $imageFile;
if (null !== $imageFile) {
$this->updatedAt = new \DateTimeImmutable();
}
}
public function getimageFile(): ?File
{
return $this->imageFile;
}
public function setimageName(?string $imageName): void
{
$this->imageName = $imageName;
}
public function getimageName(): ?string
{
return $this->imageName;
}
public function setimageSize(?int $imageSize): void
{
$this->imageSize = $imageSize;
}
public function getimageSize(): ?int
{
return $this->imageSize;
}
}
头像类型.PHP
<?PHP
namespace App\Form;
use App\Entity\Avatar;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Vich\UploaderBundle\Form\Type\VichImageType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AvatarType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder,array $options)
{
$builder
->add('imageFile',VichImageType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Avatar::class,]);
}
}
头像控制器
<?PHP
namespace App\Controller;
use App\Entity\Avatar;
use App\Form\AvatarType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class AvatarController extends AbstractController
{
/**
* @Route("/avatar",name="avatar")
*/
public function index(Request $request,EntityManagerInterface $manager): Response
{
$avatar=new Avatar();
$form = $this->createForm(AvatarType::class);
$form->handleRequest($request);
if ($form->isSubmitted() and $form->isValid()) {
$manager->persist($avatar);
$manager->flush();
$this->addFlash(
'success','Modification enregistrée'
);
return $this->redirectToRoute('profile');
}
return $this->render('avatar/index.html.twig',[
'form' => $form->createView()
]);
}
}
解决方法
对不起,我发现我的错误
在我的 AvatarController 中,我忘记了 $avatar 参数!!!
$form = $this->createForm(AvatarType::class,$avatar);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。