Created
December 28, 2010 10:11
-
-
Save liuliu/757109 to your computer and use it in GitHub Desktop.
model.php
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 | |
require_once 'odm.php'; | |
require_once 'util.php'; | |
class Atomic | |
{ | |
private $identifier; | |
public function get($pipe) | |
{ | |
if (isset($pipe)) | |
return $pipe->get($this->identifier); | |
else { | |
global $odm; | |
return $odm->get($this->identifier); | |
} | |
} | |
public function set($val, $pipe) | |
{ | |
if (isset($pipe)) | |
return $pipe->set($this->identifier, $val); | |
else { | |
global $odm; | |
return $odm->set($this->identifier, $val); | |
} | |
} | |
public function incrby($val, $pipe) | |
{ | |
if (isset($pipe)) | |
return $pipe->incrby($this->identifier, $val); | |
else { | |
global $odm; | |
return $odm->incrby($this->identifier, $val); | |
} | |
} | |
public function decrby($val, $pipe) | |
{ | |
if (isset($pipe)) | |
return $pipe->decrby($this->identifier, $val); | |
else { | |
global $odm; | |
return $odm->decrby($this->identifier, $val); | |
} | |
} | |
public function __construct($identifier) | |
{ | |
$this->identifier = $identifier; | |
} | |
} | |
class Model | |
{ | |
public $rawData; | |
private $existence; | |
private $field; | |
private $className; | |
private $primary; | |
public function exists() | |
{ | |
return $this->existence; | |
} | |
public function toArray() | |
{ | |
$result = array(); | |
foreach ($this->field as $k => $v) | |
$result[$k] = $this->$k; | |
return $result; | |
} | |
public function fromArray($array) | |
{ | |
foreach ($this->field as $k => $v) | |
switch ($v[0]) | |
{ | |
case 'integer': | |
$this->$k = intval(edx($array, $k, $v[1])); | |
break; | |
case 'number': | |
$this->$k = floatval(edx($array, $k, $v[1])); | |
break; | |
case 'boolean': | |
$this->$k = edx($array, $k, $v[1]) ? true : false; | |
break; | |
case 'string': | |
$this->$k = strval(edx($array, $k, $v[1])); | |
break; | |
case 'array': | |
$this->$k = edx($array, $k, isset($v[1]) ? $v[1] : array()); | |
break; | |
case 'serializable': | |
$this->$k = edx($array, $k, new $v[1]()); | |
break; | |
case 'atomic': | |
// escape if atomic | |
break; | |
case 'primary': | |
// escape if primary key | |
break; | |
default: | |
$this->$k = edx($array, $k, $v[1]); | |
} | |
} | |
public static function all($class, $uuids) | |
{ | |
if (is_array($uuids) && count($uuids) > 0) | |
{ | |
global $odm; | |
$keys = array(); | |
foreach ($uuids as $uuid) | |
$keys[] = "{$class}->fetch({$uuid})"; | |
$values = $odm->mget($keys); | |
$all = array(); | |
foreach ($values as $value) | |
$all[] = new $class(unserialize($value)); | |
return $all; | |
} | |
} | |
public function fetch($uuid) | |
{ | |
global $odm; | |
$rawData = $odm->get("{$this->className}->fetch({$uuid})"); | |
if (isset($rawData)) | |
{ | |
$this->rawData = unserialize($rawData); | |
$this->existence = true; | |
} else { | |
$this->rawData = unserialize($odm->get("{$this->className}->fetch(0)")); | |
$this->existence = false; | |
} | |
$this->fromArray($this->rawData); | |
foreach ($this->field as $k => $v) | |
if ($v[0] == 'atomic') | |
$this->$k = new Atomic("{$this->className}->{$k}({$uuid})"); | |
if (isset($this->primary)) | |
{ | |
$primary = $this->primary; | |
$this->$primary = $uuid; | |
} | |
} | |
private function hint() | |
{ | |
$field = array(); | |
foreach ($this as $k => $v) | |
if (is_array($v)) | |
{ | |
$field[$k] = $v; | |
if ($v[0] == 'primary') | |
$this->primary = $k; | |
} | |
$this->field = $field; | |
} | |
public function __construct($uuid) | |
{ | |
$this->hint(); | |
$this->className = get_class($this); | |
if (is_array($uuid)) | |
$this->fromArray($uuid); | |
else | |
$this->fetch($uuid); | |
} | |
public function save() | |
{ | |
global $odm; | |
$primary = $this->primary; | |
echo "{$this->className}->fetch({$this->$primary})"; | |
if (isset($this->primary)) | |
{ | |
$primary = $this->primary; | |
$odm->set("{$this->className}->fetch({$this->$primary})", serialize($this->toArray())); | |
$this->existence = true; | |
} | |
} | |
} |
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 | |
require_once 'model.php'; | |
class Post extends Model | |
{ | |
public $uuid = array('primary'); | |
public $title = array('string', 'untitle'); | |
public $text = array('string'); | |
public function __construct($uuid) | |
{ | |
parent::__construct($uuid); | |
} | |
} |
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 | |
require_once 'post.php'; | |
$posts = Model::all(Post, array('post-1', 'post-2', 'post-3')); | |
var_dump($posts); | |
$posts[0]->title = 'new title'; | |
$posts[0]->save(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment