【问题标题】:Many to many with attributes (sonata_type_collection) in sonata admin bundle奏鸣曲管理包中具有属性(sonata_type_collection)的多对多
【发布时间】:2017-05-31 11:01:56
【问题描述】:

我有两个实体 Ventes 和 Article 具有多对多关系,在实体关系中是 ventes_article 具有属性“quantite”:

    <?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)
    {

    }

}

结果:

但我想用同一篇文章的服装属性来显示这个结果:

你能帮帮我吗??

【问题讨论】:

  • 不确定 Sonata,但 Symfony 和 Doctrine 不是为此而构建的(相反,使用类似 Ventes VentesArticle(自己的实体) 文章)
  • 我正在使用它(VentesArticle)
  • 不,Ventes 和 Article 之间有 ManyToMany,Ventes 和 VentesArticle 之间需要 ManyToMany,VentesArticle 和 Article 之间需要 ManyToMany。但是我对奏鸣曲没有经验,所以我不是最好的帮助
  • 类名最好使用单数名词。即使您想保留法语名称,最好使用class Vente 而不是class Ventes。或者用英文叫它class Saleclass Article 中的属性应该保留ventes,因为它是多对多关系。

标签: php symfony many-to-many sonata-admin symfony-sonata


【解决方案1】:

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

谓词:

每篇文章可以属于不同的 Ventes,一个 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'),
              ])
         ;
    }
}

您将能够添加任意数量的文章或任何您想要的内容以及任意数量的属性。查看屏幕:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 2015-04-20
    • 2015-07-22
    • 2018-04-19
    • 2017-06-03
    • 2018-08-11
    • 2017-04-13
    相关资源
    最近更新 更多