Last active
August 29, 2015 14:01
-
-
Save mio991/69b347a5cf4e72aa4de8 to your computer and use it in GitHub Desktop.
A BussinesObject base class for mongodb with php and a sample inherit class.
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 'data/BussinesObject.php'; | |
class Auftrag extends BussinesObject | |
{ | |
public function Auftrag($var, $db) | |
{ | |
parent::BussinesObject($var, $db, constant("AUFTRAG_COLLECTION")); | |
} | |
public static function Find($selector, $db) | |
{ | |
$result = array(); | |
foreach (BussinesObject::Find($selector, $db, constant("AUFTRAG_COLLECTION")) as $value) { | |
$result[count($result)] = new Auftrag($value, $db); | |
} | |
return $result; | |
} | |
public static function CreateNewInstance($db) | |
{ | |
return new Auftrag(BussinesObject::CreateNewInstance($db, constant("AUFTRAG_COLLECTION"))); | |
} | |
} | |
?> |
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 | |
class BussinesObject implements arrayaccess | |
{ | |
var $collection; | |
var $db; | |
var $data; | |
var $changed = false; | |
protected function BussinesObject($var, $db, $collection) | |
{ | |
$this->db = $db; | |
$this->collection = $collection; | |
if(gettype($var) == 'string') | |
{ | |
$this->construct_id($var); | |
} | |
else | |
{ | |
$this->construct_data($var); | |
} | |
} | |
private function construct_id($id) | |
{ | |
$this->data = $this->db->selectCollection($this->collection)->findOne(array("_id" => $id)); | |
if(count($this->data) == 0) | |
{ | |
throw new Exception("BussinesObject not found.", 404); | |
} | |
} | |
private function construct_data($data) | |
{ | |
$this->data = $data; | |
} | |
protected static function Find($selector, $db, $collection) | |
{ | |
$result = array(); | |
$data = $db->selectCollection($collection)->find($selector); | |
foreach ($data as $value) { | |
$result[count($result)] = $value; | |
} | |
return $result; | |
} | |
protected static function CreateNewInstance($db, $collection) | |
{ | |
$data = $db->selectCollection($collection)->save(array('_id' => uniqid())); | |
return $data; | |
} | |
public function offsetSet($offset, $value) | |
{ | |
$this->data[$offset] = $value; | |
$changed = true; | |
} | |
public function offsetExists($offset) | |
{ | |
return array_key_exists($offset, $data); | |
} | |
public function offsetUnset($offset) | |
{ | |
unset($data[$offset]); | |
} | |
public function Save() | |
{ | |
$this->db->selectCollection($this->collection)->save($this->data); | |
} | |
public function offsetGet($offset) | |
{ | |
return $this->data[$offset]; | |
} | |
public function GetJSON() | |
{ | |
return json_encode($this->data); | |
} | |
public function Update($update) | |
{ | |
foreach (json_decode($update, true) as $key => $value) { | |
$this[$key] = $value; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment