gpt4 book ai didi

php - configureListFields 中的 Sonata Admin 自定义查询

转载 作者:可可西里 更新时间:2023-11-01 13:01:05 26 4
gpt4 key购买 nike

我在这上面停留了几个小时。

我有管理类来列出所有类别,并且在一个表列中有相关产品(产品实体):Table example相关代码:

protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('name')
->add('products') // Entity Product, @ORM\OneToMany
->add('ord')
;
}

我需要做的是根据“(boolean) product.active”隐藏不活动的产品,但我无法弄清楚。我知道“createQuery”方法,但它不起作用。当我生成 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;
}

谢谢你的帮助

最佳答案

您可以将字段类型更改为空或实体并添加 query_builder option调整使用的查询:

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

我没有用 oneToMany 关系测试它。

这个topic里面有一些信息

关于php - configureListFields 中的 Sonata Admin 自定义查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33293419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com