Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* The global model! All hail potentate MY_Controller!
* It provides some great default queries...
*/
class MY_Model extends Model
{
var $default_table;
function get_all()
{
// set up the query
$query = $this->db->get($this->default_table);
// run the query
$data = $query->result();
// return data
return $data;
}
function get_one($id)
{
// set up the query
$query = $this->db->get_where($this->default_table, array('id' => $id));
// run the query
$data = $query->result();
// return data
if (!empty($data)) return $data[0];
else return FALSE;
}
function add($data)
{
// simple: just save it, return if success
return $this->db->insert($this->default_table, $data);
}
function save($data, $id)
{
$this->db->where('id', $id);
return $this->db->update($this->default_table, $data);
}
function delete($id)
{
return $this->db->delete($this->default_table, array('id' => $id));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment