【问题标题】:how to remove single quotes inside BETWEEN condition cakephp如何删除 BETWEEN 条件 cakephp 中的单引号
【发布时间】:2019-04-06 12:14:31
【问题描述】:

我正在尝试将单引号与 cakephp 2 中 BETWEEN 条件内的值分开,但它总是获取带有单引号的值,例如 BETWEEN '2' 和 '34' 。 这是我的代码:

$ref_no1 = $this->request->data['Lead']['ref_no1'];
$ref_no2 = $this->request->data['Lead']['ref_no2'];

$customerHo = $this-> Customer -> find('all',array(
'order' => array('Customer.customer_name' => 'asc'),
'joins'=>array( 
array('table'=>'leads','alias'=>'Lead','type'=>'LEFT','foreignKey'=>false,'conditions'=>array('Customer.customer_id = Lead.customer_id')),
),
'fields'=>'Customer.*,Lead.*',
'conditions' => array(
'Customer.status' => 'active','Customer.customer_id Like'=>'S%',
'Customer.company_id'=>$company_id,
'AND' => array(
array('Lead.ref_no BETWEEN ? and ?' => array($ref_no1,$ref_no2) ),
),
)));

我得到如下输出:

SELECT `Customer`.*, `Lead`.*
FROM `customers` AS `Customer`
LEFT JOIN `timezip_db_demo`.`leads` AS `Lead` ON (`Customer`.`customer_id` = `Lead`.`customer_id`)
WHERE  `Customer`.`customer_id` Like 'S%' AND
`Lead`.`ref_no` BETWEEN '2' and '34' 
ORDER BY `Customer`.`customer_name` asc

预期输出:

SELECT `Customer`.*, `Lead`.* 
FROM `customers` AS `Customer`  
LEFT JOIN `timezip_db_demo`.`leads` AS `Lead` ON (`Customer`.`customer_id` = `Lead`.`customer_id`)
WHERE  `Customer`.`customer_id` Like 'S%' AND 
`Lead`.`ref_no` BETWEEN 2 and 34 
ORDER BY `Customer`.`customer_name` asc

【问题讨论】:

    标签: cakephp cakephp-2.0


    【解决方案1】:

    你就不能这样吗?我还没有测试这段代码。

    $ref_no1 = $this->request->data['Lead']['ref_no1'];
    $ref_no2 = $this->request->data['Lead']['ref_no2'];
    
    $customerHo = $this-> Customer -> find('all',array(
    'order' => array('Customer.customer_name' => 'asc'),
    'joins'=>array( 
    array('table'=>'leads','alias'=>'Lead','type'=>'LEFT','foreignKey'=>false,'conditions'=>array('Customer.customer_id = Lead.customer_id')),
    ),
    'fields'=>'Customer.*,Lead.*',
    'conditions' => array(
    'Customer.status' => 'active','Customer.customer_id Like'=>'S%',
    'Customer.company_id'=>$company_id,
    'AND' => array(
    array('Lead.ref_no BETWEEN' . $ref_no1 . 'and' . $ref_no2 ),
    ),
    )));
    

    【讨论】:

      【解决方案2】:

      你可以把那个字符串转换成整数,我希望它能工作

       array((int)$ref_no1,(int)$ref_no2)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-28
        相关资源
        最近更新 更多