0

私は Symfony3.1 を使用しており、VichUploaderBundle をインストールしました。ドキュメントに従っていますが、このエラーは、フィールド 'image_name' を空にすることはできません (null) ことを意味します

SQLSTATE[23000]: 整合性制約違反: 1048 Le champ 'image_name' ne peut être vide (null)

設定

vich_uploader:
db_driver: orm
mappings:
    product_image:
        uri_prefix:         /images/actualites
        upload_destination: kernel.root_dir/../web/images/actualites

        inject_on_load:     false
        delete_on_update:   true
        delete_on_remove:   true

実体

/**
 *
 * @Vich\UploadableField(mapping="actualite_image", fileNameProperty="imageName")
 *
 * @var File
 */
private $imageFile;

/**
 * @ORM\Column(type="string", length=255)
 *
 * @var string
 */
private $imageName;

/**
 * @ORM\Column(type="datetime")
 *
 * @var \DateTime
 */
private $updatedAt;

/**
 * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
 *
 * @return Actualite
 */

public function setImageFile(File $image = null)
{
    $this->imageFile = $image;

    if ($image) {
        // It is required that at least one field changes if you are using doctrine
        // otherwise the event listeners won't be called and the file is lost
        $this->updatedAt = new \DateTime('now');
    }

    return $this;
}

/**
 * @return File
 */
public function getImageFile()
{
    return $this->imageFile;
}

/**
 * @param string $imageName
 *
 * @return Actualite
 */
public function setImageName($imageName)
{
    $this->imageName = $imageName;

    return $this;
}

/**
 * @return string
 */
public function getImageName()
{
    return $this->imageName;
}

フォームタイプ:

->add('imageFile', FileType::class)

小枝

<form class="form-horizontal" method="post" enctype="multipart/form-data">
 //...
 {{ form_widget(form.imageFile) }}

アクション

public function creerAction(Request $request)
{
    $entity = new Actualite();
    $form = $this->createForm(BaseType::class, $entity);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirectToRoute('backend_actualite_liste');
    }

    return $this->render('Backend/Actualite/creer.html.twig',array(
            'form' => $form->createView(),
        )
    );
}
4

3 に答える 3

0

参考までに、ここには異なるSymfony3 セットアップがあり、参考になるかもしれません。

あなたの設定では、Symfony3 では異なると思います。これを試して:

->add('imageFile', VichImageType::class, array(
    'required'      => false,
))

また、次の変更も必要です。

# app/config/config.yml
twig:
    form_themes:
        # other form themes
        - 'VichUploaderBundle:Form:fields.html.twig'
于 2016-06-20T16:07:40.217 に答える