Skip to content

Instantly share code, notes, and snippets.

@fdcore
Created April 30, 2017 20:53
Show Gist options
  • Save fdcore/cba3bc77bd31e1c1b14150add6e038fb to your computer and use it in GitHub Desktop.
Save fdcore/cba3bc77bd31e1c1b14150add6e038fb to your computer and use it in GitHub Desktop.
<?php
class Example extends CI_Model
{
const TABLE = 'example';
private $CI = null;
private $data = [];
function __construct($id = null)
{
parent::__construct();
$this->CI =& get_instance();
if($id) {
$this->set_id($id);
}
}
function set_id($id)
{
if($id) {
$this->data = $this->CI->s->from(self::TABLE)->where(array('id' => $id))->one();
}
}
static function by_id($id)
{
return new Example($id);
}
public function __get($name)
{
if (isset($this->data[$name])) {
return $this->data[$name];
}
return FALSE;
}
public function __set($name, $value)
{
$this->data[$name] = $value;
}
public function save()
{
if(isset($this->data['id'])){
$this->CI->s->from(self::TABLE)->where(array('id' => $this->id))->update($this->data)->execute();
}
}
public function other_method()
{
// code..
}
}
$class = Example::by_id(1);
$class->status = 'success';
$class->save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment