在Phalcon Framework中的方法find()条件下传递变量

时间:2017-09-23 22:22:01

标签: php mysql sql phalcon phalcon-orm

我在控制器中收到一个变量,我想用搜索变量进行绑定查询,我试试这个:

$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>

1 个答案:

答案 0 :(得分:1)

我的$ search是空的,在我的JQuery中我需要在ajax中添加一个标题:

...
source: function ( request, response ) {
            $.ajax({
                type: "POST",
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },  
...

谢谢@Abhik Chakraborty