<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Common_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        date_default_timezone_set("Asia/Ho_Chi_Minh");
    }

    public function update($table, $id, $data_update)
    {
        try{
            $this->db->where('id', $id);
            $this->db->update($table, $data_update);
            return true;
        } catch (Exception $e) {
            return false;
        }
    }

    /**
     * https://codeigniter.com/userguide3/database/query_builder.html#updating-data
     *
     * @param $table
     * @param $data
     * @param string $update_by_field
     * @return bool
     */
    public function update_batch($table, $data, $update_by_field = 'id')
    {
        if ($table && is_array($data) && count($data) > 0) {
            try {
                $this->db->update_batch($table, $data, $update_by_field);
                return true;
            } catch (Exception $e) {
                return false;
            }
        } else {
            return false;
        }
    }

    public function delete($table, $id)
    {
        if ($table && $id > 0) {
            $this->db->where('id', $id);
            $this->db->delete($table);
            //
            return true;
        } else {
            return false;
        }
    }

}