2016-10-26 8 views
0

누군가가 기본 검색을 도와 줄 수 있습니까? 나는 그것을 알아낼 수 없다. 1 단어를 입력 할 때만 내 검색이 작동합니다. 하지만 2,3,4, ... 단어를 입력하면 작동하지 않습니다.Symfony/Doctrine - 검색

: I는 입력을 검색 입력하면 POLYKARBONÁTOVÉ POUZDRO NA MACBOOK (맥북에 대한 폴리 카보네이트 커버)

그냥 "pouzdro는"그것은 모든 덮개를 반환합니다. "Polykarbonátové pouzdro"라고 입력하면 아무 것도 반환하지 않습니다. "Polykarbonátové"를 입력하면 아무 것도 반환하지 않습니다. 로그에서 쿼리 (내가 PostgreSQL을 사용하고 있습니다) : 그래서

[2016-10-26 16:47:11] doctrine.DEBUG: SELECT p0_.id AS id_0, p0_.serial_number AS serial_number_1, p0_.title AS title_2, p0_.url AS url_3, p0_.note AS note_4, p0_.views AS views_5, p0_.price AS price_6, p0_.bought_price AS bought_price_7, p0_.old_price AS old_price_8, p0_.is_active AS is_active_9, p0_.is_accessory AS is_accessory_10, p0_.is_special AS is_special_11, p0_.quantity AS quantity_12, p0_.created_at AS created_at_13, p0_.updated_at AS updated_at_14, p0_.details_id AS details_id_15, p0_.accessory_category_id AS accessory_category_id_16 FROM products p0_ WHERE p0_.title LIKE ? ["%polykarbonatove%"] []

[2016-10-26 16:53:52] doctrine.DEBUG: SELECT p0_.id AS id_0, p0_.serial_number AS serial_number_1, p0_.title AS title_2, p0_.url AS url_3, p0_.note AS note_4, p0_.views AS views_5, p0_.price AS price_6, p0_.bought_price AS bought_price_7, p0_.old_price AS old_price_8, p0_.is_active AS is_active_9, p0_.is_accessory AS is_accessory_10, p0_.is_special AS is_special_11, p0_.quantity AS quantity_12, p0_.created_at AS created_at_13, p0_.updated_at AS updated_at_14, p0_.details_id AS details_id_15, p0_.accessory_category_id AS accessory_category_id_16 FROM products p0_ WHERE p0_.title LIKE ? AND p0_.title LIKE ? ["%polykarbonatove%","%pouzdro%"] []

답변

0

,

if ($keyword != null) { 
     $keyword = strtolower(preg_replace('/\p{Mn}/u', '', \Normalizer::normalize($keyword, \Normalizer::FORM_KD))); 
     $keywords = explode(' ', $keyword); 
     $i = 0; 
     foreach ($keywords as $key) { 
      $i++; 
      if ($i == 1) { 
       $qb->where("p.title LIKE :keyword$i"); 
      } else { 
       $qb->andWhere("p.title LIKE :keyword$i"); 
      } 
      $qb->setParameter("keyword$i", "%" . $key . "%"); 
     } 
    } 

당신 :

편집 감사합니다 여기에

내 코드입니다
  1. 당신은 정말 우리에게 어떤 교리 실제 쿼리를주고 당신이

    foreach ($keywords as $key) { 
        $i++; 
        if ($i == 1) { 
         $qb->where("p.title LIKE :keyword$i"); 
        } else { 
         $qb->andWhere("p.title LIKE :keyword$i"); 
        } 
        $qb->setParameter("keyword$i", "%" . $key . "%"); 
    } 
    
  2. 당신이 로그 파일을 열 수 있습니다

    이 방법에 코딩해야 코드를 지우려면이

    LOWER() // LIKE is not case sensitive 
    
  3. 시도가 필요하지 않습니다 시도했습니다 (var/cache/logs/dev.log)

+0

나는 내 질문을 편집했습니다. –

0

발생한 문제는 검색어의 수에 대한 것이 아니라 액센트에 대한 것입니다.

액센트없이 검색을 수행하려는 경우 데이터 정렬을 악센트를 구분하지 않는 utf8_general_ci으로 변경할 수 있습니다. here을 설명하고 hattila에서 this answer에 구현

교리와 정렬을 변경하려면 사용자 정의 DQL 함수를 구현해야합니다 :

namespace MyCompany\MyBundle\DQL; 

use Doctrine\ORM\Query\AST\Functions\FunctionNode; 
use Doctrine\ORM\Query\Lexer; 

class CollateFunction extends FunctionNode 
{ 
    public $expressionToCollate = null; 
    public $collation = null; 

    public function parse(\Doctrine\ORM\Query\Parser $parser) 
    { 
     $parser->match(Lexer::T_IDENTIFIER); 
     $parser->match(Lexer::T_OPEN_PARENTHESIS); 
     $this->expressionToCollate = $parser->StringPrimary(); 

     $parser->match(Lexer::T_COMMA); 

     $parser->match(Lexer::T_IDENTIFIER); 
     $lexer = $parser->getLexer(); 
     $this->collation = $lexer->token['value']; 

     $parser->match(Lexer::T_CLOSE_PARENTHESIS); 
    } 

    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) 
    { 
     return sprintf('%s COLLATE %s', $this->expressionToCollate->dispatch($sqlWalker), $this->collation); 
    } 
} 

그런 다음 심포니 as explained here에 추가합니다.

+0

고마워, 그렇게해야 하나? 또는 $ qb-> where ("p.title LIKE COLLATE (: 키워드 $ i, utf8_general_ci)"); ? 왜냐하면 나는 이것을 가지고 있습니다 : SQLSTATE [42704] : 정의되지 않은 개체 : 7 오류 : "UTF8"인코딩을위한 "utf8_general_ci"콜레 션이 존재하지 않습니다. –

+0

오류에 대해 조사한 후, 전체 데이터베이스의 데이터 정렬을 변경해야 할 것 같습니다. postgresql은 이와 같은 데이터 정렬을 변경할 수 없으므로 데이터 정렬 'C'에 추가합니다. 난 당신이 대답 http://stackoverflow.com/a/35795540/4074148 또는 다른 사람을 볼 것을 권 해드립니다. 그러나 나는이 질문의 범위 밖에 있다고 생각합니다. – Veve