206 lines
4.8 KiB
PHP
206 lines
4.8 KiB
PHP
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
/**
|
|
* Description of MY_Model
|
|
* Algemene database functies
|
|
*
|
|
* @author Theo Weijmer
|
|
* @date 12-07-2011
|
|
*
|
|
* my_model VHM versie 2.1
|
|
*
|
|
*/
|
|
|
|
|
|
class MY_Model extends CI_Model{
|
|
|
|
function __construct(){
|
|
parent::__construct();
|
|
// Disable ONLY_FULL_GROUP_BY for legacy queries
|
|
$this->db->query("SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''))");
|
|
}
|
|
|
|
function get_record_by_id($id, $fieldname=FALSE, $row=TRUE){
|
|
$query = $this->db->get_where($this->tablename, array('id' => $id));
|
|
if($query->num_rows()==0){
|
|
return FALSE;
|
|
}elseif($row){
|
|
$row = $query->row();
|
|
if($fieldname){
|
|
return $row->$fieldname;
|
|
}else{
|
|
return $row;
|
|
}
|
|
}else{
|
|
return $query->result();
|
|
}
|
|
}
|
|
|
|
function get_fieldvalue($where, $fieldname){
|
|
$query = $this->db->get_where($this->tablename, $where);
|
|
if($query->num_rows()==0){
|
|
return FALSE;
|
|
}else{
|
|
$row = $query->row();
|
|
return $row->$fieldname;
|
|
}
|
|
}
|
|
|
|
function get_records_where($where=FALSE, $row=FALSE, $order=FALSE, $sort='asc'){
|
|
if($order) $this->db->order_by($order, $sort);
|
|
if($where){
|
|
$query = $this->db->get_where($this->tablename, $where);
|
|
}else{
|
|
$query = $this->db->get($this->tablename);
|
|
}
|
|
// echo $this->db->last_query();
|
|
if($query->num_rows()==0){
|
|
return FALSE;
|
|
}else{
|
|
if($row){
|
|
return $query->row();
|
|
}else{
|
|
return $query->result();
|
|
}
|
|
}
|
|
}
|
|
|
|
function check_for_record($where){
|
|
$query = $this->db->get_where($this->tablename, $where);
|
|
if($query->num_rows()==0){
|
|
return FALSE;
|
|
}else{
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
function get_query_result(){
|
|
$query = $this->db->query($this->sql);
|
|
if($query === FALSE){
|
|
$error = $this->db->error();
|
|
log_message('error', 'Database query failed: ' . $error['message'] . ' | SQL: ' . $this->sql);
|
|
// Tijdelijk ook direct tonen voor debugging
|
|
if(ENVIRONMENT !== 'production'){
|
|
echo "SQL Error: " . $error['message'] . "\n";
|
|
echo "SQL: " . $this->sql . "\n";
|
|
}
|
|
return FALSE;
|
|
}
|
|
$num_rows = $query->num_rows();
|
|
// Tijdelijk loggen voor debugging
|
|
if(ENVIRONMENT !== 'production'){
|
|
log_message('debug', 'Query returned ' . $num_rows . ' rows | SQL: ' . substr($this->sql, 0, 100));
|
|
}
|
|
if($num_rows == 0){
|
|
return FALSE;
|
|
}else{
|
|
return $query->result();
|
|
}
|
|
}
|
|
|
|
function check_query_record(){
|
|
$query = $this->db->query($this->sql);
|
|
if($query->num_rows()==0){
|
|
return FALSE;
|
|
}else{
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
function get_query_row($fieldname=FALSE){
|
|
$query = $this->db->query($this->sql);
|
|
if($query->num_rows()==0){
|
|
return FALSE;
|
|
}else{
|
|
if($fieldname){
|
|
$row = $query->row();
|
|
return $row->$fieldname;
|
|
}else{
|
|
return $query->row();
|
|
}
|
|
}
|
|
}
|
|
|
|
function get_result_count($where){
|
|
$this->db->where($where);
|
|
$this->db->from($this->tablename);
|
|
return $this->db->count_all_results();
|
|
}
|
|
|
|
function update_record($data, $id){
|
|
$data['update_date']=date('Y-m-d G:i:s');
|
|
$data['updated_by']=$this->Users_model->gID;
|
|
$this->db->update($this->tablename, $data, 'id = '.$id);
|
|
|
|
}
|
|
|
|
function update_record_where($data, $where){
|
|
$data['update_date']=date('Y-m-d G:i:s');
|
|
$data['updated_by']=$this->Users_model->gID;
|
|
$this->db->update($this->tablename, $data, $where);
|
|
}
|
|
|
|
function insert_record($data){
|
|
$data['creation_date']=date('Y-m-d G:i:s');
|
|
$data['created_by']=$this->Users_model->gID;
|
|
$data['update_date']=date('Y-m-d G:i:s');
|
|
$data['updated_by']=$this->Users_model->gID;
|
|
$this->db->insert($this->tablename, $data);
|
|
return $this->db->insert_id();
|
|
}
|
|
|
|
function delete_record($id){
|
|
$this->db->delete($this->tablename, array('id'=>$id));
|
|
}
|
|
|
|
function delete_record_where($where){
|
|
$this->db->delete($this->tablename, $where);
|
|
}
|
|
|
|
function sanitise_postinput($post, $notinclude){
|
|
foreach($post as $key=>$value){
|
|
if(!in_array($key, $notinclude))$data[] = array($key=>$value);
|
|
}
|
|
}
|
|
|
|
function save_record($data, $id){
|
|
if(!$id){
|
|
$id = $this->insert_record($data);
|
|
}else{
|
|
$this->update_record($data, $id);
|
|
}
|
|
return $id;
|
|
}
|
|
|
|
function get_ar_query(){
|
|
$query = $this->db->get();
|
|
if($query->num_rows()==0){
|
|
return FALSE;
|
|
}else{
|
|
return $query->result();
|
|
}
|
|
}
|
|
|
|
function get($where = FALSE, $fieldname = FALSE, $row = FALSE) {
|
|
if( $fieldname ) $row = TRUE;
|
|
|
|
if ( ! is_array( $where ) && $where ) {
|
|
$this->db->where( array('T1.id' => $where) );
|
|
$row = TRUE;
|
|
} elseif ( $where ) {
|
|
$this->db->where( $where );
|
|
}
|
|
|
|
if ( $fieldname ) $this->db->select( $fieldname );
|
|
$query = $this->db->get( $this->tablename . ' AS T1' );
|
|
if ( ! $query || $query->num_rows() == 0 ) return FALSE;
|
|
|
|
if ( $row ){
|
|
$row = $query->row();
|
|
if ( $fieldname ) { return $row->$fieldname; }
|
|
else { return $row; }
|
|
}
|
|
else { return $query->result(); }
|
|
}
|
|
|
|
} |