Sonata - Use the repository of an entity in admin - symfony

I am working on a Symfony project, using Sonata.
Context:
I got different entities:
Product (ID, categories (relation), characteristicValues (relation))
Category (ID, characteristics (relation))
Characteristic (ID, id_category (relation), label).
CharacteristicValue (ID, id_product (relation), id_characteristic (relation), value)
Relations:
Product --OneToMany--> CharacteristicValue
Category -->OneToMany--> Characteristic
Characteristic -->OneToMany--> CharacteristicValue
Product --ManyToMany--> Category
Problem:
I need to get all characteristics of a the categories of a product (and their values if they're set) in the ProductAdmin, and show an input for each of them (like Characteristic1 : value1).
What I did:
I tried to call a function the CharacteristicValueRepository in the ProductAdmin, but the repository was not instantiated.
The code of ProductAdmin is really basic:
final class ProductAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Product information', ['class' => 'col-md-6'])
->add('name', TextType::class, [
'label' => 'Name of the product'
])
->add('categories', EntityType::class, [
'class' => Category::class,
'choice_label' => 'name',
'multiple' => true,
'label' => 'Categories of the product'
])
->end();
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('name');
$datagridMapper->add('categories');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->add('id');
$listMapper->addIdentifier('name');
$listMapper->addIdentifier('categories');
}
}
Notes:
I am using the last version of everything (Symfony, Sonata, ...)
If someone knows how to help me, I would be really grateful!

You need to configure custom form type for example ProductCharacteristicsType. While using Form Event listeners you could fetch all characteristics and form a proper collection. What you have here is EAV (Entity attribute value) model. It may cause confusion for Symfony, but it is manageable. On SonataAdmin you must use that custom type of yours.

Related

Sonata ODM Admin custom set of filters

I am trying to upgrade my sonata-admin section in project from version 2 to 3. As I understood, the system of filtering had a little bit changed.
The section of filtering now is like below:
/**
* Fields to be shown on filter forms
*
* #param DatagridMapper $datagridMapper
*/
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('updated_from', 'doctrine_mongo_callback', [
'callback' => function ($queryBuilder, $alias, $field, $value) {
if (!$value['value']) {
return;
}
$queryBuilder
->field('updated_at')
->gte(new \DateTime($value['value']));
return true;
}, 'label' => 'Updated at, from'])
->add('updated_to', 'doctrine_mongo_callback', [
'callback' => function ($queryBuilder, $alias, $field, $value) {
if (!$value['value']) {
return;
}
$queryBuilder
->field('updated_at')
->lte(new \DateTime($value['value']));
return true;
}, 'label' => 'Updated at, to'])
->add('role', 'doctrine_mongo_choice', [], ChoiceType::class, [
'label' => 'Role Name',
'operator_type' => HiddenType::class,
'field_options' => [
'choices' => $this->getFilter('role')
]
]);
}
Generally filters work, but I can not use only one filter separately.
When I am trying to filter by "updated_from" sonata ask me to choose other 2 filters and show me nothing. Then I choose those other 2 filters and everything work.
Can somebody help in this question? Thank you.
After investigation in this question, I came to decision.
When we set the second parameter at "add" method, sonata identify the filter type as "DefaultFilterType". Then Sonata assigns some default parameters to that filter. It influenced to the filter behavior.
But if I leave second parameter as "null", then filter will be looking for a type of "FieldType" (4th parameter). Then filter become a "ChoiceType" in my case and it works fine.
And one more important stuff, that "ChoiceType" should be from Symfony, not from Sonata.

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().

How to properly configure 'sonata_type_collection' field in Sonata Admin

In a nutshell:
When I am using 'sonata_type_collection' in OneToMany relationship I have to specify the other side of the relation, which in the "create action" still does not exist and in "update action" could be set, but it is also possible to specify entirely different parent.
More detailed explanation:
I am using Sonata Admin Bundle for the CRUD operations and lets say that I have only Post(id, title, content) and Tag(id, post_id, title) entities.
I would like to be able to add and remove tag entities while I am editing the Post entity, so I use 'sonata_type_collection' field.
This is the configureFormFields method from the PostAdmin class:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title')
->add('content')
->add('tags', 'sonata_type_collection', array(), array(
'edit' => 'inline',
'inline' => 'table'
))
))
;
}
The problem is that in the create form, when I add new tag I have to specify both post and title, but the Post still does not exist, so I am not able to add tags.
While I am editing the post I could add new tags, but for every one of them I have to explicitly set a post, and I am able for example to add a tag for entirely different post.
Could you tell me how to solve this problem?
You might want to set the by_reference option to false.
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title')
->add('content')
->add('tags', \Sonata\CoreBundle\Form\Type\CollectionType::class,
array('by_reference' => false),
array('edit' => 'inline',
'inline' => 'table'
)
);
}
[edit]
So it looks like the problem was coming from the Post entity which had to call tags' setPost() method from the addTag() method.
public function addTag($tag)
{
$tag->setPost($this);
$this->tags->add($tag);
return $this;
}

Sonata admin, custom query in filter

I'm using SonataAdminBundle and I have a question about filters in the class MyEntityAdmin.
I have a first function protected function configureFormFields(FormMapper $formMapper) for list all fields to be shown on create/edit forms.
If I have a field type entity, I can do something like this:
->add('commercial', 'entity', array(
'class' => 'MyBundle:User',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')
->groupBy('u.id')
->orderBy('u.id', 'ASC')
->setParameters(array(1 => 'Commercial'));
},)
)
But I have another function protected function configureDatagridFilters(DatagridMapper $datagridMapper) for Fields to be shown on filter forms, and I have do to the same thing, a custom query on an entity field type, but if I do the same, I have the error:
No attached service to type named `entity`
How can I do that ?
Filters configuration is quite different than form configuration in sonata admin bundle.
Look at documentation: http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/filter_field_definition.html
When you add new filter through configuratDataFilters it receives parameters: field name, filter type, filter configuration, form field type and form field configuration.
So if you want only to override query_buider for entity choice type you should try call like this:
->add('commercial', null, array(), 'entity', array(
'class' => 'MyBundle:User',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')
->groupBy('u.id')
->orderBy('u.id', 'ASC')
->setParameters(array(1 => 'Commercial'));
}
))

Resources