-
Notifications
You must be signed in to change notification settings - Fork 0
/
CI_Eloquent.php
82 lines (74 loc) · 1.68 KB
/
CI_Eloquent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* @author anthony.ras
* Codeigniter 3 Eloquent ORM module
*/
class CI_Eloquent extends CI_Model
{
protected $attributes = NULL;
protected $retrievable = [];
protected $except = [];
protected $config;
function __construct($config = NULL)
{
if( is_null( $this->attributes ) )
{
$this->attributes = new \stdClass;
}
if( !is_null($config ))
{
$this->config = $config;
}
}
function __set($key, $value)
{
$this->attributes{$key} = $value;
}
/**
* everytime you call the object it will check if there is a ci key on that
* if none it will check the attributes.
* @param [type] $key [description]
* @return [type] [description]
*/
function __get($key)
{
if(property_exists(get_called_class(), $key))
{
return $this->{$key};
}
return $this->attributes->{$key};
}
/**
* @author anthony.ras
*
* This will still be effiecient since we are still using the singleton method of CI
*
* @param String $method name of the method called statically
* @param Array $args Arguments being passed.
* @return Object newInstance returns the new instance of the object.
*/
static function __callStatic($method, $args)
{
$class = get_called_class();
$caller = $method;
if(!method_exists($class, $caller))
{
throw new \Error("Method Not Found {$caller}", 1);
}
$class_instance = new $class;
return $class_instance->{$caller}( ...$args );
}
private function find(int $id = 0)
{
$ci =& get_instance();
$this->attributes = $ci->db
->where($this->primary_key, $id)
->get($this->table)
->row();
return $this;
}
public function get_attributes()
{
return $this->attributes;
}
}