duanchi4184 2015-08-06 14:56
浏览 66

Symfony Sonata Media捆绑一对多的关系

I have a project working with sonata AdminBundle and I want to use the mediaBundle for the upload. I have an entity prototype that can have many images. I would like to have a form so I can add many images before saving. I followed the steps in MediaBundle ~ MediaType improved But it doesn't work and I don't understand why !

EDIT : To be clear : I have 3 sections and I would like to be able to upload many images in each one of them.

Here is my prototype entity :

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

And in 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;
}
}

Here is prototypeAdmin.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(),
            )
        ))

    ;
}



}

Here is 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'))


;
}

}

I've tried the solution in Handling multiple file uploads in Sonata Admin Bundle but I still a problem : I can't have the possibility to upload in many places : I don't have the add button in the 'tablette' and 'mobile' section. I've just started using Symfony and sonata and I still feel confused.

here is what I have now :

enter image description here

Thank you for your help

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效
    • ¥15 悬赏!微信开发者工具报错,求帮改