symfony对象图像多对多

时间:2017-05-19 12:32:06

标签: symfony

我有一个数据库,需要有一个对象的多个图像。 在列表视图中,我看到多个图像,但它出现在texte中,我需要有图像!!!

这是我的产品实体的列表映射器(一个带有许多图像的产品)

 /**
 * @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';

  }

}

感谢帮助我,

我需要看到我的照片(图片)而不是文字!!!

我的学校(产品)的学说

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

1 个答案:

答案 0 :(得分: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 %}