[英]Symfony/Doctrine - Search
could someone help me with basic search? 有人可以帮助我进行基本搜索吗? I just can't figure it out. 我只是想不出来。 My search works only if I type 1 word. 只有输入1个单词时,我的搜索才有效。 But if I type 2,3,4,... words it doesn't work. 但是,如果我键入2,3,4,......单词则不起作用。
Example : POLYKARBONÁTOVÉ POUZDRO NA MACBOOK (Polycarbonate cover for Macbook) 示例 :POLYKARBONÁTOVÉPOUZDRONA MACBOOK (Macbook的聚碳酸酯盖)
If I type to search input just "pouzdro" it returns all covers. 如果我输入搜索输入只是“pouzdro”它返回所有封面。 If I type "Polykarbonátové pouzdro" it return nothing. 如果我输入“Polykarbonátovépouzdro”,它什么都不返回。 If I type "Polykarbonátové" it returns nothing. 如果我输入“Polykarbonátové”,它什么都不返回。
Here is my code: 这是我的代码:
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 . "%");
}
}
Thank you :) 谢谢 :)
EDIT: Query from the log(I am using postgresql): 编辑:从日志查询(我正在使用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 ? [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 buying_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%"] [] [“%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 ? [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 buying_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 ? 和p0_.title喜欢? ["%polykarbonatove%","%pouzdro%"] [] [“%polykarbonatove%”,“%pouzdro%”] []
So, 所以,
You don't really need this 你真的不需要这个
LOWER() // LIKE is not case sensitive
Try to clear your code, you should code on this way 尝试清除代码,您应该以这种方式编写代码
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 . "%"); }
Can you open the log file and give us the real query which doctrine tried (var/cache/logs/dev.log) 你可以打开日志文件并给我们真正的查询,其中学习过(var / cache / logs / dev.log)
The problem you encountered isn't about the number of searched terms, but about accents. 您遇到的问题不是搜索条件的数量,而是关于重音。
If you want to perform a search without accents, you can change the collation to utf8_general_ci
, which is accent insensitive. 如果要执行不带重音的搜索,可以将排序规则更改为utf8_general_ci
,这是重音不敏感的。
To change the collation with Doctrine, you'll have to implement a custom DQL function, as explained here and implemented in this answer from hattila : 要更改与学说的整理,你必须实现自定义DQL功能,如解释在这里和在实现这个答案从hattila :
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 );
}
}
Then add it to Symfony as explained here . 然后按照此处的说明将其添加到Symfony。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.