Created
April 30, 2017 20:53
-
-
Save fdcore/cba3bc77bd31e1c1b14150add6e038fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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