在 Phalcon Framework 的方法 find() 的条件下传递变量
Passing variable in conditions of method find() in Phalcon Framework
我在我的控制器中收到一个变量,我想用搜索变量进行绑定查询,我试试这个:
$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>
我的 $search 是空的,在我的 JQuery 中我需要在 ajax 中添加一个 header:
...
source: function ( request, response ) {
$.ajax({
type: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
...
谢谢@Abhik Chakraborty
我在我的控制器中收到一个变量,我想用搜索变量进行绑定查询,我试试这个:
$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>
我的 $search 是空的,在我的 JQuery 中我需要在 ajax 中添加一个 header:
...
source: function ( request, response ) {
$.ajax({
type: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
...
谢谢@Abhik Chakraborty