0

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.

1 Answers1

1

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

Community
  • 1
  • 1
11mb
  • 1,339
  • 2
  • 16
  • 33
  • Thanks @11mb ! very helpfull. In the edit view of clients i have a link to the loan list and even i can edit them from there. Works excelent! Even i add a Loans Button to the _action element and works like a shortcut. In order to continue with this method, i had connected Loans to Collections. Works fine as well. But when i go to edit an specific client, click to Loans and Edit one of them, i can't addMenu Collections to continue with the unchained relationship? Another way to do this will be a link to the edit loan directly (not embed in client edit view) – Denis Gimenez May 06 '15 at 21:15
  • Nice it worked for you.. Sonata is very good in hiding all the fun stuff ;).. I don't think I really understand your question in the comment.. Maybe you can rephrase it? An extra comment I have on my answer: I noticed that my search function didn't work anymore.. I changed the $admin->getRequest() code.. See here for more info: http://stackoverflow.com/questions/19551715/sonata-admin-search-feature – 11mb May 07 '15 at 07:21
  • Sorry for my english. I will try to explain my problem. For example, in Loan Class i have an attribute pointing to a Client Class. But also, i added an attribute pointing to the Seller Class. When i configure the LoansAdmin configure function like this: $this->parentAssociationMapping = 'seller'; $this->parentAssociationMapping = 'client'; So when I am in the Clients List, the Loans buttons take me to the Loans list filtered by client attribute (in configureDatagridFilters) But, when i go to Sellers List, the Loans button doesnt work and the seller id **doesnt work!**. **Can i do this?** – Denis Gimenez Jun 12 '15 at 14:47