class DBObject in Entity Construction Kit (ECK) 7.2
Same name and namespace in other branches
- 7.3 eck.classes.inc \DBObject
@file Classes for all the different objects used in ECK.
Hierarchy
- class \DBObject implements \Iterator
Expanded class hierarchy of DBObject
File
- ./
eck.classes.inc, line 7 - Classes for all the different objects used in ECK.
View source
class DBObject implements Iterator {
// Wheteher this object was loaded or just created.
public $isNew;
// Iterator variable.
private $position;
// The database table where the objects exist.
private $table;
private $vars;
private $data;
private $primaryKeys;
private $serialize;
/**
* Constructor.
*/
protected function __construct($table) {
$this->serialize = array();
$this->isNew = TRUE;
// Iterator variable.
$this->position = 0;
// Is this a real table? If it is, check it.
if ($schema = drupal_get_schema($table)) {
$this->table = $table;
$this->primaryKeys = $schema["primary key"];
$this->vars = array_keys($schema['fields']);
// Do we want to handle searialized variables by default? let's do it
// and wait for some critizism.
foreach ($schema['fields'] as $name => $field) {
if (array_key_exists('serialize', $field) && $field['serialize']) {
$this->serialize[] = $name;
}
}
foreach ($this->vars as $var) {
if ($schema['fields'][$var]['type'] != "serial") {
$this->data[$var] = NULL;
}
}
}
else {
// @todo throw an exception.
}
}
/**
* Magic method.
*/
public function __set($var, $value) {
if (in_array($var, $this->vars)) {
$this->data[$var] = $value;
}
}
/**
* Magic method.
*/
public function __get($var) {
if (property_exists($this, $var)) {
return $this->{$var};
}
return $this->data[$var];
}
/**
* Magic method.
*/
public function __isset($name) {
return isset($this->data[$name]);
}
/**
* Magic method.
*/
public function __unset($name) {
unset($this->data[$name]);
}
/**
* Save.
*/
public function save() {
// Before we save, lets serialize the properties that require it.
foreach ($this->serialize as $property) {
$this->{$property} = drupal_json_encode($this->{$property});
}
if ($this->isNew) {
$this->id = db_insert($this->table)
->fields($this->data)
->execute();
}
else {
// Well I need to know what the primary id is to set up the condition.
$primary_key = $this->primaryKeys[0];
db_update($this->table)
->condition($primary_key, $this->{$primary_key}, '=')
->fields($this->data)
->execute();
}
// Now that we are done saving lets deserialize in case that for some
// reason we will continue manipulating the properties.
foreach ($this->serialize as $property) {
$this->{$property} = drupal_json_decode($this->{$property});
}
$this->isNew = FALSE;
}
/**
* Load.
*
* @param string $property
* The property we will use to search for the record.
* @param mixed $value
* The value the property should match.
*/
protected function load($property, $value) {
$result = db_select($this->table, 't')
->fields('t')
->condition($property, $value, '=')
->execute()
->fetchAssoc();
if ($result) {
foreach ($result as $property => $value) {
if (in_array($property, $this->serialize)) {
$value = drupal_json_decode($value);
}
$this->{$property} = $value;
}
// We should only set the isNew flag as false if we loaded something.
$this->isNew = FALSE;
}
}
/**
* Delete.
*/
public function delete() {
// We can only deleted if its a loaded object, or if it has been saved.
if (!$this->isNew) {
$query = db_delete($this->table);
$primary_key = $this->primaryKeys[0];
$query
->condition($primary_key, $this->{$primary_key}, '=');
$query
->execute();
// Should we delete the data from the object.. not sure.
// For right now lets just set it back to new.
$this->isNew = TRUE;
}
}
/**
* From Iterator Interface.
*/
public function rewind() {
$this->position = 0;
}
/**
* From Iterator Interface.
*/
public function current() {
return $this->data[$this
->key()];
}
/**
* From Iterator Interface.
*/
public function key() {
return $this->vars[$this->position];
}
/**
* From Iterator Interface.
*/
public function next() {
++$this->position;
}
/**
* From Iterator Interface.
*/
public function valid() {
if (in_array($this->position, array_keys($this->vars))) {
return TRUE;
}
else {
return FALSE;
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DBObject:: |
private | property | ||
DBObject:: |
public | property | ||
DBObject:: |
private | property | ||
DBObject:: |
private | property | ||
DBObject:: |
private | property | ||
DBObject:: |
private | property | ||
DBObject:: |
private | property | ||
DBObject:: |
public | function | From Iterator Interface. | |
DBObject:: |
public | function | Delete. | 2 |
DBObject:: |
public | function | From Iterator Interface. | |
DBObject:: |
protected | function | Load. | |
DBObject:: |
public | function | From Iterator Interface. | |
DBObject:: |
public | function | From Iterator Interface. | |
DBObject:: |
public | function | Save. | 2 |
DBObject:: |
public | function | From Iterator Interface. | |
DBObject:: |
protected | function | Constructor. | 2 |
DBObject:: |
public | function | Magic method. | |
DBObject:: |
public | function | Magic method. | |
DBObject:: |
public | function | Magic method. | |
DBObject:: |
public | function | Magic method. |