CakePHP使用%like%查找查询

前端之家收集整理的这篇文章主要介绍了CakePHP使用%like%查找查询前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下查找我的Cake PHP应用程序的查询
$this->paginate = array(
'limit'=>5,'order'=>'Note.datetime DESC','conditions' => array(
    'Note.status'=>1,'OR' => array(
        'Note.title LIKE' => '%'. $q . '%','Note.content LIKE' => '%'. $q . '%'
        )
    )
);

其中一个名为$q的参数就像对标题内容查询一样.

例如,如果我有以下内容

标题:Lorem ipsum
内容:Lorem ipsum dolare

并寻找’lorem’

它会发现很好.但如果我搜索’lorem dolare’,它将找不到它.

我怎么做?

注意:我不想使用任何插件等.

编辑:如果我使用FULLTEXT这会工作吗?

$this->paginate = array(
'limit'=>5,'OR' => array(
        'MATCH(Note.title) AGAINST('.$q.' IN BOOLEAN MODE)','MATCH(Note.content) AGAINST('.$q.' IN BOOLEAN MODE)'
        )
    )
);

编辑2:

尝试以上操作时出现此错误

Error: sqlSTATE[42000]: Syntax error or access violation: 1064 You have an error in your sql Syntax; check the manual that corresponds to your MysqL server version for the right Syntax to use near 'dolor IN BOOLEAN MODE)) OR (MATCH(`Note`.`content`) AGAINST(lorem dolor IN BOOLE' at line 1

sql Query: SELECT `Note`.`id`,`Note`.`title`,`Note`.`excerpt`,`Note`.`content`,`Note`.`datetime`,`Note`.`user_id`,`Note`.`slug`,`Note`.`status`,`Note`.`topic_id`,`User`.`id`,`User`.`email`,`User`.`firstname`,`User`.`lastname`,`User`.`password`,`User`.`status`,`Topic`.`id`,`Topic`.`title`,`Topic`.`slug` FROM `db52704_driz2013`.`notes` AS `Note` LEFT JOIN `db52704_driz2013`.`users` AS `User` ON (`Note`.`user_id` = `User`.`id`) LEFT JOIN `db52704_driz2013`.`topics` AS `Topic` ON (`Note`.`topic_id` = `Topic`.`id`) WHERE `Note`.`status` = 1 AND ((MATCH(`Note`.`title`) AGAINST(lorem dolor IN BOOLEAN MODE)) OR (MATCH(`Note`.`content`) AGAINST(lorem dolor IN BOOLEAN MODE))) ORDER BY `Note`.`datetime` DESC LIMIT 5
试试这个
$this->paginate = array(
    'limit'=>5,'conditions' => array(
        'Note.status'=>1,'OR' => array(
            "MATCH(Note.title) AGAINST('".$q."' IN BOOLEAN MODE)","MATCH(Note.content) AGAINST('".$q."' IN BOOLEAN MODE)"
        )
    )
);

换句话说,用引号括起搜索条件

编辑ndm的建议是有道理的

$this->paginate = array(
    'limit'=>5,'OR' => array(
            'MATCH(Note.title) AGAINST(? IN BOOLEAN MODE)' => $q,'MATCH(Note.content) AGAINST(? IN BOOLEAN MODE)' => $q
        )
    )
);
原文链接:https://www.f2er.com/php/133623.html

猜你在找的PHP相关文章