Sonata Admin configureListFields show through query

蹲街弑〆低调 提交于 2019-12-08 11:17:27

问题


I am using sonata admin bundle for admin panel. I want to show data in configureListFields through query. I have table userChoiceProduct and fields :-

User_Id
Product_Id

These fields automatically fill when user select any product and submit form. But these fields no relationship to other table.and I want to show User Email and Product Name in configureListFields bases on User_Id and Product_Id.

Thanks!


回答1:


I solved this:-

In Sonata Admin list :-

            ->add('User Email', null, array('template' => 'ABCAdminBundle:UserChoiceProduct:user.html.twig'))
    ->add('Product Name', null, array('template' => 'ABCAdminBundle:UserChoiceProduct:prodcut.html.twig'))

I mentioned one twig file (user.html.twig) for example :

<td>{{object.userId|getUserDetail()}}</td>

And create getUserDetail() in twig extension :-

class ABCExtension extends \Twig_Extension {

private $generator;
private $container;

public function __construct(UrlGeneratorInterface $generator, Container $container) {
    $this->generator = $generator;
    $this->container = $container;
}
public function getFilters() {
    return array(
        'getUserDetail' => new \Twig_Filter_Method($this, 'getUserDetail'),
    );
}
public function getUserDetail($userId)
{
    $em = $this->container->get('doctrine')->getManager();
    $user = $em->getRepository('ABCUserBundle:User')->findOneBy(array('id' =>$userId));
    if(empty($user)){
        $userEmail = 'User does not Exist';
        return $userEmail;          
    }else{
        $userEmail = $user->getEmail();
        return $userEmail;
    }
}
}

And then all work is done successfully.




回答2:


You can use the createQuery method in your admin to customize the parent query, like that :

public function createQuery($context = 'list') {
    $query = parent::createQuery($context);
    $query
        ->addSelect('u')
        ->leftJoin('Path\To\User\Entity', 'u', \Doctrine\ORM\Query\Expr\Join::ON, 't0_.User_Id = u.id')
    ;
    return $query;
}

By getting and replacing t0_ by the prefix of userChoiceProduct table in main query.



来源:https://stackoverflow.com/questions/24503370/sonata-admin-configurelistfields-show-through-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!