2017-05-19 90 views
-1

我有一個數據庫,需要有一個對象的多個圖像。 在列表視圖中,我看到了多個圖像,但它出現在texte中,我需要有圖像!symfony對象圖像多對多

這是我的產品實體(一個produc很多圖片)

/** 
* @param ShowMapper $showMapper 
*/ 
protected function configureShowFields(ShowMapper $showMapper) 

{ 

    $showMapper 
     ->with('PRODUCT DESCRIPTION', array('class' => 'col-md-7')) 
     ->add('name') 
     ->add('country') 
     ->add('descriptionshortnat', null, array('label' => 'Description short (national)')) 
     ->add('descriptionlongnat', null, array('label' => 'Description long (national)')) 
     ->add('descriptionshorten', null, array('label' => 'Description short (english)')) 
     ->add('descriptionlongen', null, array('label' => 'Description long (english)')) 
     ->add('created') 
     ->add('updated') 
     ->end() 

     ->with('PICTURES', array('class' => 'col-md-5')) 
     ->add('image', 'string' , array("template"=>"AdminBundle::Admin/list_types/image.html.twig")) 
     ->end() 

     ->with('CLASSIFICATION', array('class' => 'col-md-5')) 
     ->add('category') 
     ->add('preservation', null, array('label' => 'Preservation method')) 
     ->add('population', null, array('label' => 'Population target')) 
     ->add('innovation') 
     ->add('timeframe', null, array('label' => 'Comsumer timeframe')) 
     ->add('component') 
     ->end() 
     ->with('PARTNERS', array('class' => 'col-md-5')) 
     ->end()    
     ->with('COMPETITION', array('class' => 'col-md-5')) 
     ->add('contact', 'sonata_type_model', array('required' => false, 'multiple' => true,'label' => 'Student team')) 
     ->add('organisation', 'sonata_type_model', array('required' => false)) 
     ->add('competition', 'sonata_type_model', array('required' => false, 'multiple' => true)) 
     ->end() 



    ; 
} 

}

列表映射器,這是我枝杈我的 '形象'

{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %} 

{% block field %} 
    <a href="{{ asset('uploads/documents/') }}{{ value|nl2br }}"><img class="admin-list-preview" src="{{ asset('uploads/documents/') }}{{ value|nl2br }}"/></a> 
{% endblock %} 

和我的班'形象'

<?php 

namespace AdminBundle\Entity; 

use Symfony\Component\HttpFoundation\File\UploadedFile; 

/** 
* Image 
*/ 
class Image 
{ 
    /** 
    * @var int 
    */ 
    private $id; 


    /** 
    * @var type 
    */ 
    private $file; 

    /** 
    * @var type 
    */ 
    private $fichier; 



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


public function __toString() 
{ 
return $this->fichier;  
} 


public function getname() 
{ 
return $this->fichier; 
} 




/** 
* Set fichier 
* 
* @param string $fichier 
* 
* @return Image 
*/ 
public function setFichier($fichier) 
{ 
    $this->fichier = $fichier; 

    return $this; 
} 

/** 
* Get fichier 
* 
* @return string 
*/ 
public function getFichier() 
{ 
    return $this->fichier; 

} 



/** 
* Set file 
* 
* @param string $file 
* @return Image 
*/ 
public function setFile(UploadedFile $file=null) 
{ 
    $this->file = $file; 

    return $this; 
} 

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





public function upload() { 
    // the file property can be empty if the field is not required 
    if(null === $this->getFile()) 
    { 
     return; 
    } 

    // we use the original file name here but you should 
    // sanitize it at least to avoid any security issues 

    // move takes the target directory and target filename as params 
    $this->getFile()->move(
     $this->getUploadRootDir(), 
     $this->getFile()->getClientOriginalName() 
    ); 

    // set the path property to the path where you've saved the file 
    $this->setFichier($this->getFile()->getClientOriginalName()); 

    // clean up the file property as you won't need it anymore 
    $this->setFile(null); 






} 



public function removeUpload() { 
    if($file = $this->getAbsolutePath()) 
    { 
     if(is_file($file)) 
     { 
      unlink($file); 
     } 
    } 

} 

public function getAbsolutePath() 
{ 
    return null === $this->getFichier() 
     ? null 
     : $this->getUploadRootDir().'/'.$this->getFichier(); 

} 

public function getWebPath() 
{ 
    return null === $this->getFichier() 
     ? null 
     : $this->getUploadDir().'/'.$this->getFichier(); 


} 

protected function getUploadRootDir() 
{ 
    // the absolute directory path where uploaded 
    // documents should be saved 
    return __DIR__.'/../../../web/'.$this->getUploadDir(); 

} 

protected function getUploadDir() 
{ 
    // get rid of the __DIR__ so it doesn't screw up 
    // when displaying uploaded doc/image in the view. 
    return 'uploads/documents'; 

    } 

}

感謝幫助我在此,

我要看看我的圖片(圖片)不是文本!我enity的

學說陽明海運(產品)

AdminBundle\Entity\Products: 
    type: entity 
    table: null 
    sortable: true 
    repositoryClass: AdminBundle\Repository\ProductsRepository 
    id: 
     id: 
      type: integer 
      id: true 
      generator: 
       strategy: AUTO 
    fields: 
     name: 
      type: string 
      length: 255 
     descriptionshortnat: 
      type: text 
      length: 500 
      nullable: true 
     descriptionlongnat: 
      type: text 
      length: 5000 
      nullable: true 
     descriptionshorten: 
      type: text 
      length: 500 
      nullable: true 
     descriptionlongen: 
      type: text 
      length: 5000 
      nullable: true 
     created: 
      type: datetime 
      nullable: true 
      gedmo: 
       timestampable: 
       on: create 
     updated: 
      type: datetime 
      nullable: true 
      gedmo: 
       timestampable: 
       on: update 
    lifecycleCallbacks: 
     prePersist: [ setCreatedAtValue,setUpdatedAtValue ] 
     preUpdate: [ setUpdatedAtValue ] 
    manyToOne: 
     country: 
      targetEntity: AdminBundle\Entity\Country 
      joinColumn: 
       name: country 
       referencedColumnName: id 
      nullable: TRUE 
     category: 
      targetEntity: AdminBundle\Entity\Category 
      joinColumn: 
       name: category 
       referencedColumnName: id 
      nullable: TRUE 
     preservation: 
      targetEntity: AdminBundle\Entity\Preservation 
      joinColumn: 
       name: preservation 
       referencedColumnName: id 
      nullable: TRUE 
     population: 
      targetEntity: AdminBundle\Entity\Population 
      joinColumn: 
       name: population 
       referencedColumnName: id 
      nullable: TRUE 
     innovation: 
      targetEntity: AdminBundle\Entity\Innovation 
      joinColumn: 
       name: innovation 
       referencedColumnName: id 
      nullable: TRUE 
     timeframe: 
      targetEntity: AdminBundle\Entity\Timeframe 
      joinColumn: 
       name: timeframe 
       referencedColumnName: id 
      nullable: TRUE 
     component: 
      targetEntity: AdminBundle\Entity\Component 
      joinColumn: 
       name: component 
       referencedColumnName: id 
      nullable: TRUE 
    manyToMany: 
     contact: 
      targetEntity: AdminBundle\Entity\Contact 
      joinTable: 
       name: allproducts 
       joinColumns: 
        products: 
         referencedColumnName: id 
       inverseJoinColumns: 
        contact: 
         referencedColumnName: id 
     organisation: 
      targetEntity: AdminBundle\Entity\Organisation 
      joinTable: 
       name: allorganisation 
       joinColumns: 
        product: 
         referencedColumnName: id 
       inverseJoinColumns: 
        organisation: 
         referencedColumnName: id 
     competition: 
      targetEntity: AdminBundle\Entity\Competition 
      joinTable: 
       name: allcompetition 
       joinColumns: 
        competition: 
         referencedColumnName: id 
       inverseJoinColumns: 
        product: 
         referencedColumnName: id 
     image: 
      targetEntity: AdminBundle\Entity\Image 
      joinTable: 
       name: allpictures 
       joinColumns: 
        product: 
         referencedColumnName: id 
       inverseJoinColumns: 
        image: 
         referencedColumnName: id 

轉儲值

SCREEN SHOT OF THE DUMP()

回答

0

如果你不會看到你的形象 - 使用這種結構:

{{ asset('uploads/documents/' ~ object.image) }} 
uploads/documents/ - folder to you directory with images. 
object.image - image name (twig variable) 

例如用SonataAdminBundle

{% for image in object.image %} 
    <img class="admin-list-preview" src="{{ asset('uploads/documents/' ~ image) }}"/> 
{% end for %} 
+0

謝謝,我試試這個,但它給了我這個消息錯誤:在呈現模板期間拋出異常(「Catchable致命錯誤:類Doctrine \ ORM \ PersistentCollection的對象無法轉換爲字符串」 )在AdminBundle :: Admin/list_types/image.html.twig第3行。 –

+0

@olivier toppin您可以顯示代碼? –

+0

你想要什麼代碼? –