如何限制Sonata多对多字段中的可用选项?

问题描述 投票:0回答:1

我正在开发一个使用Sonata Admin软件包构建的程序。我有一个ScheduleAdmin类,其中包含以下逻辑:

    $formMapper
        ->add(
            'markets',
            null,
            [
                'required' => true,
                'label'    => 'shared_countries',
                'data'     => $this->getMarketsByUser($this->getUser())
            ]
        )
    ;

时间表与市场之间的关系是多对多的关系。

getMarketsByUser()方法总是返回一个市场 - 例如USA。我在浏览器中获得的是包含我的市场的字段,但也包括包含所有其他国家/地区的自动填充下拉列表。

现在我想摆脱那些其他自动填充选项,只允许我的用户附属市场。我怎么做?

php symfony doctrine many-to-many sonata-admin
1个回答
0
投票

那很简单。我看了一下this page,然后改变了我的add()方法调用,看起来像这样:

    $formMapper
        ->add(
            'markets',
            null,
            [
                'required' => true,
                'label'    => 'shared_countries',
                'data'     => $this->getMarketsByUser($this->getUser()),
                'choices'     => $this->getMarketsByUser($this->getUser()),
            ]
        )
    ;

......现在它起作用了。

© www.soinside.com 2019 - 2024. All rights reserved.