为什么`LIKE`运算符不使用整数列?

时间:2017-07-05 05:11:42

标签: php sql cakephp query-builder cakephp-3.x

我正在尝试从我的数据库中接收一些ID,以便在我的CAKEPHP 3.3站点上进行自动完成搜索。但我的问题是它只返回id,如果我输入完全id而不是它的一部分。

这是我搜索数据的功能。 name变量是从输入传递的内容。

public function search()
{
    if ($this->request->is('ajax')) 
    {
        $name = $this->request->query['term'];
        $resultArr = $this->Invoices->find('all', [
            'conditions' => ['Invoices.id LIKE' => ($name . '%')]
        ]);
        $resultsArr = [];
        foreach ($resultArr as $result) 
        {
             $resultsArr[] = ($result['id']);
        }

        $this->set('resultsArr', $resultsArr);
        // This line is what handles converting your array into json
        // To get this to work you must load the request handler
        $this->set('_serialize', ['resultsArr']);
    }
}

例如,表'5254'中有一个id,我输入id'52'的一部分,没有返回任何内容但是当我输入整个id'5254'时,返回id。 enter image description here

enter image description here

我不确定为什么会出现这种情况,因为在我的SQL查询中,我使用百分号表示在输入内容后输入任何字符。

这是我桌子的一部分 enter image description here

输入52时的SQL调试。

object(Cake\ORM\Query) {

    '(help)' => 'This is a Query object, to get the results execute or iterate it.',
    'sql' => 'SELECT Invoices.id AS `Invoices__id`, Invoices.start_date AS `Invoices__start_date`, Invoices.close_date AS `Invoices__close_date`, Invoices.customer_id AS `Invoices__customer_id`, Invoices.invoice_to_address AS `Invoices__invoice_to_address`, Invoices.ship_to_address AS `Invoices__ship_to_address`, Invoices.customer_contact_id AS `Invoices__customer_contact_id`, Invoices.aircraft_registration_id AS `Invoices__aircraft_registration_id`, Invoices.shipping_company_id AS `Invoices__shipping_company_id`, Invoices.notes AS `Invoices__notes`, Invoices.worksheet_notes AS `Invoices__worksheet_notes`, Invoices.closed AS `Invoices__closed`, Invoices.times_printed AS `Invoices__times_printed`, Invoices.payment_due AS `Invoices__payment_due`, Invoices.GST_rate AS `Invoices__GST_rate`, Invoices.opening_notes AS `Invoices__opening_notes`, Invoices.courier_ticket AS `Invoices__courier_ticket`, Invoices.job_description AS `Invoices__job_description`, Invoices.worksheets_printed AS `Invoices__worksheets_printed`, Invoices.supervising_engineer_id AS `Invoices__supervising_engineer_id`, Invoices.job_type_id AS `Invoices__job_type_id`, Invoices.opened_by_id AS `Invoices__opened_by_id`, Invoices.assigned_to_id AS `Invoices__assigned_to_id`, Invoices.certification_required AS `Invoices__certification_required`, Invoices.currency_id AS `Invoices__currency_id`, Invoices.xero_batch_number AS `Invoices__xero_batch_number`, Invoices.xero_amount AS `Invoices__xero_amount`, Invoices.exchange_rate AS `Invoices__exchange_rate`, Invoices.payment_instructions AS `Invoices__payment_instructions`, Invoices.email AS `Invoices__email`, Invoices.inv_email AS `Invoices__inv_email` FROM invoices Invoices WHERE Invoices.id like :c0',
    'params' => [
        ':c0' => [
            'value' => '52%',
            'type' => 'integer',
            'placeholder' => 'c0'
        ]

4 个答案:

答案 0 :(得分:2)

id列的类型为INTEGER,因此值正如此绑定,如查询转储中所示,它显示为'type' => 'integer'。绑定为整数将导致它被转换,并且您最终只会与52进行比较。

您可以通过告知查询构建器将列视为字符串类型来解决此问题。这可以通过查询构建器$types方法的第二个参数(*where())来完成:

$this->Invoices
    ->find()
    ->where(
        ['Invoices.id LIKE' => ($name . '%')],
        ['Invoices.id' => 'string']
    );

另见

答案 1 :(得分:0)

试试这样:

'conditions' => ['Invoices.id LIKE' => '"' . $name . '%"']

答案 2 :(得分:0)

在这种情况下,您可以“注入”普通查询 - conditions中带有数字索引的数组值被视为普通查询,并且不会进行参数化。注意:在这种情况下,必须使用Typecast到整数来防止SQL注入:

    $result = $this->Invoinces->find('all' , [
            'conditions' => [
                'id LIKE "'.(int)$input.'%" '
            ]
        ])
    ->toArray();

答案 3 :(得分:0)

您仍然可以在cakephp 3中这样做

$results = $clients->find()->select(['id','email','name','accountid','created','status'])
            ->Where(function (QueryExp $exp, Query $q) use ($requestData) {
                $orCond = $exp->or_([
                    new Comparison('accountid',$requestData['search']['value'],null,'LIKE'),
                    new Comparison('email',$requestData['search']['value'],null,'LIKE'),
                    new Comparison('name',$requestData['search']['value'],null,'LIKE'),
                    new Comparison('created',$requestData['search']['value'],null,'LIKE'),
                    new Comparison('status',$requestData['search']['value'],null,'LIKE'),
                ]);
                return $exp->add($orCond);
            });