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

class MY_Model extends CI_Model {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	protected $tb_name = "";
	
	public function insert(array $row)
	{
		$this->db->trans_start();
		$this->db->insert($this->tb_name, $row);
		$this->db->trans_commit();
		$this->db->trans_complete();
        return $this->db->insert_id();
	}

	public function get($id){
		$query = $this->db->select("*")
						  ->from($this->tb_name)
				          ->where('id', $id)
				          ->get();
		if($query->num_rows() > 0){
			return array("success"=>TRUE, "data"=>$query->row_array());
		}else{
			return array("success"=>FALSE, "data"=>array());
		}
	}

	public function get_all(){
		$query = $this->db->select("*")
						  ->from($this->tb_name)
				          ->get();
		if($query->num_rows() > 0){
			return array("success"=>TRUE, "data"=>$query->result_array());
		}else{
			return array("success"=>FALSE, "data"=>array());
		}
	}

	public function update(array $data){
		$result = $this->db->where("id", $data["id"])
						   ->update($this->tb_name, $data);
		return $result;
	}

	public function delete($id){
		$result = $this->db->where("id", $id)
                           ->delete($this->tb_name);
        return $result;
    }
    
    public function insert_batch(array $data){
        $result = $this->db->insert_batch($this->tb_name, $data);
        return $this->db->affected_rows();
    }
}
