Update entity Many-to-Many relation SonataAdmin - symfony

I have 5 Entities
Entity\Book:
class Book
{
/**
* #var integer
*
* #ORM\Column(name="b_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $bId;
/**
* #var string
*
* #ORM\Column(name="b_foto", type="string", length=255, nullable=true)
*/
private $bFoto;
/**
* #var string
*
* #ORM\Column(name="b_title", type="string", length=255, nullable=false)
*/
private $bTitle;
public $author;
public $heading;
}
Entity\Author
/**
* Author
*
* #ORM\Table(name="author")
* #ORM\Entity
*/
class Author
{
/**
* #var integer
*
* #ORM\Column(name="a_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $aId;
/**
* #var string
*
* #ORM\Column(name="a_firstname", type="string", length=100, nullable=false)
*/
private $aFirstname;
/**
* #var string
*
* #ORM\Column(name="a_lastname", type="string", length=100, nullable=false)
*/
private $aLastname;
}
Entity\Heading
/**
* Heading
*
* #ORM\Table(name="heading")
* #ORM\Entity
*/
class Heading
{
/**
* #var integer
*
* #ORM\Column(name="h_id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $hId;
/**
* #var string
*
* #ORM\Column(name="h_title", type="string", length=100, nullable=false)
*/
private $hTitle;
}
Entity\BooksAuthor
/**
* BooksAuthor
*
* #ORM\Table(name="books_author", indexes={#ORM\Index(name="book_in", columns={"b_id"}), #ORM\Index(name="author_in", columns={"a_id"})})
* #ORM\Entity
*/
class BooksAuthor
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \AppBundle\Entity\Book
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Book")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="b_id", referencedColumnName="b_id")
* })
*/
private $b;
/**
* #var \AppBundle\Entity\Author
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Author")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="a_id", referencedColumnName="a_id")
* })
*/
private $a;
}
Entity\BooksHeading
class BooksHeading
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \AppBundle\Entity\Heading
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Heading")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="h_id", referencedColumnName="h_id")
* })
*/
private $h;
/**
* #var \AppBundle\Entity\Book
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Book")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="b_id", referencedColumnName="b_id")
* })
*/
private $b;
}
I use SonataAdmin and when I update my Entity\Book I want to update and another Entities.
That`s my BookAdmin
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
class BookAdmin extends Admin
{
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('bTitle', null , array('label' => 'Название'))
->add('author', 'sonata_type_model', array('class' => 'AppBundle\Entity\Author',
'label'=>"Авторы",
'multiple'=>true,
'required'=>false,
'multiple'=>true,
'expanded'=> true,
'by_reference' => false,),
array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position')
)
->add('heading', 'sonata_type_model', array('class' => 'AppBundle\Entity\Heading',
'label'=>'Рубрика',
'required'=>false,
'multiple'=>true,
'expanded'=> true,
'by_reference' => false,),
array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'id',))
->add('bFoto','file',array('label'=>'Обложка',
'data_class'=> null,
'required'=>false,
))
;
}
// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
//->add('author', null , array('label' => 'Автор',
// 'class' => 'AppBundle\Entity\BookAuthor',
// ))
//->add('heading', null, array('label' => 'Рубрика',
//'class' => 'AppBundle\Entity\BookHeading',
//))
;
}
// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('b_id', null, array('label' => 'ID'))
->add('bTitle', null , array('label' => 'Название'))
;
}
}
Can you help me?

You need to indicate the cascade type in your entities BooksHeading and BooksAuthor,
Try this:
#ORM\ManyToOne(targetEntity="AppBundle\Entity\Heading", casccade="persist")

Related

Symfony 4 form Many to Many using join table with extra attributes

I cannot seem to figure out the way to embed forms together, I have used this SO question to try and get close but all I get is an out of memory error.
On the creation of a Buyer I want to add Contacts at the same time. I see examples of checkboxes for this but I want input fields and a contact might not exist.
Also contacts is a general storage for any contact to which they might be part of different buyers / sellers.
/**
*
* #ORM\Table(name="buyer")
* #ORM\Entity
*/
class Buyer
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=30, nullable=false)
*/
private $name;
/**
* #var string|null
*
* #ORM\Column(name="address", type="string", length=60, nullable=true)
*/
private $address;
/**
* #var string|null
*
* #ORM\Column(name="city", type="string", length=60, nullable=true)
*/
private $city;
/**
* #var string|null
*
* #ORM\Column(name="state", type="string", length=20, nullable=true)
*/
private $state;
/**
* #var int|null
*
* #ORM\Column(name="zip", type="integer", nullable=true)
*/
private $zip;
/**
* #ORM\OneToMany(targetEntity="App\Entity\BuyerContact", mappedBy="buyer")
*/
protected $contacts;
public function __construct()
{
$this->contacts = new ArrayCollection();
}
...
/**
*
* #ORM\Table(name="buyer_contact", indexes={#ORM\Index(name="fk_buyer", columns={"buyer"}), #ORM\Index(name="fk_contact", columns={"contact"})})
* #ORM\Entity
*/
class BuyerContact
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var Buyer
*
* #ORM\ManyToOne(targetEntity="App\Entity\Buyer", inversedBy="contacts")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="buyer", referencedColumnName="id")
* })
*/
private $buyer;
/**
* #var Contact
*
* #ORM\ManyToOne(targetEntity="App\Entity\Contact")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="contact", referencedColumnName="id")
* })
*/
private $contact;
...
/**
*
* #ORM\Table(name="contact")
* #ORM\Entity
*/
class Contact
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string|null
*
* #ORM\Column(name="job_title", type="string", length=30, nullable=true)
*/
private $jobTitle;
/**
* #var string|null
*
* #ORM\Column(name="name", type="string", length=60, nullable=true)
*/
private $name;
/**
* #var string|null
*
* #ORM\Column(name="email", type="string", length=120, nullable=true)
*/
private $email;
class BuyerType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('buyer', TextType::class,[
'required'=>true,
'label'=>'Buyer',
'help'=>'The business name of the buyer'
])
->add('address', TextType::class, [
'required'=>false,
'label'=>'Address 1',
'help'=>'Address line one.'
])
->add('city', TextType::class, [
'required'=>false,
'label'=>'City',
])
->add('state', TextType::class,[
'required'=>false,
'label'=>'State',
])
->add('zip', NumberType::class, [
'required'=>false,
'label'=>'Zip',
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Buyer::class,
]);
}
}
class BuyerContactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('buyer')
->add('contact')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => PublisherContact::class,
]);
}
}
if i understand correctly what you want is to embed form fields:
Basically, what you need is to create 2 different forms, and in the one you want to embed the fields add them like this (you have to put this code in BuyerType form class):
$builder->add('field', BuyerContactType::class, [
// ...
]);
Where BuyerContactType::class is the form that contains the fields you want to embed. (or viceversa BuyerContactType to BuyerType)
Hope it helps, don't know if understand the question correctly
This was my mistake as the entities were generated for an existing application. All I had to do was configure the ManyToMany unidirectional relationship between the Buyer and contacts and everything works as intended.

Sonata Admin Bundle: Dependencies & sonata_type_model_list

I have two Entities
Contacts:
<?php
namespace AppBundle\Entity;
use...
/**
* #ORM\Entity
* #UniqueEntity(
* fields={"client", "name", "surname"},
* errorPath="name",
* message="This contact is already in use for this client."
* )
* #ORM\Table(name="contact")
*/
class Contact
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id()
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var integer
* #Assert\NotBlank()
* #ORM\ManyToOne(targetEntity="Client")
* #ORM\JoinColumn(name="client_id", referencedColumnName="client_id", nullable=false)
*/
private $client;
/**
* #ORM\Column(type="string", length=255, options={"default": ""}, nullable=true)
* #Assert\NotBlank()
*/
protected $name;
/**
* #ORM\Column(type="string", length=255, options={"default": ""}, nullable=true)
* #Assert\NotBlank()
*/
protected $surname;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Email")
* #ORM\JoinColumn(name="email_id", referencedColumnName="id")
*/
protected $email;
...
}
and Emails:
class Email
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id()
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity="Client")
* #ORM\JoinColumn(name="client_id", referencedColumnName="client_id")
*/
private $client;
/**
* #var string
*
* #ORM\Column(name="email", type="string", nullable=false)
* #Assert\Email(
* message = "The email '{{ value }}' is not a valid email.",
* checkMX = true
* )
*/
protected $email;
...
}
I am using the "sonata admin bundle" to generate the GUIs.
For the ContactAdmin i have the following snippet:
...
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('email', 'sonata_type_model_list', array(
// 'query' => $query,
'label' => 'E-Mail',
'compound' => true,
'by_reference' => true,
))
The problem: When I add a new entry or choose one entry from the list - I do not want to select the "client" again. It has similar to the already choosen one.
I was able to access the pcode-Parameter in order to see if I have a parent class (Contact-Class for the Email-Class) - but how can I access the data which client has been choosen/selected by the parent-class?

Symfony change property name

I have two entities:
/**
* Device
*
* #ORM\Table(name="device", indexes={#ORM\Index(name="referente_id", columns={"referente_id"})})
* #ORM\Entity
*/
class Device
{
/**
* #var string
*
* #ORM\Column(name="id_device", type="string", length=60)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idDevice;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="Acme\BasicCmsBundle\Entity\Carrier", mappedBy="device")
*/
private $carrier;
/**
* Carrier
*
* #ORM\Table(name="carrier")
* #ORM\Entity
*/
class Carrier
{
/**
* #var string
*
* #ORM\Column(name="rag_soc", type="string", length=200, nullable=true)
*/
private $ragSoc;
/**
* #var string
*
* #ORM\Column(name="via", type="string", length=150, nullable=true)
*/
private $via;
/**
* #var string
*
* #ORM\Column(name="nr", type="string", length=8, nullable=true)
*/
private $nr;
/**
* #var string
*
* #ORM\Column(name="citta", type="string", length=100, nullable=true)
*/
private $citta;
/**
* #var string
*
* #ORM\Column(name="naz", type="string", length=5, nullable=true)
*/
private $naz;
/**
* #var string
*
* #ORM\Column(name="tel", type="string", length=20, nullable=true)
*/
private $tel;
/**
* #var integer
*
* #ORM\Column(name="id_carrier", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idCarrier;
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="Acme\BasicCmsBundle\Entity\Device", inversedBy="carrier")
* #ORM\JoinTable(name="carrier_device",
* joinColumns={
* #ORM\JoinColumn(name="carrier_id", referencedColumnName="id_carrier")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="device_id", referencedColumnName="id_device")
* }
* )
*/
private $device;
The entities have a many-to-many relation. I have created the admin page:
class CarrierAdmin extends Admin
{
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('rag_soc', 'string')
->addIdentifier('citta', 'string')
->add('device', null, array('associated_property' => 'id_device'))
->add('_action', 'actions', array(
'actions' => array(
'view' => array(),
'edit' => array(),
'delete' => array(),
)
));
}
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Carrier Information', array('class' => 'col-md-8', 'box_class' => 'box box-solid box-danger', 'description' => 'Lorem ipsum',))
->add('rag_soc', 'text')
->add('citta', 'text')
->end()
->with('Devices', array('class' => 'col-md-8', 'box_class' => 'box box-solid box-danger', 'description' => 'Lorem ipsum',))
->add('device', 'sonata_type_model_autocomplete', array('property'=>'idDevice', 'multiple' => true,))
->end();
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('ragSoc');
$datagridMapper->add('citta', null, array('show_filter'=>true))
->add('device');
}
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper->add('rag_soc');
}
class DeviceAdmin extends Admin
{
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('idDevice', 'string');
}
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('form.group_general', array('class' => 'col-md-8', 'box_class' => 'box box-solid box-danger', 'description' => 'Lorem ipsum',))
->add('idDevice', 'text')
->end();
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('idDevice');
}
public function getExportFormats()
{
return array();
}
}
In CarrierAdmin I define in the form the device item as sonata_type_model_autocomplete. My problem is that adding or deleting a device causes a "property not found" exception. Symfony changes the property name from Device to Devouce.
The error is in PropertyAccessor:writeProperty function, which invokes StringUtil::singularify($camelized) which changes the property name.
Is it a bug?
Temporarily I have solved it by adding the original property name to the result of StringUtil::singularify($camelized).

Sonata Admin sonata_type_collection

I have an issue with Sonata admin form...
I would like to insert music in a album with position...
My issue is when I create an album... I can add many as I want AlbumHasMusic... But when I submit my form... Each AlbumHasMusic have a null album whereas music and positions are ok.
How could I put album id to each AlbumHasMusic ?
These are my Entities :
Album:
/**
* Album
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppBundle\Entity\AlbumRepository")
* #ORM\HasLifecycleCallbacks()
*/
class Album
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="titre", type="string", length=255)
*/
private $titre;
/**
* #var UploadedFile
*/
private $cover;
/**
* #var String
*
* #ORM\Column(name="filename", type="string", length=255, nullable=true)
*/
private $covername;
/**
*
* #var Array<AlbumHasMusiques>
* #ORM\OneToMany(targetEntity="AppBundle\Entity\AlbumHasMusiques", mappedBy="album", cascade={"persist"})
*/
private $albumHasMusiques;
/**
* #var \DateTime
*
* #ORM\Column(name="updated", type="time", nullable=true)
*/
private $updated;
}
AlbumHasMusic:
/**
* AlbumHasMusiques
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppBundle\Entity\AlbumHasMusiquesRepository")
*/
class AlbumHasMusiques
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var Album
*
* #ORM\ManyToOne(targetEntity="Album")
* #ORM\JoinColumn(name="album_id", referencedColumnName="id")
*/
private $album;
/**
* #var Musique
*
* #ORM\ManyToOne(targetEntity="Musique")
* #ORM\JoinColumn(name="musique_id", referencedColumnName="id")
*/
private $musique;
/**
* #var integer
*
* #ORM\Column(name="position", type="integer")
*/
private $position;
}
Music:
/**
* Musique
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="AppBundle\Entity\MusiqueRepository")
* #ORM\HasLifecycleCallbacks()
*/
class Musique
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="duree", type="time", nullable=true)
*/
private $duree;
/**
* #var \DateTime
*
* #ORM\Column(name="updated", type="time", nullable=true)
*/
private $updated;
/**
* #var string
*/
private $file;
/**
* #var String
*
* #ORM\Column(name="filename", type="string", length=255, nullable=true)
*/
private $filename;
/**
* #var string
*
* #ORM\Column(name="titre", type="string", length=255, nullable=true)
*/
private $titre;
/**
* #var Genre
*
* #ORM\ManyToOne(targetEntity="Genre", inversedBy="musiques")
* #ORM\JoinColumn(name="genre_id", referencedColumnName="id", nullable=false)
*/
private $genre;
/**
* #ORM\ManyToMany(targetEntity="MotClef", inversedBy="musiques")
*/
private $motsClef;
}
I would like to do something like Galeries and media of Sonata Media Bundle.
And this is my AlbumAdmin:
class AlbumAdmin extends Admin
{
/**
* #param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('titre')
->add('covername')
->add('updated')
->add('albumHasMusiques', 'sonata_type_collection', array(
'cascade_validation' => true,
'by_reference' => false,
), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
'link_parameters' => array('context' => 'default'),
'admin_code' => 'app.admin.album_has_musiques',
)
)
;
}
/**
* {#inheritdoc}
*/
public function prePersist($album)
{
$album->setAlbumHasMusiques($album->getAlbumHasMusiques());
}
/**
* {#inheritdoc}
*/
public function preUpdate($album)
{
$album->setAlbumHasMusiques($album->getAlbumHasMusiques());
}
}
And AlbumHasMusicAdmin :
class AlbumHasMusiquesAdmin extends Admin
{
/**
* #param FormMapper $formMapper
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('musique', 'sonata_type_model_list', array('required' => true), array(
'link_parameters' => ['context' => 'default'],
))
->add('position', 'hidden')
;
}
}
Do you have any idea ?
I think that you missed something in the addAlbumHasMusiques() function in your album entity : make sure that you wrote it like that :
public function addAlbumHasMusiques(\AppBundle\Entity\AlbumHasMusiques $albumHasMusiques) {
$albumHasMusiques->setAlbum($this); // The important line !!!!
$this->albumHasMusiques[] = $albumHasMusiques;
return $this;
}

Show only user with a specific role in FormFields sonata

Here I am on a project for my studies and I use symfony fosUser and sonata, I have a Book entity that I administer with sonata.
The problem is that I want to give an user to a book, but it becomes complicated when I want to find in my field user only the user whose role ROLE_MyRole, I search on forums for several hours without finding.
I'm sorry for my incompetence I learn symfony recently and does not yet have enough experience.
This is my BookAdmin
class BookAdmin extends Admin
{
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
static $options = array();
$currentBook = $this->getSubject();
if (null !== $currentBook->getFileName())
$options = array(
'required' => false,
'help' => 'getWebPath().'">Download File : '.$currentBook->getFileName().'',
);
$formMapper
->add('title', null, array('label' => 'Titre : '))
->add('summary', null, array('label' => 'Résumé : '))
->add('category', null, array('label' => 'Catégorie : '))
->add('readers', null, array('label' => 'Lecteur(s) : '))
->add('file', 'file', $options)
;
}
And this is my Book entity with readers field
class Book
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
protected $title;
/**
* #var string
*
* #ORM\Column(name="summary", type="text")
*/
protected $summary;
/**
* #Assert\File(
* mimeTypes={"application/pdf", "application/x-pdf"},
* mimeTypesMessage="Only PDF"
* )
*/
protected $file;
/**
* #var string
*
* #ORM\Column(name="file_path", type="string", length=255)
*/
protected $fileName;
/**
* #var \DateTime
*
* #Gedmo\Timestampable(on="create")
* #ORM\Column(name="created_at", type="datetime")
*/
protected $createdAt;
/**
* #var \DateTime
*
* #Gedmo\Timestampable(on="update")
* #ORM\Column(name="updated_at", type="datetime")
*/
protected $updatedAt;
/**
* #var bool
*
* #ORM\Column(name="enabled", type="boolean", nullable=true)
*/
protected $enabled;
/**
* #var bool
*
* #ORM\Column(name="received_by_reader", type="boolean", nullable=true)
*/
protected $receivedByReader;
/**
* #var bool
*
* #ORM\Column(name="download_by_reader", type="boolean", nullable=true)
*/
protected $downloadByReader;
/**
* #var bool
*
* #ORM\Column(name="send_by_reader", type="boolean", nullable=true)
*/
protected $sendByReader;
/**
* #var bool
*
* #ORM\Column(name="reader_validation", type="boolean", nullable=true)
*/
protected $readerValidation;
/**
* #var bool
*
* #ORM\Column(name="edited", type="boolean", nullable=true)
*/
protected $edited;
/**
* #var User
*
* #ORM\ManyToOne(targetEntity="Application\Sonata\UserBundle\Entity\User")
*/
protected $author;
/**
* #var ArrayCollection
*
* #ORM\ManyToMany(targetEntity="Application\Sonata\UserBundle\Entity\User", cascade={"persist"})
*/
protected $readers;
Thanks You
You can do something like this in your admin class:
$formMapper
->add('readers', null, array(
'label' => 'Lecteur(s) : ',
'code' => 'getReadersWithMyRole'
))
and define in your User entity the following method:
/**
* Returns readers with "ROLE_MyRole" role
*
* #return array
*/
public function getReadersWithMyRole()
{
$result = array();
foreach ($this->getReaders() as $reader) {
if ($reader->hasRole('ROLE_MyRole')) {
$result[] = $reader;
}
}
return $result;
}
Hope it helps.
it doesn't work too but we have find a solution, we can do that like this :
->add('reader', null, array(
'class' => 'ApplicationSonataUserBundle:User',
'label' => 'Lecteur : ',
'query_builder' => function (EntityRepository $er) {
$qb = $er->createQueryBuilder('u');
$qb->where($qb->expr()->like('u.roles', $qb->expr()->literal('%ROLE_READER%')));
return $qb;
}))
if it can help someone

Resources