我在控制器中收到一个变量,我想用搜索变量进行绑定查询,我试试这个:
$search = $this->request->getPost('term');
$item = Item::find(
[
'columns' => 'name',
'conditions' => "name LIKE :searchT: ",
'bind' => [
'searchT' => '%'.$search.'%',
],
]
);
以上代码返回的项目与LIKE限制不匹配。
如果我传递字符串字面上工作正常:
$item = Item::find(
[
'conditions' => "name LIKE '%Os%' ",
'columns' => 'name',
'limit' => 10,
]
);
JQuery的:
<script type="text/javascript">
$( function() {
$("#itemSearch").autocomplete({
source: function ( request, response ) {
$.ajax({
type: "POST",
url: "/item/search",
dataType: "json",
data: {
term: request.term
},
contentType: "application/json",
success: function(data) {
response(data);
}
});
},
minLength: 2
})
})
</script>
答案 0 :(得分:1)
我的$ search是空的,在我的JQuery中我需要在ajax中添加一个标题:
...
source: function ( request, response ) {
$.ajax({
type: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
...
谢谢@Abhik Chakraborty