简体   繁体   中英

Cakephp 3 case sensitive query

    $date = date("Y-m-d");
    $prev_date = date('m-d-Y', strtotime($date .' -1 day'));
    $q = "SELECT `id`, `external_id`, `status`, DATE_FORMAT(from_unixtime(`created`),'%m-%d-%Y') as crt, `extras` 
            FROM `gshuplogs` WHERE DATE_FORMAT(from_unixtime(`created`),'%m-%d-%Y') = '$prev_date' and
            (BINARY `status` in ('success','DEFERRED') or `status` IS NULL)";
    $conn = \Cake\Datasource\ConnectionManager::get('default');
    $res = $conn->execute($q)->fetchAll('assoc');
    debug($res);exit;

The above query I have written in mysql now I just wanted to write in cakephp 3.

I have tried

$res = $this->Gshuplogs->find()->select([
        'id', 'external_id', 'status', 'created'
    ])->where([
        "DATE_FORMAT(from_unixtime(`created`),'%m-%d-%Y')" => $prev_date,
        'OR' => [
            ['LOWER status' => 'success'],
            ['status' => 'DEFERRED'],
            ['status IS NULL']
        ]
    ]);

AND

$res = $this->Gshuplogs->find()->select([
        'id', 'external_id', 'status', 'created'
    ])->where([
        "DATE_FORMAT(from_unixtime(`created`),'%m-%d-%Y')" => $prev_date,
        'OR' => [
            ['BINARY status' => 'success'],
            ['status' => 'DEFERRED'],
            ['status IS NULL']
        ]
    ]);

But it is throwing error.

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' `),'%m-%d-%Y'`) = '06-01-2016' AND (`LOWER` status 'success' OR `status` = 'DEFE ' at line 1

Thanks for your response. I got the solution.

    $date = date("Y-m-d");
    $prev_date = date('m-d-Y', strtotime($date .' -1 day'));

    $res = $this->Gshuplogs->find()->select([
        'id', 'external_id', 'status', "created" 
    ])->where([
        "FROM_UNIXTIME(`created`,'%m-%d-%Y')" => $prev_date,
        'OR' => [
            ['BINARY(status) in ' => ['success','DEFERRED']],
            ['status IS NULL']
        ]
    ]);

    debug($res->toArray());exit;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM