Связь Symfony Sonata Media Bundle один ко многим

У меня есть проект, работающий с sonata AdminBundle, и я хочу использовать mediaBundle для загрузки. У меня есть прототип объекта, который может иметь много изображений. Я хотел бы иметь форму, чтобы я мог добавить много изображений перед сохранением. Я выполнил шаги, описанные в MediaBundle – улучшенный MediaType. это не работает, и я не понимаю, почему!

РЕДАКТИРОВАТЬ: Чтобы было ясно: у меня есть 3 раздела, и я хотел бы иметь возможность загружать много изображений в каждый из них.

Вот мой прототип сущности:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Prototype
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Entity\PrototypeRepository")
 * @Vich\Uploadable
 * @ORM\HasLifecycleCallbacks
 */
class Prototype
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

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


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

/**
 * @var \DateTime
 *
 * @ORM\Column(name="dateCreation", type="date")
 */
private $dateCreation;


/**
 * @ORM\Column(type="string", length=255, name="fichier_nom")
 *
 * @var string $nomFichier
 */
public $nomFichier;


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


   /**
* Unmapped property to handle file uploads
* @Vich\UploadableField(mapping="prototype_fichier", fileNameProperty="nomFichier")
*
* @var File $file
*/
private $file;


     /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Projet", inversedBy="prototypes")
     * @ORM\joinColumn(name="projet_id", referencedColumnName="id")
     */
private $projet;

/**
 * @Assert\NotBlank()
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\PrototypeHasMedia", mappedBy="prototype",cascade={"persist","remove"} )
 */
protected $links;


/**
 * Remove widgetImages
 *
 * @param \Application\Sonata\MediaBundle\Entity\Media $widgetImages
 */
public function removeLinks(\AppBundle\Entity\PrototypeHasMedia $links)
{
$this->links->removeElement($links);
}


/**
 * Get widgetImages
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getLinks()
{
return $this->links;
}


/**
 * {@inheritdoc}
 */
public function setLinks($links)
{
$this->links = new ArrayCollection();


foreach ($links as $prototype) {
    $this->addLinks($prototype);
}
}

/**
 * {@inheritdoc}
 */
public function addLinks(\AppBundle\Entity\PrototypeHasMedia $links)
{
$links->setPrototype($this);


$this->links[] = $links;
}


public function __construct()
{   
    $this->dateCreation =  new \DateTime("now");
    $this->nom = "";
    $this->description = " ";

}


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


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

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

/**
 * Set nom
 *
 * @param string $nom
 * @return Prototype
 */
public function setNom($nom)
{
    $this->nom = $nom;

    return $this;
}


/**
 * Set description
 *
 * @param string $description
 * @return Prototype
 */
public function setDescription($description)
{
    $this->description = $description;

    return $this;
}

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

/**
 * Set dateCreation
 *
 * @param \DateTime $dateCreation
 * @return Prototype
 */
public function setDateCreation($dateCreation)
{
    $this->dateCreation = $dateCreation;

    return $this;
}

/**
 * Get dateCreation
 *
 * @return \DateTime 
 */
public function getDateCreation()
{
    return $this->dateCreation;
}


/**
 * Set projet
 *
 * @param \AppBundle\Entity\Projet $projet
 * @return Prototype
 */
public function setProjet(\AppBundle\Entity\Projet $projet = null)
{
    $this->projet = $projet;
    return $this;
}

/**
 * Get projet
 *
 * @return \AppBundle\Entity\Projet 
 */
public function getProjet()
{
    return $this->projet;
}



/**
 * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
 *
 * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $file
 */
public function setFile(File $file = null)
{
    $this->file = $file;

    if ($file) {

        $this->updatedAt = new \DateTime('now');
    }
}

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

/**
 * @param string $nomFichier
 */
public function setNomFichier($nomFichier)
{
    $this->nomFichier = $nomFichier;
}

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


/**
 * Set updatedAt
 *
 * @param \DateTime $updatedAt
 * @return Prototype
 */
public function setUpdatedAt($updatedAt)
{
    $this->updatedAt = $updatedAt;

    return $this;
}

/**
 * Get updatedAt
 *
 * @return \DateTime 
 */
public function getUpdatedAt()
{
    return $this->updatedAt;
}





/**
 * Add links
 *
 * @param \AppBundle\Entity\PrototypeHasMedia $links
 * @return Prototype
 */
public function addLink(\AppBundle\Entity\PrototypeHasMedia $links)
{
    $this->links[] = $links;

    return $this;
}

/**
 * Remove links
 *
 * @param \AppBundle\Entity\PrototypeHasMedia $links
 */
public function removeLink(\AppBundle\Entity\PrototypeHasMedia $links)
{
    $this->links->removeElement($links);
}
}

И в PrototypeHasMedia:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;




/**
 * PrototypeHasMedia
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AppBundle\Entity\PrototypeHasMediaRepository")
 */
class PrototypeHasMedia
{

    /**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
* @var \Application\Sonata\MediaBundle\Entity\Media
* @Assert\NotBlank()
* @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"persist"}, fetch="LAZY")
* @ORM\JoinColumn(name="media_id", referencedColumnName="id")
*/
protected $media;


/**
 * @var \AppBundle\Entity\Prototype
* @Assert\NotBlank()
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Prototype", cascade={"persist","remove"} ,inversedBy="links", fetch="LAZY" )
 * @ORM\JoinColumn(name="prototype_id", referencedColumnName="id",nullable=true)
 */
protected $prototype;






/**
 * @var string
 * @ORM\Column(name="link", type="text")
 */
private $link;

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

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


/**
 * Set media
 *
 * @param \Application\Sonata\MediaBundle\Entity\Media $media
 * @return PrototypeHasMedia
 */
public function setMedia(\Application\Sonata\MediaBundle\Entity\Media $media = null)
{
    $this->media = $media;

    return $this;
}

/**
 * Get media
 *
 * @return \Application\Sonata\MediaBundle\Entity\Media 
 */
public function getMedia()
{
    return $this->media;
}



/**
 * Set prototype
 *
 * @param \AppBundle\Entity\Prototype $prototype
 * @return PrototypeHasMedia
 */
public function setPrototype(\AppBundle\Entity\Prototype $prototype = null)
{
    $this->prototype = $prototype;

    return $this;
}

/**
 * Get prototype
 *
 * @return \AppBundle\Entity\Prototype 
 */
public function getPrototype()
{
    return $this->prototype;
}

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

    /**
 * Set link
 *
 * @param string $link
 * @return PrototypeHasMedia
 */
public function setLink($link)
{
    $this->link = $link;

    return $this;
}

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

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

    return $this;
}

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

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

    return $this;
}

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

Вот прототипAdmin.php

<?php


namespace AppBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;





class PrototypeAdmin extends Admin
{   

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->with('Général')
            ->add('nom', 'text', array('label' => 'Nom'))
            ->add('description','text',array('label'=>'Description'))
            ->add('dateCreation', 'date', array('label' => 'Date de création'))

            ->add('projet','entity',array('class' => 'AppBundle\Entity\Projet'))
        ->end()

        ->with('Desktop')

           ->add('links', 'sonata_type_collection', array(
            'cascade_validation' => false,
            'type_options' => array('delete' => false),
        ), array(

            'edit' => 'inline',
            'inline' => 'table',
            'sortable' => 'position',
            'link_parameters' => array('context' => 'prototype'),
            'admin_code' => 'sonata.admin.prototype_has_media' /*here provide service name for junction admin */
        )
    )


        ->end()

        ->with('Tablette')

           ->add('links', 'sonata_type_collection', array(
            'cascade_validation' => false,
            'type_options' => array('delete' => false),
        ), array(

            'edit' => 'inline',
            'inline' => 'table',
            'sortable' => 'position',
            'link_parameters' => array('context' => 'prototype'),
            'admin_code' => 'sonata.admin.prototype_has_media' /*here provide service name for junction admin */
        )
    )
        ->end()


        ->with('Mobile')

            ->add('links', 'sonata_type_collection', array(
            'cascade_validation' => false,
            'type_options' => array('delete' => false),
        ), array(

            'edit' => 'inline',
            'inline' => 'table',
            'sortable' => 'position',
            'link_parameters' => array('context' => 'prototype'),
            'admin_code' => 'sonata.admin.prototype_has_media' /*here provide service name for junction admin */
        )
    )
        ->end()

        ->with('Dossier Complet')
            ->add('file', 'file', array('required' => false , 'label' => 'Dossier complet'))
        ->end()
     ;

}


protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        ->add('nom')
        ->add('dateCreation')
        ->add('projet.id')
    ;
}


protected function configureListFields(ListMapper $listMapper)
{   

    $listMapper
        ->add('nom')
        ->add('description')
        ->add('dateCreation')
        ->add('_action', 'actions', array(
                'actions' => array(
                'show' => array(),
                'delete' => array(),
            )
        ))

    ;
}



}

Вот PrototypeHasMediaAdmin

<?php


namespace AppBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;





class PrototypeHasMediaAdmin extends Admin
{ 

protected function configureFormFields(FormMapper $formMapper)
{
$link_parameters = array();

if ($this->hasParentFieldDescription()) {
    $link_parameters = $this->getParentFieldDescription()->getOption('link_parameters', array());
}

if ($this->hasRequest()) {
    $context = $this->getRequest()->get('context', null);

    if (null !== $context) {
        $link_parameters['context'] = $context;
    }
}

$formMapper

    ->add('media', 'sonata_type_model_list', array('required' => false), array(
        'link_parameters' => $link_parameters
    ))


    ->add('link', 'text', array('required' => false))
    ->add('commentaire', 'text', array('required' => false))
    ->add('typeDevice', 'text', array('required' => false))

    ->add('prototype','entity',array('class' => 'AppBundle\Entity\Prototype'))


;
}

}

Я пробовал решение в разделе Обработка нескольких загрузок файлов в Sonata Admin Bundle но у меня все еще проблема: у меня нет возможности загружать во многих местах: у меня нет кнопки добавления в разделе «планшет» и «мобильный». Я только начал использовать Symfony и сонату, и я все еще чувствую себя сбитым с толку.

вот что у меня сейчас:

введите здесь описание изображения

спасибо за помощь


person Lightning M    schedule 06.08.2015    source источник
comment
возможный дубликат Обработка нескольких загрузок файлов в Sonata Admin Bundle I надеюсь, что это решит вашу проблему, вы также можете найти ссылку github для примера кода в ответе   -  person M Khalid Junaid    schedule 06.08.2015
comment
@MKhalidJunaid Спасибо за ваш ответ. Я пытался перейти по ссылке, но все еще не могу решить свою проблему, поскольку я сказал, что хотел бы загрузить в каждый раздел: в «планшет» и «мобильный», например, в «рабочий стол».   -  person Lightning M    schedule 07.08.2015
comment
это всего лишь пример, теперь вы должны понимать, как Symfony и Sonata работают вместе, никто здесь не будет писать полный код для вашего приложения, вам просто нужно понять, как это работает, если мой предыдущий ответ может решить вашу проблему для 1 раздел, то вы можете использовать ту же логику для реализации этой функции и для других разделов, но попросите полный рабочий пример, я думаю, у вас нет много времени, чтобы написать его без обид, но попытайтесь изучить логику того, как он загружает несколько изображений в 1 раздел затем реализуйте для других разделов, я надеюсь, что я прояснил свою точку зрения   -  person M Khalid Junaid    schedule 07.08.2015
comment
Я не ожидаю, что кто-то напишет за меня код, мне просто нужна подсказка, чтобы понять, как это может работать! в любом случае спасибо за помощь   -  person Lightning M    schedule 07.08.2015