views:

135

answers:

3

Hello,

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?

+1  A: 

Try using this:

$this->db->set('field', 'field+1', FALSE);
Thorpe Obazee
A: 

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 ?

Lee
A: 
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)
}
Phil Sturgeon