Last active
December 16, 2015 16:56
-
-
Save jonahgeorge/fb00cc748ecdf16c95b8 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 | |
| // Fly you fools! | |
| class ActiveRecord { | |
| use ActiveRecordAll; | |
| use ActiveRecordFind; | |
| use ActiveRecordFindBy; | |
| use ActiveRecordInsert; | |
| use ActiveRecordUpdate; | |
| // use ActiveRecordWhere; | |
| use ActiveRecordDestroy; | |
| private static $columns; | |
| function updateAttributes($whitelist, $form) { | |
| foreach ($whitelist as $p) { | |
| $this->{$p} = $form->{$p}; | |
| } | |
| } | |
| static function columns() { | |
| global $db; | |
| if (!isset(self::$columns)) { | |
| $query = $db->prepare( | |
| sprintf("SHOW COLUMNS FROM %s", static::TABLE_NAME)); | |
| $query->execute(); | |
| self::$columns = array_map(function ($row) { | |
| return $row["Field"]; | |
| }, $query->fetchAll()); | |
| } | |
| return self::$columns; | |
| } | |
| function save() { | |
| // Yes, this is slow, but it's better than not being entirely sure. | |
| if (!self::find($this->{static::PRIMARY_KEY})) { | |
| $this->insert(); | |
| } else { | |
| $this->update(); | |
| } | |
| } | |
| } | |
| trait ActiveRecordAll { | |
| static function all_sql() { | |
| return sprintf("SELECT * FROM %s", static::TABLE_NAME); | |
| } | |
| function all_params() { | |
| return null; | |
| } | |
| static function all() { | |
| global $db; | |
| $sql = self::all_sql(); | |
| $query = $db->prepare($sql); | |
| $query->setFetchMode(PDO::FETCH_CLASS, get_called_class()); | |
| $query->execute(); | |
| return $query->fetchAll(); | |
| } | |
| } | |
| trait ActiveRecordFind { | |
| static function find_sql() { | |
| return sprintf("SELECT * FROM %s WHERE %s = %s LIMIT 1", | |
| static::TABLE_NAME, static::PRIMARY_KEY, ":".static::PRIMARY_KEY); | |
| } | |
| static function find_params($id) { | |
| return [ ':'.static::PRIMARY_KEY => $id ]; | |
| } | |
| static function find($id) { | |
| global $db; | |
| $sql = self::find_sql(); | |
| $query = $db->prepare($sql); | |
| $query->setFetchMode(PDO::FETCH_CLASS, get_called_class()); | |
| $query->execute(self::find_params($id)); | |
| return $query->fetch(); | |
| } | |
| } | |
| trait ActiveRecordFindBy { | |
| static function find_by_sql($column) { | |
| return sprintf("SELECT * FROM %s WHERE %s = %s LIMIT 1", | |
| static::TABLE_NAME, $column, ":".$column); | |
| } | |
| static function find_by_params($column, $value) { | |
| return [ ':'.$column => $value ]; | |
| } | |
| static function find_by($column, $value) { | |
| global $db; | |
| $sql = self::find_by_sql($column, $value); | |
| $query = $db->prepare($sql); | |
| $query->setFetchMode(PDO::FETCH_CLASS, get_called_class()); | |
| $query->execute(self::find_by_params($column, $value)); | |
| return $query->fetch(); | |
| } | |
| } | |
| trait ActiveRecordUpdate { | |
| static function update_sql() { | |
| // Remove Primary Key from columns | |
| $updateColumns = self::columns(); | |
| unset($updateColumns[static::PRIMARY_KEY]); | |
| $mapping = join(",", array_map(function ($row) { | |
| return "$row = :$row"; | |
| }, $updateColumns)); | |
| return sprintf("UPDATE %s SET %s WHERE %s = %s", | |
| static::TABLE_NAME, $mapping, static::PRIMARY_KEY, | |
| ":".static::PRIMARY_KEY); | |
| } | |
| function update_params() { | |
| $params = []; | |
| foreach (self::columns() as $column) { | |
| $params[":$column"] = $this->{$column}; | |
| } | |
| return $params; | |
| } | |
| function update() { | |
| global $db; | |
| $query = $db->prepare(self::update_sql()); | |
| $query->execute($this->update_params()); | |
| } | |
| } | |
| trait ActiveRecordInsert { | |
| static function insert_sql() { | |
| $mapping = join(",", array_map(function ($row) { | |
| return "$row = :$row"; | |
| }, self::columns())); | |
| return sprintf("INSERT INTO %s SET %s", static::TABLE_NAME, $mapping); | |
| } | |
| function insert_params() { | |
| $params = []; | |
| foreach (self::columns() as $column) { | |
| $params[":$column"] = $this->{$column}; | |
| } | |
| return $params; | |
| } | |
| function insert() { | |
| global $db; | |
| $query = $db->prepare(self::insert_sql()); | |
| $query->execute($this->insert_params()); | |
| } | |
| } | |
| // trait ActiveRecordWhere { | |
| // static function where_sql($conditions) { | |
| // $sql = self::all_sql(); | |
| // } | |
| // | |
| // function where_params($conditions) { | |
| // | |
| // } | |
| // | |
| // function where($conditions) { | |
| // global $db; | |
| // $query = $db->prepare(self::where_sql($conditions)); | |
| // $query->execute($this->where_params($conditions)); | |
| // } | |
| // } | |
| trait ActiveRecordDestroy { | |
| static function destroy_sql() { | |
| return sprintf("DELETE FROM %s WHERE %s = :%s", | |
| static::TABLE_NAME, static::PRIMARY_KEY, static::PRIMARY_KEY); | |
| } | |
| function destroy_params() { | |
| return [ ":".static::PRIMARY_KEY => $this->{static::PRIMARY_KEY} ]; | |
| } | |
| function destroy() { | |
| global $db; | |
| $query = $db->prepare(self::destroy_sql()); | |
| $query->execute($this->destroy_params()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment