【问题标题】:PHP Doctrine2 search "Like" with an Array "%$array%"PHP Doctrine2 使用数组“%$array%”搜索“Like”
【发布时间】:2017-07-25 07:52:52
【问题描述】:

Doctrine2.5 与 PHP 5.6

我有很多东西要搜索,例如: 在用户表中,我想搜索拥有 字段名称中的名称“Reis”或“Shimidt”。用数组

$arraySearch = ['Reis', 'Shimidt'];

我想带例如以下条目,

  • 约翰·雷斯·卡尔森,
  • 玛丽·施密特·林肯,
  • 比尔·雷斯·阿布多诺·盖茨。

我尝试过这样的事情:

$this->query->andWhere(" pb.name LIKE '%:name%' ");
$this->query->setParameter('name', $name, \Doctrine\DBAL\Types\Type::SIMPLE_ARRAY);

不行,我也这样试过,但是明显返回数组到字符串的转换:

$this->query->setParameter('name', '%'.$name.'%', \Doctrine\DBAL\Types\Type::SIMPLE_ARRAY);

任何想法如何解决这个问题,而无需编写凌乱的代码?

【问题讨论】:

    标签: php laravel doctrine sql-like


    【解决方案1】:

    我建议你使用这样的东西:

    foreach ($arraySearch as $search) {
        $this->query->orWhere(" pb.name LIKE :search ");
        $this->query->setParameter("search", '%'.$search.'%');
    }
    

    【讨论】:

    • 很高兴为您提供帮助!接受答案以帮助其他有同样问题的人@AlbertAbdonor
    【解决方案2】:

    试试这个:

    $this->query->setParameter('name', implode("%', OR name LIKE '%", $arraySearch));
    

    【讨论】:

    • 已编辑,对此感到抱歉
    猜你喜欢
    • 1970-01-01
    • 2018-10-24
    • 2017-03-14
    • 2011-04-17
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 2015-12-19
    相关资源
    最近更新 更多