Bookshelf.js 喜欢动态输入的地方
Bookshelf.js Where Like Dynamic Input
我正在尝试使用 Express/Bookshelf/Awesomeplete 创建自动完成搜索。
我一直在试图弄清楚如何使用像这样的 Where Like 查询将搜索词传递给 Bookshelf,我正在从 PHP/MYSQL.
转换
$query = mysql_escape_string($_REQUEST['query']);
$query = htmlentities($query);
SELECT SID, schoolName FROM schools WHERE schoolName LIKE '%$query%'
这是我目前的情况。
var terms = req.params.search;
new Model.School()
//This is the part that I can't get to work.
.query('where', 'schoolName', 'LIKE', '%'?'%',[terms])
.fetchAll({columns: ['SID', 'schoolName']})
.then(function (schools) {
console.log(schools);
var schools = schools.toJSON();
res.json(schools);
})
.catch(function (error) {
res.json({'message':'An error occured in your search'});
});
如果我把上面的改成:
new Model.School()
.query('where', 'schoolName', 'LIKE', '%american%')
.fetchAll({columns: ['SID', 'schoolName']})
.then(function (schools) {
console.log(schools);
var schools = schools.toJSON();
res.json(schools);
})
.catch(function (error) {
res.json({'message':'An error occured in your search'});
});
Bookshelf 查询功能符合我的需要,但我希望查询参数是动态的。我尝试了很多排列组合,但无法正常工作。
根据我的评论和您的验证,它看起来像以下作品。
基本上,您希望在将搜索值传递到 .query
LIKE 子句之前将“%”附加到搜索值。
var terms = "%"+req.params.search+"%";
new Model.School()
.query('where', 'schoolName', 'LIKE', terms)
.fetchAll({columns: ['SID', 'schoolName']})
.then(function (schools) {
console.log(schools);
var schools = schools.toJSON();
res.json(schools);
})
.catch(function (error) {
res.json({'message':'An error occured in your search'});
});
快乐编码。加里.
我正在尝试使用 Express/Bookshelf/Awesomeplete 创建自动完成搜索。
我一直在试图弄清楚如何使用像这样的 Where Like 查询将搜索词传递给 Bookshelf,我正在从 PHP/MYSQL.
转换$query = mysql_escape_string($_REQUEST['query']);
$query = htmlentities($query);
SELECT SID, schoolName FROM schools WHERE schoolName LIKE '%$query%'
这是我目前的情况。
var terms = req.params.search;
new Model.School()
//This is the part that I can't get to work.
.query('where', 'schoolName', 'LIKE', '%'?'%',[terms])
.fetchAll({columns: ['SID', 'schoolName']})
.then(function (schools) {
console.log(schools);
var schools = schools.toJSON();
res.json(schools);
})
.catch(function (error) {
res.json({'message':'An error occured in your search'});
});
如果我把上面的改成:
new Model.School()
.query('where', 'schoolName', 'LIKE', '%american%')
.fetchAll({columns: ['SID', 'schoolName']})
.then(function (schools) {
console.log(schools);
var schools = schools.toJSON();
res.json(schools);
})
.catch(function (error) {
res.json({'message':'An error occured in your search'});
});
Bookshelf 查询功能符合我的需要,但我希望查询参数是动态的。我尝试了很多排列组合,但无法正常工作。
根据我的评论和您的验证,它看起来像以下作品。
基本上,您希望在将搜索值传递到 .query
LIKE 子句之前将“%”附加到搜索值。
var terms = "%"+req.params.search+"%";
new Model.School()
.query('where', 'schoolName', 'LIKE', terms)
.fetchAll({columns: ['SID', 'schoolName']})
.then(function (schools) {
console.log(schools);
var schools = schools.toJSON();
res.json(schools);
})
.catch(function (error) {
res.json({'message':'An error occured in your search'});
});
快乐编码。加里.