2016-07-17 16 views
-1

Ich benutze den Bundle VichUploader, um ein Bild hochzuladen. Das Bild wird speichern und im Web-Verzeichnis, aber wenn ich versuche, es zu machen, ich habe die folgende Fehlermeldung:Fehler beim Hochladen des Bildes VichUploaderBundle [symfony2.8]

An exception has been thrown during the rendering of a template ("Notice: Undefined index: entity")

config.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 %} 

ADD:

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()); 
} 

Antwort

0

Ich sehe keinen Zweck durch ein Element der Looping, so:

{% for entity in entity %} 

macht keinen Sinn für mich. Was ich noramlly tun, wenn ich einen Satz von Entitäten:

{% for entity in entities %} 

Mit Entitäten ist ein Array von Objekten an die Vorlage übergeben. Was ist der Code Ihres Controllers? damit wir die Parameter sehen können, die Sie an die Vorlage übergeben. Was bedeutet product?

Was ist die Codezeile, die den Fehler verursacht?

+0

Dies ist die Codezeile, die den Fehler auslöst: '{% für Entität in Entität%} {{ product.name }} {% endfor%}'. Ich habe meinen Controller oben auf meinem Post hinzugefügt. – Sirius

+0

Sogar '{% für Entität in Entität%}' kann syntaktisch korrekt sein, es hat keinen Sinn. Warum durch ein Element durchlaufen? Siehst du meine Antwort? –

+0

ok, da ich nur ein Bild rendern muss, brauche ich keine Schleife. Also sollte ich so etwas versuchen: '{{entity.imageName }}'? – Sirius

0

Ich habe endlich einen Weg gefunden, mein Problem zu beheben. Wenn ich das Element überprüfe, bemerke ich, dass der Pfad zum Bild wie folgt aussieht: /userprofile/pictures, aber eigentlich sollte es so aussehen: /symfony/web/bundles/flyplatform/userprofile/pictures/. Ich habe die uri_prefix: /userprofile/pictures zu diesem uri_prefix: /symfony/web/bundles/flyplatform/userprofile/pictures/

<img src="{{ vich_uploader_asset(entity, 'imageFile') }}" alt="{{entity.imageName }}" /> 

.

vich_uploader: 
    db_driver: orm 
    mappings: 
     product_image: 
      uri_prefix: /symfony/web/bundles/flyplatform/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 

ich hoffe, dass dies jemand in der Zukunft helfen wird.

Verwandte Themen