Reputation: 243
I have two entities Ventes and Article with many to many relationship and in the entity relation is ventes_article that have the attribut "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;
}
#.............................
#..........
}
and this is my entity Article :
<?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;
}
}
and in my class VentesAdmin
I have :
<?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)
{
}
}
in result :
but I want to show this result with costum attributes for same Article :
can you help me ??
Upvotes: 2
Views: 2380
Reputation: 863
So, in this case we have a collection.
Predicate:
Each Article can belongs to different Venteses and One Ventes contains a lot of different articles.
This is Many-To-Many. And each article has different attributes. For this purposes we have to use
sonaty_type_collection
example for the configureFormFields
function:
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'),
])
;
}
}
You will have ability to add as many articles or whatever you want and with any number of attributes. See screens:
Upvotes: 1