针对 ListMapper 的 Sonata_type_model 字段的 Sonata Admin 列表自定义查询

Kup*_*ris 2 symfony sonata-admin

我尝试获取一些与我所有步骤对象匹配的条目,但我在它们之间没有直接关系,因此我需要使用自定义查询。

我在我的管理课上试过:

    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'))));
Run Code Online (Sandbox Code Playgroud)

getProtocoles 函数:

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

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

    return $querybuilder;
}
Run Code Online (Sandbox Code Playgroud)

我还想知道如何传递当前实体 id(标识符返回 FieldDescription 对象)。

也许我错过了什么或什么,我希望我的帖子是全面的。

在此先感谢您的时间。

Web*_*bHQ 5

我认为你尝试的方法行不通。

我会尝试为协议属性定义一个自定义模板

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

在里面你会有对象和它的 id

{{ object.id | protocols() }}
Run Code Online (Sandbox Code Playgroud)

比我会写一个树枝扩展

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';
    }
}
Run Code Online (Sandbox Code Playgroud)