Sonata Admin listMapper - symfony

I am using Symfony2.6 with Sonata Admin.In my Entity "Order", i have an array column named "Products". this is "ArrayCollection" type and ManyToMany relation with Product Table
How I can show this field in Sonata Admin listMapper.
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id')
->add('products')
}
Right now in list It's showing Blank.So How I can show all Products in List Mapper in Order list.

I think you need to pass service name for products admin in 'admin_code' property. Result should look similar to this:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id')
->add('productProperty', null, array('','admin_code' =>'application.admin.product'))
}

Related

Sonata Admin Bundle loosing filter settings on edit

I created a standard simple admin for some entities, according to the Sonata handbook.
The problem is that the configured filter gets lost when editing an enity. Say I have set 3 filter values and then click on an entity to edit it. Neither "Save" nor the action "Back to list" brings me back to the filtered list. Even pagination starts from 1 again.
How can I keep the set filter?
This is an example admin class:
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
/**
* Description of OrtAdmin
*
* #author markus
*/
class OrtAdmin extends AbstractAdmin{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('name', 'text');
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('name');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('name', 'string');
}
//Remove some export formats
public function getExportFormats() {
return array(
'csv', 'xls'
);
}
//No batch actions
public function getBatchActions() {
$actions = parent::getBatchActions();
unset($actions['delete']);
return $actions;
}
}
Your filter gets lost after leaving the list view. Re-Opening the list (without getting back to the same URL) will always result in your pre-configured filters, which you can define on per-Admin-class.
Simply enable persistent filters per configuration. Please be aware, that those get persisted into your user's session, which mean they will only reset or change if you press the button "reset filters".
You can easily activate the option like this:
sonata_admin:
persist_filters: true
There is no dedicated documentation, but you can find the option in SonataAdmin Full Configuration Options.

Unable to find template "SonataAdminBundle:CRUD:list__action_show.html.twig"

Error showed :
Unable to find template "SonataAdminBundle:CRUD:list__action_show.html.twig" in SonataAdminBundle:CRUD:base_list_field.html.twig at line 23.
Configure entity for only show list(remove create, edit and delete routes) and showed this error, i find this template in sonata admin bundle but no exists, please help me with this issue.
There is really no template called: SonataAdminBundle:CRUD:list__action_show.html.twig
Your problem is not in routes, but in declaring wrong inline action in configureListFields method. The inline action should be called view so the SonataAdminBundle:CRUD:list__action_view.html.twig will be called.
The inline actions should be declared like this:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
//... some other fields ...
// add "show" link in each row of table
->add('_action', 'actions', array(
'actions' => array(
'view' => array(),
)
))
;
}

How to show a Entity value of type array on list action in Sonata Admin Bundle?

How Can I display array value on entitty in sonata admin bundle list action?
I've tried to do something like this:
->add('daysOfWeek', null, array('type' => 'array'))
in configureListFields method but then I get error:
"An exception has been thrown during the rendering of a template ("Notice: Array to string conversion"
You need to add a template for special fields, I don't think there is a such thing as "default" way of displaying array fields.
The anwser was to do it like so:
->add('daysOfWeek', 'array', array('template' => 'WshBackendBundle:EventAdmin:list_days_of_week.html.twig'))
The template method is working well but for simple entity you can use the 'collection' type
I have a Post entity with many tags.
So the $tags variable in the Post entity is collection of Tag.
Now in your PostAdmin:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('name')
...
->add('tags', ' collection')
...
}
Then you need a toString method in your Tag entity.
function __toString() {
return $this->getName();
}

Sonata admin - "order by" field in related table

I have a Product admin class. The Product entity has a many-to-one relationship with a Category entity, i.e. a product is associated with a category.
In the admin "list" page for products, I need to sort by the category name (alphabetically) that each product is associated with.
Setting the default field to sort by is easy if the field is on the entity itself (see Sonata admin bundle order for how to do this). But I cannot figure out how to sort by a field in a related table.
Any help is appreciated.
It seems a workaround, but it works. You have to add a join overriding createQuery() method, than assign a default sortBy overriding $datagridValues:
<?php
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
class ExpenseAdmin extends Admin
{
protected $datagridValues = array(
'_page' => 1,
'_sort_order' => 'ASC', // sort direction
'_sort_by' => 'c.name' // field name
);
/**
* #return \Sonata\AdminBundle\Datagrid\ProxyQueryInterface
*/
public function createQuery($context = 'list')
{
$query = parent::createQuery($context);
return new ProxyQuery($query
->join(sprintf('%s.category', $query->getRootAlias()), 'c'));
}
}
Asume name is the property of entity Category by wich you want to sort. You may do this in you ProductAdmin.php
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->add('category.name', null, array(
'sortable' => true,
));
...
}
This way you leverage the ordering links in the header of the list, generated by Sonata.
Edit
If you would also like to have a link on the category name in products list to quickly edit the Category entity, assuming you have created a CategoryAdmin class, you should write your code like this:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->add('category', null, array(
'sortable' => 'category.name',
));
...
}
And in your Category class you should implement the __toString() method like this:
public function __toString()
{
return $this->getName();
}

Symfony2 SontataAdminBundle change filter label

How to change filter label on AdminBundle? Example in documentations doesn't work
->add('tags', null, array('label' => 'les tags')
You're changing the label used with filters. Use configureListFields() method instead of configureDatagridFilters().
Edit: Also, you should use name instead of label.
/**
* #param Sonata\AdminBundle\Datagrid\ListMapper $listMapper
*
* #return null
*/
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('title')
->add('isPublished', null, array('name' => 'Is published?'))
;
}
Now, in the last version of Sonata, you should use label instead of name.
That seems more logic.

Resources