1084

update with where clause !

Comments for “update with where clause !”
 

Posted by investobd on Monday 4th April 2022 at 10:09 GMT

Hello Good day,
Is this possible to use update database with custom where clause like codeignitor ..

investobd

User Level: Guest

Date Joined: 10/12/2021

Posted by Davcon on Wednesday 6th April 2022 at 01:32 GMT

Hi. Could you please give an example, using Codeigniter syntax?
Founding Member

Davcon

User Level: Founding Member

Date Joined: 3/11/2018

Posted by DaFa on Wednesday 6th April 2022 at 04:26 GMT

Hi, as DC said above, an example of a custom CI where query would be good, also what version of CI are you using?

I suggest you go to the Trongate docs here:
> https://trongate.io/docs_m/information/the-get-where-custom-method
it is very well explained with very clear examples:
$rows = $this->model->get_where_custom('first_name', 'Bruce');

For CI4, working with the query builder isn't as `elegant` as the docs try to express:
> https://codeigniter.com/user_guide/models/model.html#working-with-query-builder
$users = $userModel->where('status', 'active')
                   ->orderBy('last_login', 'asc')
                   ->findAll();

This comment was edited by DaFa on Wednesday 6th April 2022 at 04:30 GMT

Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

Posted by investobd on Wednesday 6th April 2022 at 09:23 GMT

Hello Good day,
Like this
$this->db->where('instrument_id', $instrument_id);
        if ($this->db->get('dse_financial_key')->num_rows()) {
            // The record already exists; update it
         $this->db->where('instrument_id', $instrument_id);
            $this->db->update('dse_financial_key', $db_array);
        }
       else {
          // The record does not yet exist; insert it
         $this->db->insert('dse_financial_key', $db_array);
            
        }

investobd

User Level: Guest

Date Joined: 10/12/2021

Posted by DaFa on Wednesday 6th April 2022 at 11:01 GMT

That's better now I can see what you are doing.

All the information you need are in the docs, I suggest you read 'Database Interaction':
> https://trongate.io/docs_m/contents

In your example, this is what the code would look like using Trongate:
$num_rows = $this->model->count_where('instrument_id', $instrument_id, '=', 'instrument_id', 'dse_financial_key');
if ($num_rows > 0) {
     //update an existing record
     $this->model->update($instrument_id, $data, 'dse_financial_key');
} else {
     //insert the new record
     $instrument_id = $this->model->insert($data, 'dse_financial_key');
}


Hope that helped

This comment was edited by DaFa on Wednesday 6th April 2022 at 11:07 GMT

Founding Member

DaFa

User Level: Founding Member

Date Joined: 30/11/2018

×