在列表中仅显示 country_id =1 (Symfony + Sonata)
Show only country_id =1 in list (Symfony + Sonata)
这是我对“新闻”实体的简单列表
protected function configureListFields(ListMapper $list): void
{
$list
->filte
->add('id')
->add('name')
->add('description')
->add('createdAt')
->add('updatedAt')
->add(ListMapper::NAME_ACTIONS, null, [
'actions' => [
'show' => [],
'edit' => [],
'delete' => []
],
]);
}
News 实体还有一个 country_id 字段(我不会显示)。
我只需要显示 country_id = 1 的新闻。
怎么样?
好的,我用 documentation
解决了
protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface
{
$query = parent::configureQuery($query);
$rootAlias = current($query->getRootAliases());
$query->andWhere(
$query->expr()->eq($rootAlias . '.my_field', ':my_param')
);
$query->setParameter('my_param', 'my_value');
return $query;
}
但需要进行一些更改,因为 $query 不是查询生成器对象。
protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface
{
$query = parent::configureQuery($query);
$query_qb = $query->getQueryBuilder();
$rootAlias = current($query->getQueryBuilder()->getRootAliases());
$query_qb->andWhere(
$query_qb->expr()->eq($rootAlias . '.idCountry', ':idCountry')
);
$query_qb->setParameter('idCountry', $this->security->getUser()->getCountry()->getId());
return $query;
}
这是我对“新闻”实体的简单列表
protected function configureListFields(ListMapper $list): void
{
$list
->filte
->add('id')
->add('name')
->add('description')
->add('createdAt')
->add('updatedAt')
->add(ListMapper::NAME_ACTIONS, null, [
'actions' => [
'show' => [],
'edit' => [],
'delete' => []
],
]);
}
News 实体还有一个 country_id 字段(我不会显示)。 我只需要显示 country_id = 1 的新闻。 怎么样?
好的,我用 documentation
解决了protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface
{
$query = parent::configureQuery($query);
$rootAlias = current($query->getRootAliases());
$query->andWhere(
$query->expr()->eq($rootAlias . '.my_field', ':my_param')
);
$query->setParameter('my_param', 'my_value');
return $query;
}
但需要进行一些更改,因为 $query 不是查询生成器对象。
protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface
{
$query = parent::configureQuery($query);
$query_qb = $query->getQueryBuilder();
$rootAlias = current($query->getQueryBuilder()->getRootAliases());
$query_qb->andWhere(
$query_qb->expr()->eq($rootAlias . '.idCountry', ':idCountry')
);
$query_qb->setParameter('idCountry', $this->security->getUser()->getCountry()->getId());
return $query;
}