2017-05-31 111 views
1

我有两个实体VENTES和文章有多对多的关系,并在实体关系是ventes_article是有attribut“quantite”:多对多与奏鸣曲管理包的属性(sonata_type_collection)

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Ventes 
* 
* @ORM\Table(name="ventes") 
* @ORM\Entity(repositoryClass="AppBundle\Repository\VentesRepository") 
*/ 
class Ventes 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    #...................... 



    /** 
    * Many Articles have Many paks. 
    * @ORM\ManyToMany(targetEntity="Article", inversedBy="Ventes") 
    * @ORM\JoinTable(name="ventes_article") 
    */ 
    private $articles; 





    /** 
    * Add article 
    * 
    * @param \AppBundle\Entity\Article $article 
    * 
    * @return Ventes 
    */ 
    public function addArticle(\AppBundle\Entity\Article $article) 
    { 
     $this->articles[] = $article; 

     return $this; 
    } 

    /** 
    * Remove article 
    * 
    * @param \AppBundle\Entity\Article $article 
    */ 
    public function removeArticle(\AppBundle\Entity\Article $article) 
    { 
     $this->articles->removeElement($article); 
    } 

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

    #............................. 
    #.......... 
} 

和这是我的实体文章:

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Article 
* 
* @ORM\Table(name="article") 
* @ORM\Entity(repositoryClass="AppBundle\Repository\ArticleRepository") 
*/ 
class Article 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 


    /** 
    * Many packs have Many article. 
    * @ORM\ManyToMany(targetEntity="Ventes", mappedBy="Article") 
    * @ORM\JoinTable(name="ventes_article") 
    */ 
    private $ventes; 

    public function __construct() { 
     $this->ventes = new \Doctrine\Common\Collections\ArrayCollection(); 
     $this->packs = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 


#/............ 



/** 
* Add vente 
* 
* @param \AppBundle\Entity\Ventes $vente 
* 
* @return Article 
*/ 
public function addVente(\AppBundle\Entity\Ventes $vente) 
{ 
    $this->ventes[] = $vente; 

    return $this; 
} 

/** 
* Remove vente 
* 
* @param \AppBundle\Entity\Ventes $vente 
*/ 
public function removeVente(\AppBundle\Entity\Ventes $vente) 
{ 
    $this->ventes->removeElement($vente); 
} 

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

} 

,并在我的课Ventesadmin我有:

<?php 

namespace AppBundle\Admin; 

use Sonata\AdminBundle\Admin\AbstractAdmin; 
use Sonata\AdminBundle\Datagrid\ListMapper; 
use Sonata\AdminBundle\Show\ShowMapper; 
use Sonata\AdminBundle\Datagrid\DatagridMapper; 
use Sonata\AdminBundle\Form\FormMapper; 
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; 
use Sonata\AdminBundle\Route\RouteCollection; 

class VentesAdmin extends AbstractAdmin 
{ 



    protected function configureFormFields(FormMapper $formMapper) 
    { 
     $formMapper 


     ->with('Acticles', array('class' => 'col-md-12')) 
     ->add('articles', 'sonata_type_model', array(
      'class' => 'AppBundle\Entity\Article', 
      'property' => 'reference', 
      'multiple' => true, 
      'required' => false, 
     )) 


     ->end() 
     ; 
    } 

    protected function configureDatagridFilters(DatagridMapper $datagridMapper) 
    { 

    } 

    protected function configureListFields(ListMapper $listMapper) 
    { 

    } 



    protected function configureShowFields(ShowMapper $showMapper) 
    { 

    } 

} 

的结果:

enter image description here

,但我想说明这个结果与同条costum属性:

enter image description here

你能帮助我吗?

+0

不确定奏鸣曲,但Symfony和学说不建这个(相反,使用某物像VENTES <-> VentesArticle(自己的实体)<->条) – kero

+0

我使用它 (VentesArticle) –

+1

不,你在Ventes和Article之间有一个ManyToMany,你需要Ventes和Ventes Article之间的ManyToMany和VentesArticle和Article之间的ManyToMany。但我对索纳塔没有经验,所以我不是最好的帮手 – kero

回答

0

所以,在这种情况下,我们有一个集合。

谓:

每个项目可以属于不同Venteses和一个VENTES包含 了很多不同的文章。

这是多对多。 而每篇文章都有不同的属性。为了这个目的,我们必须使用

sonaty_type_collection

例如为configureFormFields功能:

class OwnerAdmin extends AbstractAdmin 
{ 

    // ... 

    protected function configureFormFields(FormMapper $formMapper) 
    { 
     $formMapper 
      ->add('articles', 'sonata_type_collection', [ 
        'required'  => false, 
        'label'   => 'Article', 
        'btn_add'  => true, 
        'btn_catalogue' => 'admin', 
        'type_options' => [ 
         'delete' => true, 
        ], 
       ], [ 
        'edit'   => 'inline', 
        'inline'  => 'table', 
        'sortable'  => 'id', 
        'allow_delete' => true, 
        'placeholder' => $this->trans('admin.placeholder.no_article'), 
       ]) 
     ; 
    } 
} 

您必须添加尽可能多的物品或任何你想要的,并与任意数量的能力的属性。见屏幕: enter image description here enter image description here

相关问题