123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474 |
- <?php namespace System\DB;
- use System\Str;
- use System\Config;
- use System\Inflector;
- abstract class Eloquent {
-
- public $exists = false;
-
- public $attributes = array();
-
- public $dirty = array();
-
- public $ignore = array();
-
- public $includes = array();
-
- public $relating;
-
- public $relating_key;
-
- public $relating_table;
-
- public $query;
-
- public function __construct($attributes = array())
- {
- foreach ($attributes as $key => $value)
- {
- $this->$key = $value;
- }
- }
-
- public static function table($class)
- {
- if (property_exists($class, 'table'))
- {
- return $class::$table;
- }
- return strtolower(Inflector::plural($class));
- }
-
- public static function make($class)
- {
- $model = new $class;
-
-
- $model->query = Query::table(static::table($class));
- return $model;
- }
-
- public static function with()
- {
- $model = static::make(get_called_class());
- $model->includes = func_get_args();
- return $model;
- }
-
- public static function find($id)
- {
- return static::make(get_called_class())->where('id', '=', $id)->first();
- }
-
- private function _get()
- {
- return Eloquent\Hydrator::hydrate($this);
- }
-
- private function _first()
- {
- return (count($results = Eloquent\Hydrator::hydrate($this->take(1))) > 0) ? reset($results) : null;
- }
-
- public function has_one($model, $foreign_key = null)
- {
- $this->relating = __FUNCTION__;
- return $this->has_one_or_many($model, $foreign_key);
- }
-
- public function has_many($model, $foreign_key = null)
- {
- $this->relating = __FUNCTION__;
- return $this->has_one_or_many($model, $foreign_key);
- }
-
- private function has_one_or_many($model, $foreign_key)
- {
- $this->relating_key = (is_null($foreign_key)) ? strtolower(get_class($this)).'_id' : $foreign_key;
- return static::make($model)->where($this->relating_key, '=', $this->id);
- }
-
- public function belongs_to($model, $foreign_key = null)
- {
- $this->relating = __FUNCTION__;
- if ( ! is_null($foreign_key))
- {
- $this->relating_key = $foreign_key;
- }
- else
- {
- list(, $caller) = debug_backtrace(false);
- $this->relating_key = $caller['function'].'_id';
- }
- return static::make($model)->where('id', '=', $this->attributes[$this->relating_key]);
- }
-
- public function has_and_belongs_to_many($model, $table = null)
- {
- $this->relating = __FUNCTION__;
- if (is_null($table))
- {
- $models = array(Inflector::plural($model), Inflector::plural(get_class($this)));
- sort($models);
- $this->relating_table = strtolower($models[0].'_'.$models[1]);
- }
- else
- {
- $this->relating_table = $table;
- }
- $this->relating_key = strtolower(get_class($this)).'_id';
- $relationship = static::make($model);
- $relationship->select(array(static::table($model).'.*'));
- $relationship->join($this->relating_table, static::table($model).'.id', '=', $this->relating_table.'.'.strtolower($model).'_id');
- $relationship->where($this->relating_table.'.'.$this->relating_key, '=', $this->id);
- return $relationship;
- }
-
- public function save()
- {
- if ($this->exists and count($this->dirty) == 0)
- {
- return true;
- }
- $model = get_class($this);
-
-
- $this->query = Query::table(static::table($model));
- if (property_exists($model, 'timestamps') and $model::$timestamps)
- {
- $this->timestamp();
- }
- if ($this->exists)
- {
- $result = $this->query->where('id', '=', $this->attributes['id'])->update($this->dirty) == 1;
- }
- else
- {
- $this->attributes['id'] = $this->query->insert_get_id($this->attributes);
- $result = $this->exists = is_numeric($this->id);
- }
- $this->dirty = array();
- return $result;
- }
-
- public function delete($id = null)
- {
- if ($this->exists)
- {
- return Query::table(static::table(get_class($this)))->delete($this->id);
- }
- return $this->query->delete();
- }
-
- private function timestamp()
- {
- $this->updated_at = date('Y-m-d H:i:s');
- if ( ! $this->exists)
- {
- $this->created_at = $this->updated_at;
- }
- }
-
- public function __get($key)
- {
-
- if (array_key_exists($key, $this->ignore))
- {
- return $this->ignore[$key];
- }
-
- if (method_exists($this, $key))
- {
- $model = $this->$key();
- if (in_array($this->relating, array('has_one', 'belongs_to')))
- {
- return $this->ignore[$key] = $model->first();
- }
- return $this->ignore[$key] = $model->get();
- }
- return (array_key_exists($key, $this->attributes)) ? $this->attributes[$key] : null;
- }
-
- public function __set($key, $value)
- {
-
- if (method_exists($this, $key))
- {
- $this->ignore[$key] = $value;
- }
- else
- {
- $this->attributes[$key] = $value;
- $this->dirty[$key] = $value;
- }
- }
-
- public function __isset($key)
- {
- return (array_key_exists($key, $this->attributes) or array_key_exists($key, $this->ignore));
- }
-
- public function __unset($key)
- {
- unset($this->attributes[$key]);
- unset($this->ignore[$key]);
- unset($this->dirty[$key]);
- }
-
- public function __call($method, $parameters)
- {
- if ($method == 'get')
- {
- return $this->_get();
- }
- if ($method == 'first')
- {
- return $this->_first();
- }
- if (in_array($method, array('count', 'sum', 'min', 'max', 'avg')))
- {
- return call_user_func_array(array($this->query, $method), $parameters);
- }
-
-
- call_user_func_array(array($this->query, $method), $parameters);
- return $this;
- }
-
- public static function __callStatic($method, $parameters)
- {
- $model = static::make(get_called_class());
- if ($method == 'get' or $method == 'all')
- {
- return $model->_get();
- }
- if ($method == 'first')
- {
- return $model->_first();
- }
- if (in_array($method, array('count', 'sum', 'min', 'max', 'avg')))
- {
- return call_user_func_array(array($model->query, $method), $parameters);
- }
-
-
- call_user_func_array(array($model->query, $method), $parameters);
- return $model;
- }
- }
|