【问题标题】:Symfony Custom Announce Search with array entitySymfony 自定义公告搜索与数组实体
【发布时间】:2017-08-08 22:32:30
【问题描述】:

我正在制作一个公告网站,并且我正在尝试制作一个仅显示我选择的公告的研究表格。

例如,有一种表格,人们可以在其中标记角色,以便仅在具有所选角色的公告上进行搜索。

示例:

有 4 个按钮:

  • 教练,
  • 射手,
  • 守门员,
  • 边锋

我选择教练和射手。我希望只看到同时担任教练和射手角色的球员的公告(每个球员都可以担任许多不同的角色)。

在我的数组实体“poste”中,他可以扮演的所有不同角色。

示例:

mypost = $announce->getPoste();
// mypost[0] will have coach
// mypost[1] will have shooter

我尝试了很多东西,但对这种实体类型不起作用。

控制器:

$repository = $this
    ->getDoctrine()
    ->getManager()
    ->getRepository('PlatformBundle:Announce');

$criteria2 = $announce->getPoste();

$criteria = array_filter(array(
    'game'     => $game, // example: football
    'region'   => $announce->getRegion(), //example: Europe
    'language' => $announce->getLanguage(), //example: FR
    'poste'    => $announce->getPoste(), //example: Goalkeeper, Shooter, Coatch
    'level'    => $announce->getLevel() //example: Pro
));

$announces = $repository->findBy($criteria);
<!-- It's work for everything but not for getPoste -->

$announces = $repository->findByPoste($criteria2);
<!-- no match -->

我尝试了很多东西,比如创建存储库函数和创建自定义 SQL/DQL 函数,但仍然无法正常工作。我尝试在 Google 上搜索 **Symfony 3 findBy() 数组参数”,但我没有找到任何适合我的示例的内容。

感谢您的帮助:)

【问题讨论】:

  • it never works 是什么意思?具体是什么不工作?你得到什么输出?你没有输出吗?歪斜输出?错误信息?
  • 你好,我实际上如果我使用 $announces = $repository->findBy($criteria);一切都适用于所有变量、游戏、关卡、地区、语言,但它与 poste 不匹配,我没有找到任何公告。
  • 在我的_profiler中,我可以检查参数并且没有问题,每个参数都在变量内。参数:[ 0 => 足球,1 => 欧洲,2 => FR,3 => [ 0 => 教练,1 => 射手],4 => 职业]
  • Var_dump of $criteria: C:\wamp64\www\Wolk\src\Wolk\PlatformBundle\Controller\Announ‌​ceController.php:73: array (size=5) 'game' => string 'football' (length=8) 'region' => string 'America' (length=7) 'language' => string 'FR' (length=2) 'poste' => array (size=2) 0 => string 'coatch' (length=6) 1 => string 'shooter' (length=7) 'level' => string 'Pro' (length=3)

标签: php arrays forms symfony


【解决方案1】:

Doctrine findBy() 方法使用关联数组作为参数。如果你想用多个值来限制任何字段,那么你必须使用一个值数组作为特定的列条件。

例如你的情况:

$criteria = array(
    'game' => $game,
    'region' => $region,
    ...
    'poste' => array('Coatch', 'Shooter', ...),
    ...
);

因此,Announce 类的 getPoste() 方法必须返回一个数组,其中包含所需的玩家帖子,分别是您存储在数据库中的值。

编辑

如果你有数组类型的属性,你必须通过学说 queryBuilder 创建自定义查询。为 Announce 实体创建存储库,您可以在其中创建自己的方法来获取数据。检查下面的示例,针对您的特定情况(代码未经测试,因此需要进行细微调整)。

class AnnounceRepository extends ORM\EntityRepository
{
    ...
    ...

    public function findAnnouncesByCriteria($game, $region, $language, $poste, $level)
    {
        $qb = $this->_em->createQueryBuilder();
        $qb->select('user')
            ->from('PlatformBundle\Announce', 'announce');

        if ($game!== null) {
            $qb->andWhere('announce.game = \''.$game.'\'');

        }

        ...
        ...

        if ($poste!== null) {
            foreach($poste as $itm)
                $qb->andWhere('announce.poste like \'%'.$itm.'%\'' );
        }

        ...
        ...

        return $qb->getQuery()->getResult();
    }

}

【讨论】:

  • 你好,我已经在我的实体中有一个列宣布谁包含这个: /** * @var array * * @ORM\Column(name="poste", type="array") * / 私人 $poste;在我的 sql 数据库中,数组中有所有不同的角色,goolkeeper 等。当我写 $criteria2 = $announce->getPoste();我的变量 $criteria2 包含所有不同的角色,但它不匹配,我在测试时看不到任何公告。
  • 什么返回$announce->getPoste(),或者请分别var_dump($criteria2);。
  • 在我的_profiler中,我可以检查参数并且没有问题,每个参数都在sql变量中。参数:[ 0 => 足球,1 => 欧洲,2 => FR,3 => [ 0 => 教练,1 => 射手],4 => 职业]
  • 所以你的 $criteria 数组包含了这些值,但是它的键丢失了。 $criteria 应该看起来像 [ game =&gt; football, region =&gt; europe, language =&gt; FR, poste =&gt; [ 0 =&gt; coatch, 1 =&gt; shooter ], level =&gt; pro ] 或您示例中的 $criteria2 应该看起来像 [ poste =&gt; [ 0 =&gt; coatch, 1 =&gt; shooter ] ]
  • 它适用于所有不同的变量、游戏、地区、语言和关卡,但缺少密钥但不适用于 poste。
猜你喜欢
  • 1970-01-01
  • 2014-04-13
  • 2018-08-31
  • 1970-01-01
  • 2017-02-26
  • 2017-02-26
  • 2016-08-26
  • 1970-01-01
  • 2011-04-04
相关资源
最近更新 更多