Created
October 10, 2011 19:14
-
-
Save nerdsrescueme/1276239 to your computer and use it in GitHub Desktop.
Models
This file contains 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 | |
use Atom\Arr; | |
class Model { | |
protected static $_locked = true; // Is the database table locked from changes? | |
protected $_data = array(); | |
protected static $_meta = array(); | |
public function __construct() | |
{ | |
// Parse out $_meta... Cache too. | |
$this->_table = Arr::get(static::$_meta, '_table.0'); | |
foreach(static::$_meta as $column => $attrs) | |
{ | |
if(substr($column, 1) == '_') continue; // Skip _columns | |
$type = Arr::get($attrs, '0'); | |
list($type, $constraint) = explode('(', rtrim($type, ')'); | |
// Perform data pull | |
$attrs = explode(', ', $attrs); | |
foreach($attrs as $offset => $attr) | |
{ | |
list($key, $value) = explode('=', $attr); | |
$attrs[$key] = ($value === null) ? $value : true; | |
unset($attrs[$offset]); | |
} | |
} | |
} | |
public function __get($property) | |
{ | |
if(!isset(static::$_meta[$property])) | |
{ | |
throw new \InvalidArgumentException('Unknown model property ['.$property.']'); | |
} | |
return $this->_data[$property]; | |
} | |
public function __set($property, $value) | |
{ | |
if(!isset(static::$_meta[$property])) | |
{ | |
throw new \InvalidArgumentException('Unknown model property ['.$property.']'); | |
} | |
$this->_data[$property] = $value; | |
} | |
} | |
class Comment extends \Atom\Model { | |
protected static $_meta = array( | |
'_table' => array('comments', 'ENGINE=InnoDB, CHARSET=utf-8'), | |
'id' => array('INTEGER(11)', 'REQUIRED, AUTO_INCREMENT, PRIMARY, HAS_AND_BELONGS_TO_MANY=comments.id'), | |
'post_id' => array('INTEGER(11)', 'REQUIRED, BELONGS_TO=posts.id'), | |
'author_id' => array('INTEGER(11)', 'REQUIRED, BELONGS_TO=authors.id, EAGER_LOADING'), | |
'email' => array('STRING(255)', 'REQUIRED, UNIQUE, MIN_LENGTH=6'), | |
'body' => array('STRING', 'DB_KEY=content, MIN_LENGTH=1'), | |
'approved' => array('BOOLEAN', 'DEFAULT=false'), | |
'created' => array('DATE(timestamp)'), | |
'updated' => array('DATE(timestamp)'), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment