-1

Bundle VichUploader を使用して画像をアップロードしています。画像は Web ディレクトリに適切に保存されていますが、レンダリングしようとすると、次のエラーが発生します。

テンプレートのレンダリング中に例外がスローされました (「通知: 未定義のインデックス: エンティティ」)

構成.yml

vich_uploader:
    db_driver: orm
    mappings:
        product_image:
            uri_prefix:         /userprofile/pictures
            upload_destination: %kernel.root_dir%/../web/bundles/flyplatform/userprofile/pictures
            namer: vich_uploader.namer_uniqid
            inject_on_load:     false
            delete_on_update:   true
            delete_on_remove:   true

Post.php

<?php

namespace FLY\BookingsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Application\Sonata\UserBundle\Entity\User;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use JMS\SecurityExtraBundle\Annotation\Secure;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Form\Type\VichImageType;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Form\Extension\Core\Type\FileType;
/**
 * Post
 *
 * @ORM\Table(name="post")
 * @ORM\Entity(repositoryClass="FLY\BookingsBundle\Entity\PostRepository")
 * @Vich\Uploadable
 */
class Post
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */

    private $id;

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



    /**
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
     * of 'UploadedFile' is injected into this setter to trigger the  update. If this
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
     * must be able to accept an instance of 'File' as the bundle will inject one here
     * during Doctrine hydration.
     *
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
     *
     * @return User
     */
    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 User
     */
    public function setImageName($imageName)
    {
        $this->imageName = $imageName;

        return $this;
    }

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

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

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


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

}

PostType.php

$builder

    ->add('imageFile', 'vich_image', array(
    ))

new.html.twig

 {% for entity in entity %}
                                    <img src="{{ vich_uploader_asset(entity, 'imageFile') }}" alt="{{ product.name }}" />
                                    {% endfor %}

追加:

PostController.php

/**
     * Creates a new Post entity.
     *
     * @Route("/", name="post_create")
     * @Method("POST")
     * @Secure(roles="ROLE_USER")
     * @Template("FLYBookingsBundle:Post:new.html.twig")
     */
    public function createAction(Request $request)
    {
        $user = $user = $this->getUser();
        $entity = new Post();
        $entity->setEmail($user);
        $form = $this->createForm(PostType::class,$entity);
        $form->handleRequest($request);
        if ($form->isValid()) {
            $userManager = $this->container->get('fos_user.user_manager');
            $usr = $userManager->findUserByUsername($this->container->get('security.context')
                ->getToken()
                ->getUser());
            $entity->setUsername($usr);

            $user = $this->getUser();
            $entity->setUser($user);

            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('post_show', array('id' => $entity->getId())));
        }

        return array(
            'entity' => $entity,
            'form' => $form->createView(),
        );
    }



/**
 * Displays a form to create a new Post entity.
 *
 * @Route("/new", name="post_new")
 * @Method("GET")
 * @Template()
 */
public function newAction()
{
    $entity = new Post();
    $form = $this->createCreateForm($entity);



    return array( 'entity' => $entity,'form' => $form->createView());
}
4

2 に答える 2

0

1 つのアイテムをループする目的が見当たらないので、次のようにします。

   {% for entity in entity %}

私には意味がありません。一連のエンティティがある場合に通常行うこと:

   {% for entity in entities %}

With entities は、テンプレートに渡されるオブジェクトの配列です。あなたのコントローラーのコードは何ですか?テンプレートに渡すパラメーターを確認できるようにします。とはどういうproduct意味ですか?

エラーをスローするコード行は何ですか?

于 2016-07-17T18:38:52.603 に答える