2017-07-25 149 views
1

Doctrine2.5隨着PHP 5.6

我有一個事物的陣列進行搜索,例如: 以用戶爲表,我想在字段名稱中搜索名稱爲「Reis」或「Shimidt」的所有人的姓名。隨着陣列

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

我要帶上例如下面的條目,

  • 約翰·雷斯卡爾森,
  • 瑪麗Shimidt林肯,
  • 比爾李嘉欣Abdonor·蓋茨。

我想是這樣的:

$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); 

任何想法如何解決這個,沒有做一個凌亂的代碼?

回答

1

我建議你使用這樣的:

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

感謝亞歷山德羅,這樣的作品。 –

+0

很高興爲您服務!接受答案,以幫助其他人與您的同樣的問題@AlbertAbdonor –

0

試試這個:

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

你忘了格式化你的答案爲代碼:) – ArtOsi

+0

編輯,對不起 –