Skip to content

Instantly share code, notes, and snippets.

@sanPuerquitoProgramador
Last active February 11, 2018 02:32
Show Gist options
  • Save sanPuerquitoProgramador/45bc74a5f38691966a4b17656999e8bc to your computer and use it in GitHub Desktop.
Save sanPuerquitoProgramador/45bc74a5f38691966a4b17656999e8bc to your computer and use it in GitHub Desktop.
Base Model for CodeIgniter. It's assumes your tables has crated_at and updated_at fields. The Insert and Update functions fill and updated those fields automatically
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Base_model extends CI_Model{
/**
* { function_description }
*/
function __construct()
{
parent::__construct();
}
/**
* { function_description }
*
* @param <type> $tabla The tabla
* @param <type> $data The data
*
* @return <type> ( description_of_the_return_value )
*/
public function onlyInsert($tabla, $data)
{
$this->timeStamp($tabla);
$this->db->insert($tabla, $data);
return $this->db->insert_id();
}
/**
* { function_description }
*
* @param <type> $tabla The tabla
* @param <type> $where The where
*
* @return <type> ( description_of_the_return_value )
*/
public function onlyGet($tabla, $where=NULL)
{
if ($where!=NULL) {
$query = $this->db->get_where($tabla,$where);
} else {
$query = $this->db->get($tabla);
}
return ($query->num_rows() != 0 ? $query->result() : FALSE);
}
/**
* { function_description }
*
* @param <type> $tabla The tabla
* @param <type> $data The data
*
* @return <type> ( description_of_the_return_value )
*/
public function onlyUpdate($tabla, $data)
{
$this->timeStamp($tabla,FALSE);
$this->db->where($data['where']);
return $this->db->update($tabla, $data['info']);
}
/**
* { function_description }
*
* @param <type> $tabla The tabla
* @param <type> $where The where
*
* @return <type> ( description_of_the_return_value )
*/
public function onlyDelete($tabla, $where)
{
return $this->db->delete($tabla, $where);
}
/**
* { function_description }
*
* @param <type> $table The table
* @param <type> $crea The crea
*/
public function timeStamp($table,$crea=TRUE)
{
if ($crea==TRUE) {
if ($this->db->field_exists('created_at', $table)) {
$this->db->set('created_at', 'NOW()', FALSE);
}
}
if ($this->db->field_exists('updated_at', $table)) {
$this->db->set('updated_at', 'NOW()', FALSE);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment