1

私のエンティティはすべて使用してセットアップされてymlおり、VichUploaderBundle をセットアップする方法について頭を悩ませようとしています。

私がやろうとしていること

私がやろうとしているのは、API を開発することです。ユーザーが名前付きの画像を送信する必要があり、送信された情報をデータベースに保存し、サーバーにファイルをアップロードしたい

機能しているものと機能していないもの

次のスクリーンショットでわかるように、私は Postman を使用して情報を送信しています。名前と画像フィールドが送信されています。送信時に名前は保存されますが、画像はアップロードされず、画像名フィールドも空です。 ここに画像の説明を入力

私の試み

ここですべてのコードを共有します。長くて退屈に見えるかもしれませんが、助けが必要です :)

内部に構成config.ymlを追加しましたVichUploaderBundle

vich_uploader:
    db_driver: orm
    mappings:
        gift_image:
            uri_prefix:         /uploads/gifts
            upload_destination: %kernel.root_dir%/../web/uploads/gifts

これは私orm.ymlGiftListエンティティです

AppBundle\Entity\GiftList:
    type: entity
    table: null
    repositoryClass: AppBundle\Repository\GiftListRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 255
        image_name:
            type: text
            nullable: true
    lifecycleCallbacks: {  }

これは、VichUploaderBundle の私のマッピング ファイルです。

AppBundle\Entity\GiftList:
    file:
        mapping:           gift_image
        filename_property: image_name

これは私のエンティティです

namespace AppBundle\Entity;
use Symfony\Component\HttpFoundation\File\File;

/**
 * GiftList
 */
class GiftList
{

    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var string
     */

    private $image_name;

    /**
     * @var File $image
     */
    protected $image;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return GiftList
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

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

        return $this;
    }

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

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

    /**
     * @param File $image
     */
    public function setImage(File $image)
    {
        $this->image = $image;
    }
}

これは私のフォームです

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name')
        ->add('image', VichFileType::class)
    ;
}

そして最後にコントローラー

public function postGiftAction(Request $request){
    $this->denyAccessUnlessGranted('ROLE_USER');

    $gift = new GiftList();
    $form = $this->createForm(GiftListType::class , $gift, ['csrf_protection' => false]);
    $form->submit($request->request->all());
    if($form->isValid()) {

        $em = $this->getDoctrine()->getManager();
        $em->persist($gift);
        $em->flush();
        $view = $this->view($gift, 200);
    }else{
        $view = $this->view(['form' => $form], 400);
    }

    return $this->handleView($view);
}

要約すると、保存する画像のタイトルを取得できますが、画像自体と最終的な画像名は保存されません。これについて何か助けていただければ幸いです。

4

0 に答える 0