代码之家  ›  专栏  ›  技术社区  ›  Dhia Eddine Farah

从管理类SonataAdminBundle获取存储库

  •  0
  • Dhia Eddine Farah  · 技术社区  · 8 年前

    我使用symfony 2.8和SonataAdminBundle,我想查看“客户端”用户,这是我的代码:

    客户管理员

     protected function configureListFields(ListMapper $listMapper)
    {
    
         $result = $this->getConfigurationPool()->getContainer()->get('Doctrine')->getRepository('UserBundle:User')->findClient();
    
        $listMapper
    
            ->add('client', 'sonata_type_model', array(
                'empty_value' => '', 
                'choice_list' => $result))
    
                  ;
    }
    

    用户存储库

    public function findClient()
    {   $dql = "SELECT p FROM UserBundle:User p WHERE p.type LIKE 'client' ORDER BY p.id DESC";
        return $this->getEntityManager()
            ->createQuery($dql)
    
            ->getResult();
    }
    

    1 回复  |  直到 8 年前
        1
  •  2
  •   issamwg    8 年前

    借助createQuery方法,您可以自定义列表查询:

        <?php
    
    public function createQuery($context = 'list')
    {
        $query = parent::createQuery($context);
        $query->andWhere(
            $query->expr()->eq($query->getRootAliases()[0] . '.my_field', ':my_param')
        );
        $query->setParameter('my_param', 'my_value');
        return $query;
    }
    

    Sonata Documentation