简体   繁体   English

在列表中仅显示 country_id =1 (Symfony + Sonata)

[英]Show only country_id =1 in list (Symfony + Sonata)

This is my simple listing of the "News" entity这是我对“新闻”实体的简单列表

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' => []
            ],
        ]);
}

The News entity also has a country_id field (I won't show). News 实体还有一个 country_id 字段(我不会显示)。 I need show only the news with country_id = 1. How?我只需要显示 country_id = 1 的新闻。如何?

Ok, I solved with 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;
}

But need made several changes beacuse $query not is a QueryBuilder Object.但需要进行一些更改,因为 $query 不是 QueryBuilder Object。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM