[英]Sonata Admin custom query in configureListFields
I am stuck on this for several hours. 我被困在这几个小时。
I have admin class to list all categories and in one table column there are related products (Product entity): Table example Related code: 我有管理类列出所有类别,在一个表列中有相关产品(产品实体):表示例相关代码:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('name')
->add('products') // Entity Product, @ORM\OneToMany
->add('ord')
;
}
What I need to do is hide inactive products from being listed based on "(boolean) product.active" but I can't figure it out. 我需要做的是隐藏基于“(boolean)product.active”列出的非活动产品,但我无法弄明白。 I know about "createQuery" method but it doesn't work.
我知道“createQuery”方法,但它不起作用。 When I generate SQL and run the query directly it works but here it looks like I can use ProxyQuery only to filter Category and then all Products are queried in separate query (and this separate query I am not sure how to change).
当我生成SQL并直接运行查询时,它可以工作,但在这里看起来我只能使用ProxyQuery过滤类别,然后在单独的查询中查询所有产品(而这个单独的查询我不知道如何更改)。
public function createQuery($context = 'list')
{
$query = parent::createQuery($context);
$q = new ProxyQuery($query->join(sprintf('%s.products', $query->getRootAlias()), 'p')
->andWhere('p.active = :act')->setParameter('act', true));
return $q;
}
Thank you for any help 感谢您的任何帮助
You can change the type of your field to null or entity and add a query_builder option to adapt the query used : 您可以将字段的类型更改为null或实体,并添加query_builder选项以适应所使用的查询:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('name')
->add('products', null, array(
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('qb')
->leftjoin('qb.products', 'p')
->where('p.active = :act')
->setParameter('act', true)
}
));
}
I didn't test it with a oneToMany relation. 我没有用oneToMany关系测试它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.