Search code examples
phpsymfonysonata-admin

get repository from Admin Class SonataAdminBundle


I work with symfony 2.8 and SonataAdminBundle, I want to view the users who are 'client' and here is my code:

ClientAdmin

 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))

              ;
}

UserRepository

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();
}

But it does not work and still no result


Solution

  • You can customize the list query thanks to the createQuery method :

        <?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