Created
November 5, 2012 21:46
-
-
Save jwdunne/4020549 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 | |
/** | |
* Soule Content Management Framework | |
* | |
* Open Source, Super Simple CMF | |
* | |
* @package Soule | |
* @version 1.2.0 | |
* @copyright 2011 - 2012 (c) devxdev.com | |
* @license http://soule.io/license | |
* @author Soule | |
* @link http://soule.io/ | |
* @since 1.0.0 | |
* @filesource | |
*/ | |
/** | |
* Soule Model Class | |
* | |
* Base model class to be extended by all models | |
* | |
* @package Soule | |
* @subpackage Libs | |
* @category Core Libs | |
* @author Soule | |
* @version 1.2.0 | |
* @namespace Soule\Application | |
*/ | |
namespace Soule\Application; | |
use Soule\Database\Modules\Query, | |
Soule\Application\Pagination; | |
class Model | |
{ | |
const FIND_BY = "find_by"; | |
public static $table; | |
protected static $cache = []; | |
private $id; | |
private $data = []; | |
public function __construct($id = null) | |
{ | |
if (!is_null($id)) | |
{ | |
$row = Query::table(static::$table)->where('id', '=', $id)->fetch(); | |
$this->id = $id; | |
$this->populate($row); | |
} | |
} | |
public static function make($data_obj) { | |
$class = get_called_class(); | |
$obj = new $class; | |
$obj->populate($data_obj); | |
return $obj; | |
} | |
public function __get($key) | |
{ | |
if (isset($this->data[$key])) | |
{ | |
return $this->data[$key]; | |
} | |
} | |
public function __set($key, $value) | |
{ | |
$this->data[$key] = $value; | |
} | |
public static function find($id) | |
{ | |
$class = get_called_class(); | |
$item = $id . '-' . $class; | |
if (isset(static::$cache[$item])) | |
{ | |
return static::$cache[$item]; | |
} | |
static::$cache[$item] = new $class($id); | |
return static::$cache[$item]; | |
} | |
public function populate($data) | |
{ | |
if (is_object($data)) { | |
$data = get_object_vars($data); | |
} | |
$this->data = $data; | |
} | |
public function save() | |
{ | |
if (is_null($this->id)) { | |
$this->id = static::create($this->data); | |
} else { | |
static::update($this->id, $this->data); | |
} | |
} | |
public function delete() | |
{ | |
$query = Query::table(static::$table)->where('id', '=', $this->id); | |
$this->id = null; | |
$this->data = array(); | |
return $query->delete(); | |
} | |
public static function where($column, $operator, $value) | |
{ | |
return Query::table(static::$table)->where($column, $operator, $value); | |
} | |
public static function create($data) | |
{ | |
return Query::table(static::$table)->insert_get_id($data); | |
} | |
public static function update($id, $data) | |
{ | |
return Query::table(static::$table)->where('id', '=', $id)->update($data); | |
} | |
public static function all() | |
{ | |
return Query::table(static::$table)->get(); | |
} | |
/* | |
There are probably errors, but hopefully the code explains what I'm | |
trying to achieve here. | |
Simply, if you want to find a user by username, you simply call: | |
$user = User::find_by_username('jwd'); | |
Or: | |
$moderators = User::find_by_group([Group::moderator, Group::super_user]); | |
*/ | |
public static function __callStatic($name, $args) | |
{ | |
$find_by_len = String::strlen(static::FIND_BY); | |
if (String::substr($name, 0, $find_by_len) == static::FIND_BY) | |
{ | |
$column = String::substr($name, $find_by_len); | |
return static::find_by($column, $args); | |
} | |
} | |
public static function find_by($name, $args) | |
{ | |
$rows = static::find_query($name, $args); | |
$objects = []; | |
foreach ($rows as $row) | |
{ | |
$objects[] = static::make($row); | |
} | |
if (count($row) == 1) | |
{ | |
return $objects[0]; | |
} | |
return $objects; | |
} | |
public static function find_query($name, $args) { | |
$query = Query::table(static::$table); | |
if (is_array($args)) | |
{ | |
$result = $query->where_in($name, $args); | |
} | |
else | |
{ | |
$result = $query->where($name, '=', $args); | |
} | |
return $result->fetch(); | |
} | |
/* | |
public static function paginate($page = 1, $perpage = 10) | |
{ | |
$query = Query::table(static::$table); | |
$count = $query->count(); | |
$results = $query->take($perpage)->skip(($page - 1) * $perpage)->get(); | |
return new agination($results, $count, $page, $perpage, Uri::current()); | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment