symfony sonata admin roles on embedded onetomany entities - symfony

I have Symfony 2.3 + Sonata Admin + Sonata User Bundle.
I have created an entity Student, and another entity Contact. An Student have one-to-many relationship with Contact. I have added Contact to Student with sonata_type_collection in my StudentAdmin class. I also have created a group of users Operator and assigned all permissions to Student, but only list and view to Contact.
My problem is that any user of Operator can't add or delete Contact (from Student edit page), but they can edit (and values are saved).
Any suggestions or examples?
Some code:
Roles assigned:
ROLE_SONATA_ADMIN_STUDENT_EDIT
ROLE_SONATA_ADMIN_STUDENT_LIST
ROLE_SONATA_ADMIN_STUDENT_CREATE
ROLE_SONATA_ADMIN_STUDENT_VIEW
ROLE_SONATA_ADMIN_STUDENT_DELETE
ROLE_SONATA_ADMIN_CONTACT_LIST
ROLE_SONATA_ADMIN_CONTACT_VIEW
ROLE_ADMIN: ROLE_USER, ROLE_SONATA_ADMIN
/**
* #ORM\OneToMany(targetEntity="MyBundle\Entity\Contact",
mappedBy="student",
cascade={"persist", "remove"})
**/
private $contact;
->add('contact', 'sonata_type_collection',
array(
'label' => 'Contact',
'by_reference' => false,
),
array(
'edit' => 'inline',
'inline' => 'table',
))
Thanks!

I understood your problem and I don't think Sonata handle this by default.
You have to check the current user roles and either remove contact fields or add readonly or disabled attribute on the contact fields.
Remove Contact Fields
protected function configureFormFields(FormMapper $formMapper)
{
// check if current user has role contact edition
$hasContactRole = $this->getConfigurationPool()->getContainer()->get('security.context')->isGranted('ROLE_SONATA_ADMIN_CONTACT_EDIT'));
if ($hasContactRole) {
$formMapper->add('contact', 'sonata_type_collection',
array(
'label' => 'Contact',
'by_reference' => false,
),
array(
'edit' => 'inline',
'inline' => 'table',
)
);
}
}

Related

how to embed one to many sonata admin child views in ConfigeShowFields

I have a one to many relationship between account and contacts. I use sonata admin bundle
I want to display all contacts of an account in the view detail of an account ( ConfigureShowFields in AccountAdmin class)
in class AcountAdmin.php i have :
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
# .......
->with('Liste des contacts', array('class' => 'col-md-12'))
->add('contacts')
->end()
;
}
I believe you can do this via sonata_type_collection.
->add('contacts', 'sonata_type_collection', array(
'associated_property' => 'email',
'route' => array(
'name' => 'show'
),
'admin_code' => 'app.admin.contacts',
))
The associated_property is the associated property found in the Contacts entity, and the admin_code is the contacts admin.

SonataAdmin sonata_type_model with lot of rows throw OutOfMemoryException

I have a simple one to many relation Listing -> Booking with many thousands listings.
When i add the following SonataAdmin class :
class BookingAdmin extends Admin
{
...
$formMapper
->add(
'listing',
null,
array(
'disabled' => true,
)
),
...
An OutOfMemoryException is thrown due to the lot of numbers of listings.
I would like to know how to avoid this error by displaying the listing title in the form without using the choice list.
You could use a 'sonata_type_model_autocomplete' form type for these cases (Ref.):
class BookingAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
// the dropdown autocomplete list will show only Booking
// entities that contain specified text in "title" attribute
$formMapper->add('listing', 'sonata_type_model_autocomplete', array(
'property' => 'title'
));
}
}
This one avoids to query all rows to populate the widget.
I found an another solution than Yonel one.
In this solution we only get the current Listing of the persisted Booking entity in the choice widget. It is only useful if the Listing must not be changed.
class BookingAdmin extends Admin
{
...
protected function configureFormFields(FormMapper $formMapper)
{
$listing= $this->getSubject();
$formMapper
->add(
'listing',
'sonata_type_model',
array(
'query' => $this->modelManager
->getEntityManager('Bundle:Listing')
->getRepository('Bundle:Listing')
->find(
$listing->getId()
),
'disabled' => true,
)
);
...

Symfony CMF with multiple images on Product entity

I'm currently trying to make Symfony CMF and Sonata allow the admin user to add multiple images to a product through the same view panel. I have my Product and Image entities setup and working individually with their ORM relationships too.
Product:
class Product extends AbstractEntity
{
/**
* #ORM\OneToMany(targetEntity="Image", mappedBy="product", cascade={"persist"})
*/
protected $images;
// ...
Image:
class Image extends AbstractEntity
{
/**
* #ManyToOne(targetEntity="Product", inversedBy="image", cascade={"persist"})
* #JoinColumn(name="product_id", referencedColumnName="id")
**/
private $product;
// ...
Rather than having the user add all the images first and then link to them, I want it to be achievable from the same view. So in my ProductAdmin class I have added the following:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Product')
->add('name', 'text', array('required' => true))
// everything else ...
->add('images', 'sonata_type_collection', array(
'type_options' => array('delete' => false),
), array(
'edit' => 'inline',
'inline' => 'table',
))
->end();
}
Currently this allows me to upload images from the product management page. However it does not create a link between the product and image and I'm not sure what I need to do to get it to do that.
UPDATE
Having read countless articles across StackOverflow and the wider web the common response I seem to be seeing is that the relation is only stored when the sonata_type_collection is used for the Admin page of the Entity which holds the relationship (in my case that would be the image). I can understand why it is that way, but it would be much better from a user perspective to work the other way round also (as I'd expect to add images to a product, and not add images and then add a product to them).
I'll leave this question open in case anyone has/does figure out a workaround.
WORKING
I've managed to get it working. Firstly I have changed my relationship to a Many-to-Many (so that images can be re-used for other products as my system contains versions), however I do not believe this is the fix itself, but worth noting none the less.
What I think has made it work is the inclusion of the by_reference attribute:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Product')
->add('name', 'text', array('required' => true))
// everything else ...
->add('images', 'sonata_type_collection', array(
'by_reference' => false,
'type_options' => array('delete' => false),
), array(
'edit' => 'inline',
'inline' => 'table',
))
->end();
}
So my issue was because I was missing 'by_reference' => false: http://symfony.com/doc/current/reference/forms/types/collection.html#by-reference
Similarly, if you're using the collection form type where your underlying collection data is an object (like with Doctrine's ArrayCollection), then by_reference must be set to false if you need the adder and remover (e.g. addAuthor() and removeAuthor()) to be called.
Or in my case addImage() and removeImage().

Add roles in FOSUserBundle

I feel the same as a forum user who posted this:
I implemented FOSUserBundle, and I want to add to RegisrationFormType roles that are taken from a table. When I had it like this:
->add('roles', 'choice', array('label' => 'Rol', 'required' => true,
'choices' => array( 'ROLE_ADMIN' => 'ADMINISTRADOR','ROLE_SUPERADMIN' => 'SUPERADMINISTRADOR',
'ROLE_USER' => 'USUARIO'), 'multiple' => true))
And it works! But they must leave the BD, I can not put the Entity field because roles should be an array, not an object. How I can generate the array with the roles taken from a table? In FosUSerbundle as you would add roles?
Thanks ....
I write because that user had no answer. I followed [the steps of official documentation] (https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md) and adding the above lines in the register of FOSUserBundle works, but I want to work this from the database.
And then I used to create groups this. Two additional tables were created and even now joined a group or role in the list, but not how to show the login to register a new user.
Has anyone solved it?
So you have the roles in a table? You could inject the EntityManager in the form type and use it to fetch the choices.
->add('roles', 'choice', array(
'label' => 'Rol',
'choices' => $this->getRoles(),
'multiple' => true,
'required' => true,
))
Then create a method which gives you the data the way you need.
Something like:
public function getRoles()
{
$roles = array();
$er = $this->em->getRepository('AppBundle:Role');
$results = $er->createQueryBuilder('r')
// conditions here
->getQuery()
->getResult();
// process the array?
foreach ($results as $role) {
$roles[$role->getId()] = $role->getLabel();
}
return $roles;
}

Use sonata_type_collection inside custom type

What I want to do is add sonata_type_collection to my custom formType.
Normal way is add sonata_collection_type to $formMaper inside AdminClass like:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('elements, 'sonata_type_collection', array(
'some_options' => 'options'
))
}
It work perfect, but i have my custom form type, and when i defined it like:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$formMapper->add('elements, 'sonata_type_collection', array(
'some_options' => 'options'
))
}
It doesn't work (it appear only label of filed). Problem is wrong template, so I tried to set formAdminTemplate
I made it by set template in view
{% form_theme formElement 'SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig' %}
Problem is sonata_admin variable inside this 'formTheme'. This variable doesn't exist in my form.
Of course my form type is related to admin class but i don't know how could I I tell symfony about this relation
You need an admin class for your collection child :
$formMapper->add('customizations', 'sonata_type_collection',
array(
'required' => true,
'type_options' => array('delete' => true),
'by_reference' => false,
'mapped' => true
),
array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
'targetEntity' => '/path/to/Entity/Customization',
'admin_code' => 'my.service.customization_admin'
)
);
I find solution. Instead using my custom type, I defined form using admin class. I need this form outside admin so it was little difficult.
First of all in my controller i get admin class from service. Inside admin class I override 3 methods which are use to create form
public function getFormBuilder()
public function defineFormBuilder(FormBuilder $formBuilder)
public function buildForm()
then i had to save my entity by sonata admin way. using create method instead handleRequest.

Resources