I'm quite new at Sonata. I have a proyect that involves Clients and Loans. In ClientsAdmin.php i have configured the configureRoutes and getPersistentParameters functions
protected function configureRoutes(RouteCollection $collection)
{
$collection->add('transacciones','transacciones/{id}');
}
public function getPersistentParameters()
{
if (!$this->getRequest()) {
return array();
}
return array(
'id' => $this->getRequest()->get('id'),
);
}
Also, i have override the CRUDController (and service.yml)
//service.yml
financiera.admin.clientes:
class: BitsMkt\FinancieraBundle\Admin\ClientesAdmin
arguments: [ ~,BitsMkt\FinancieraBundle\Entity\Clientes,FinancieraBundle:ClientesCRUD]
tags:
- {name: sonata.admin, manager_type: orm, group: Sistema, label: Clientes}
//ClientesCRUDController.php
namespace Bitsmkt\FinancieraBundle\Controller;
use Sonata\AdminBundle\Controller\CRUDController;
class ClientesCRUDController extends CRUDController
{
public function transaccionesAction($id = null)
{
//throw new \RuntimeException('The Request object has not been set ' . $id);
if (false === $this->admin->isGranted('LIST')) {
throw new AccessDeniedException();
}
$id = $this->get('request')->get($this->admin->getIdParameter());
if ($id == '*') {
# TODOS - Viene de Dashboard
}else
{
$object = $this->admin->getObject($id);
if (!$object) {
throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
}
$this->admin->setSubject($object);
}
$datagrid = $this->admin->getDatagrid();
$formView = $datagrid->getForm()->createView();
// set the theme for the current Admin Form
$this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
return $this->render('FinancieraBundle:Frontend:prestamos_clientes.html.twig', array(
'action' => 'list',
'form' => $formView,
'datagrid' => $datagrid,
'csrf_token' => $this->getCsrfToken('sonata.batch'),
));
}
}
The prestamos_clientes.html.twig view, shows the Clients and Loans info.
QUESTION: I want to filter the list view that i created (transaccionesAction) with an $id parameter and see the loans of an particular client.
Thanks.
You can set an admin to be the child of another. This has the advantage that you can, for example, click from one specific client to a list of loans for that particular clients..
To do this, follow the minimalistic documentation on the subject on setting an admin as child-admin: https://sonata-project.org/bundles/admin/master/doc/reference/architecture.html#create-child-admins.
When you have done that, you can add a link from a client to the loans:
Add a function 'configureSideMenu' to your clientadmin:
/**
* {@inheritdoc}
*/
protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
{
// show link only on edit and show
if (!$childAdmin && !in_array($action, array('edit', 'show'))) {
return;
}
$admin = $this->isChild() ? $this->getParent() : $this;
$id = $admin->getRequest()->get('id');
$menu->addChild(
'Loans',
array('uri' => $this->getChild('your.loan.service.id')->generateUrl('list', array('id' => $id)))
);
}
You can see a demo of this in the demo of sonata: http://demo.sonata-project.org/
click on 'Ecommerce' -> 'Order' -> 'specific order' -> 'Elements'
Here you can find the code of above example: https://github.com/sonata-project/ecommerce/tree/master/src/OrderBundle/Admin
More information on child-parent admin setup: Sonata/symfony - parent/child structure setup
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments