Search code examples
symfonysonata-admin

Sonata Admin list custom query for ListMapper sonata_type_model field


I try to get some entrys which match with all of my step objects but I don't have direct relation beetween them so I need to use a custom query.

I tried that in my admin class :

    protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('texteEtape', null, array('label' => $this->getTranslator()->trans('label.step.text')));

    $this->addCountryListMap($listMapper);

    $listMapper
        ->addIdentifier('id')
        ->add('temperature')
        ->add('rincage', null, array('label' => $this->getTranslator()->trans('label.rinsing')))
        ->add('concentration')
        ->add('temps', null, array('label' => $this->getTranslator()->trans('label.time')))
        ->add('produit', null, array('label' => $this->getTranslator()->trans('label.product')))
        ->add('enabled', null, array('editable' => true))
        ->add('_action', 'actions', array(
            'actions' => array(
                'edit' => array(),
                'delete' => array()
            )
        ))
        ->add('updatedAt')
        ->add('Protocols','sonata_type_model', array('required' => true,
            'class'=> 'HypredMainBundle:Protocole','property'=> 'name',
            'query_builder' => $this->getProtocoles($listMapper->get('id'))));

The getProtocoles function :

    private function getProtocoles($id) {
    $querybuilder = $this->getManager()->getRepository('HypredMainBundle:Etape')->getStepProtocoles($id);

    var_dump($querybuilder->getQuery()->getResult());
    die();

    return $querybuilder;
}

I would like to know how pass current entity id also (identifier return a FieldDescription object).

Maybe I miss something or something, I hope my post is comprehensive.

Thanks in advance for your time.


Solution

  • I think the way, that you try, wont work.

    I would try to define a custom template for Protocols attribute

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('id')
            ->add('Protocols','string', array('template' => 'HypredMainBundle:Protocole:list_protocole.html.twig'))
        ;
    }
    

    Inside you will have object and its id

    {{ object.id | protocols() }}
    

    Than I would wrote a Twig Extension

    class AppExtension extends \Twig_Extension
    {
        protected $container;
    
        public function __constructor($container)
        {
            // remember to pass @service_container defining a twig extension service
            $this->container = $container;
        }
    
        public function getFilters()
        {
            return array(
                new \Twig_SimpleFilter('protocols', array($this, 'protocolsFilter')),
            );
        }
    
        public function ProtocolsFilter($id)
        {
            // content of getProtocoles() function from question
    
            $querybuilder = $this->container->get('doctrine')->getManager()->getRepository('HypredMainBundle:Etape')->getStepProtocoles($id);
            var_dump($querybuilder->getQuery()->getResult());
            die();
    
            return $querybuilder;
        }
    
        public function getName()
        {
            return 'app_extension';
        }
    }