123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733 |
- <?php namespace Laravel\Database\Eloquent;
- use Laravel\Str;
- use Laravel\Database;
- use Laravel\Database\Eloquent\Relationships\Has_Many_And_Belongs_To;
- abstract class Model {
-
- public $attributes = array();
-
- public $original = array();
-
- public $relationships = array();
-
- public $exists = false;
-
- public $includes = array();
-
- public static $key = 'id';
-
- public static $accessible;
-
- public static $hidden = array();
-
- public static $timestamps = true;
-
- public static $table;
-
- public static $connection;
-
- public static $sequence;
-
- public static $per_page = 20;
-
- public function __construct($attributes = array(), $exists = false)
- {
- $this->exists = $exists;
- $this->fill($attributes);
- }
-
- public function fill($attributes)
- {
- $attributes = (array) $attributes;
- foreach ($attributes as $key => $value)
- {
-
-
-
- if (is_array(static::$accessible))
- {
- if (in_array($key, static::$accessible))
- {
- $this->$key = $value;
- }
- }
-
-
-
- else
- {
- $this->$key = $value;
- }
- }
-
-
-
- if (count($this->original) === 0)
- {
- $this->original = $this->attributes;
- }
- return $this;
- }
-
- public static function accessible($attributes)
- {
- static::$accessible = $attributes;
- }
-
- public static function create($attributes)
- {
- $model = new static($attributes);
- $success = $model->save();
- return ($success) ? $model : false;
- }
-
- public static function update($id, $attributes)
- {
- $model = new static(array(), true);
- if (static::$timestamps) $attributes['updated_at'] = new \DateTime;
- return $model->query()->where($model->key(), '=', $id)->update($attributes);
- }
-
- public static function find($id, $columns = array('*'))
- {
- $model = new static;
- return $model->query()->where(static::$key, '=', $id)->first($columns);
- }
-
- public static function all()
- {
- return with(new static)->query()->get();
- }
-
- public function _with($includes)
- {
- $this->includes = (array) $includes;
- return $this;
- }
-
- public function has_one($model, $foreign = null)
- {
- return $this->has_one_or_many(__FUNCTION__, $model, $foreign);
- }
-
- public function has_many($model, $foreign = null)
- {
- return $this->has_one_or_many(__FUNCTION__, $model, $foreign);
- }
-
- protected function has_one_or_many($type, $model, $foreign)
- {
- if ($type == 'has_one')
- {
- return new Relationships\Has_One($this, $model, $foreign);
- }
- else
- {
- return new Relationships\Has_Many($this, $model, $foreign);
- }
- }
-
- public function belongs_to($model, $foreign = null)
- {
-
-
-
- if (is_null($foreign))
- {
- list(, $caller) = debug_backtrace(false);
- $foreign = "{$caller['function']}_id";
- }
- return new Relationships\Belongs_To($this, $model, $foreign);
- }
-
- public function has_many_and_belongs_to($model, $table = null, $foreign = null, $other = null)
- {
- return new Has_Many_And_Belongs_To($this, $model, $table, $foreign, $other);
- }
-
- public function push()
- {
- $this->save();
-
-
-
- foreach ($this->relationships as $name => $models)
- {
- if ( ! is_array($models))
- {
- $models = array($models);
- }
- foreach ($models as $model)
- {
- $model->push();
- }
- }
- }
-
- public function save()
- {
- if ( ! $this->dirty()) return true;
- if (static::$timestamps)
- {
- $this->timestamp();
- }
-
-
-
- if ($this->exists)
- {
- $query = $this->query()->where(static::$key, '=', $this->get_key());
- $result = $query->update($this->get_dirty()) === 1;
- }
-
-
-
- else
- {
- $id = $this->query()->insert_get_id($this->attributes, $this->sequence());
- $this->set_key($id);
- $this->exists = $result = is_numeric($this->get_key());
- }
-
-
-
- $this->original = $this->attributes;
- return $result;
- }
-
- public function delete()
- {
- if ($this->exists)
- {
- return $this->query()->where(static::$key, '=', $this->get_key())->delete();
- }
- }
-
- protected function timestamp()
- {
- $this->updated_at = new \DateTime;
- if ( ! $this->exists) $this->created_at = $this->updated_at;
- }
-
- protected function query()
- {
- return new Query($this);
- }
-
- final public function sync()
- {
- $this->original = $this->attributes;
- return true;
- }
-
- public function changed($attribute)
- {
- return array_get($this->attributes, $attribute) !== array_get($this->original, $attribute);
- }
-
- public function dirty()
- {
- return ! $this->exists or count($this->get_dirty()) > 0;
- }
-
- public function table()
- {
- return static::$table ?: strtolower(Str::plural(class_basename($this)));
- }
-
- public function get_dirty()
- {
- $dirty = array();
- foreach ($this->attributes as $key => $value)
- {
- if ( ! isset($this->original[$key]) or $value !== $this->original[$key])
- {
- $dirty[$key] = $value;
- }
- }
- return $dirty;
- }
-
- public function get_key()
- {
- return $this->get_attribute(static::$key);
- }
-
- public function set_key($value)
- {
- return $this->set_attribute(static::$key, $value);
- }
-
- public function get_attribute($key)
- {
- return array_get($this->attributes, $key);
- }
-
- public function set_attribute($key, $value)
- {
- $this->attributes[$key] = $value;
- }
-
- final public function purge($key)
- {
- unset($this->original[$key]);
- unset($this->attributes[$key]);
- }
-
- public function to_array()
- {
- $attributes = array();
-
-
-
- foreach (array_keys($this->attributes) as $attribute)
- {
- if ( ! in_array($attribute, static::$hidden))
- {
- $attributes[$attribute] = $this->$attribute;
- }
- }
- foreach ($this->relationships as $name => $models)
- {
-
-
-
- if ( ! is_array($models))
- {
- $attributes[$name] = $models->to_array();
- }
-
-
-
- else
- {
- foreach ($models as $id => $model)
- {
- $attributes[$name][$id] = $model->to_array();
- }
- }
- }
- return $attributes;
- }
-
- public function __get($key)
- {
-
-
-
- if (array_key_exists($key, $this->relationships))
- {
- return $this->relationships[$key];
- }
-
-
-
- elseif (array_key_exists($key, $this->attributes))
- {
- return $this->{"get_{$key}"}();
- }
-
-
-
- elseif (method_exists($this, $key))
- {
- return $this->relationships[$key] = $this->$key()->results();
- }
-
-
-
- else
- {
- return $this->{"get_{$key}"}();
- }
- }
-
- public function __set($key, $value)
- {
- $this->{"set_{$key}"}($value);
- }
-
- public function __isset($key)
- {
- foreach (array('attributes', 'relationships') as $source)
- {
- if (array_key_exists($key, $this->$source)) return true;
- }
-
- if (method_exists($this, $key)) return true;
- }
-
- public function __unset($key)
- {
- foreach (array('attributes', 'relationships') as $source)
- {
- unset($this->$source[$key]);
- }
- }
-
- public function __call($method, $parameters)
- {
- $meta = array('key', 'table', 'connection', 'sequence', 'per_page', 'timestamps');
-
-
-
- if (in_array($method, $meta))
- {
- return static::$$method;
- }
-
-
-
- if (in_array($method, array('with')))
- {
- return call_user_func_array(array($this, '_'.$method), $parameters);
- }
-
-
-
- if (starts_with($method, 'get_'))
- {
- return $this->get_attribute(substr($method, 4));
- }
- elseif (starts_with($method, 'set_'))
- {
- $this->set_attribute(substr($method, 4), $parameters[0]);
- }
-
-
-
- else
- {
- return call_user_func_array(array($this->query(), $method), $parameters);
- }
- }
-
- public static function __callStatic($method, $parameters)
- {
- $model = get_called_class();
- return call_user_func_array(array(new $model, $method), $parameters);
- }
- }
|