开发者

Codeigniter Active record help

I am trying to increment a INT column by 1 if a certain field is not null on an update request, currently I have this update too columns,

public function updateCronDetails($transaction_reference, $flag, $log开发者_运维知识库) {
    $data = array (
        'flag' => $flag,
        'log' => "$log"
    );

    $this->db->where('transaction_reference', $transaction_reference);
    $this->db->update('sy_cron', $data);
}

What I need to know is how I can check if the value being sent to the log field is NULL and if it is how could I increment a column called count by 1?


Try using this:

$this->db->set('field', 'field+1', FALSE);


public function updateCronDetails($transaction_reference, $flag, $log = NULL)
{    
    // Only increment this field if not null
    is_null($log) || $this->db->set('field', 'field+1', FALSE);

    $this->db
        ->set('flag', $flag)
        ->set('log', $log)
        ->where('transaction_reference', $transaction_reference)
        ->update('sy_cron', $data)
}


How about using a ternary operator i.e

public function updateCronDetails($transaction_reference, $flag, $log) {
    $data = array (
        'flag' => $flag,
        'log' => (is_null($log))? 1 : $log,
    );

    $this->db->where('transaction_reference', $transaction_reference);
    $this->db->update('sy_cron', $data);
}

As if its null then the value would need to be ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜