Search code examples
codeigniteractiverecordquotesskip

How to skip quotes from code igniter active record update in order to use if condition in update?


I am writing query like this :-

$data = array(
    'company_billing' => "if (company_billing  = $userId, (NULL), $userId)",
    'company_admin' => "if (company_admin  = $userId, (NULL), $userId)",
);

$this->db->where('id', $organizationId);
$this->db->update('organization', $data);

I was expecting something like :-

UPDATE `organization` SET `organization`.`company_billing` = if (company_billing = 2, (NULL), 2), `organization`.`company_admin` = if (company_admin = 2, (NULL), 2) WHERE `id` = 1

But I am getting :-

UPDATE `organization` SET `organization`.`company_billing` = 'if (company_billing = 2, (NULL), 2)', `organization`.`company_admin` = 'if (company_admin = 2, (NULL), 2)' WHERE `id` = 1

I want to skip those quotes around if condition block.

Is there any way to achieve this or I will have to write native query?

Thanks


Solution

  • ::update and ::insert function values are escaped automatically producing safer queries.

    $this->db->set('company_billing' , "if (company_billing  = $userId, (NULL), $userId)", false);
    $this->db->set('company_admin', "if (company_admin  = $userId, (NULL), $userId)", false);
    $this->db->where('id', $organizationId);
    $this->db->update('organization');