/tanora.org/www/framework/system/activerecord.php
PHP | 265 lines | 151 code | 30 blank | 84 comment | 16 complexity | 3594cad5bc1d8726a6a740ab8e157202 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
- <?php
- /**
- * This is the base class for all active record models.
- */
-
- require_once(FRAMEWORK_PATH.'system/model.php');
- abstract class ActiveRecord extends Model implements Iterator, Countable{
-
- protected $db;
- protected $fields;
- protected $table;
- protected $query;
-
- /**
- * Sets the ORM structure.
- */
- public function __construct($db = 'default') {
- $this->db = Framework::load_database($db);
- $this->__structure();
- if($this->db instanceof SqlDatabase) {
- $this->query = $this->db->query('select');
- $this->query->from()->table($this->get_table());
- } else {
- // NoSQL database
- }
- }
-
- /**
- * Must be defined in subclasses. Define the model structure here
- * using the field() method.
- */
- abstract protected function __structure();
-
- /**
- * Loads Field object into the fields member variable and returns
- * the reference.
- */
- protected function _field($name) {
- require_once(FRAMEWORK_PATH.'system/field.php');
- $dirs = array(FIELD_PATH, FRAMEWORK_PATH.'fields/');
- foreach($dirs as $dir) {
- $file = $dir.strtolower($name).'field.php';
- if(file_exists($file)) {
- require_once($file);
- $index = count($this->fields);
- $class = $name.'Field';
- $this->fields[$index] = new $class($this->db);
- return $this->fields[$index];
- }
- }
- Framework::error('Cannot load field: '.$name);
- }
-
- /**
- * Sets the table member variable.
- */
- protected function _table($name) {
- $this->table = $name;
- }
-
- /**
- * Gets the table member variable.
- */
- public function get_table() {
- return $this->table;
- }
-
- /**
- * Returns the list of fields as a reference.
- */
- public function get_fields() {
- return $this->fields;
- }
-
- /**
- * Returns a reference to a single field.
- */
- public function get_field($name) {
- foreach($this->get_fields() as $field) {
- if($field->get_name() == $name) {
- return $field;
- }
- }
- Framework::error('Field '.$name.' does not exist in '.get_class($this));
- }
-
- /**
- * Returns a Form object with the appropriate ORM reference.
- */
- public function form() {
- require_once(FRAMEWORK_PATH.'system/form.php');
- return new Form($this);
- }
-
- /**
- * Sets the query member variable and returns the reference.
- */
- public function query() {
- return $this->query;
- }
-
- /**
- * Loads the row at the specified index.
- */
- public function load($index) {
- $row = $this->query->get($index);
- $this->set($row);
- }
-
- /**
- * Counts the number of rows, from Countable interface.
- */
- public function count() {
- return count($this->query);
- }
-
- /**
- * Gets the current row.
- */
- public function current() {
- $row = $this->query->current();
- $this->set($row);
- return $this;
- }
-
- /**
- * Rewinds.
- */
- public function rewind() {
- $this->query->rewind();
- }
-
- /**
- * Returns the position.
- */
- public function key() {
- return $this->query->key();
- }
-
- /**
- * Moves to the next row.
- */
- public function next() {
- $this->query->next();
- }
-
- /**
- * Checks if row exists.
- */
- public function valid() {
- return $this->query->valid();
- }
-
- /**
- * Gets the query result.
- */
- public function fill() {
- $result = $this->query->result();
- // loads the first result automatically
- if(count($this->query) > 0) {
- $this->load(0);
- }
- }
-
- /**
- * Sets the Field values with values from an object.
- */
- public function set($obj) {
- foreach($this->get_fields() as $field) {
- $name = $field->get_name();
- if(isset($obj->$name)) {
- $field->set_value($obj->$name);
- }
- }
- }
-
- /**
- * Inserts the field values in the database (or overwrites existing
- * ones).
- */
- public function save() {
- if($this->db instanceof SqlDatabase) {
- $query = $this->db->query('replace');
- $query->table($this->get_table());
- foreach($this->get_fields() as $field) {
- $value = $field->get_column()->get_value();
- $query->set($field->get_name(), $value);
- }
- $query->run();
- } else {
- // NoSQL database
- }
- }
-
- /**
- * Deletes the value from the database.
- */
- public function delete() {
- if($this->db instanceof SqlDatabase) {
- $query = $this->db->query('delete');
- $pkey = $this->db->primary_key($this->get_table());
- $query->from($this->get_table());
- $query->where()->condition($pkey, $this->$pkey);
- $query->run();
- } else {
- // NoSQL database
- }
- }
-
- /**
- * Creates the database table.
- */
- public function sync() {
- if($this->db instanceof SqlDatabase) {
- // TODO: don't drop the table
- $query = $this->db->query('drop');
- $query->table($this->get_table());
- $query->if_exists();
- $query->run();
-
- $query = $this->db->query('create');
- $query->name($this->get_table());
- foreach($this->get_fields() as $field) {
- $column = $field->get_column();
- $column->create($query);
- }
- $query->run();
- } else {
- // NoSQL database
- }
- }
-
- /**
- * Returns the value of a field.
- */
- public function __get($field) {
- if(isset($this->$field)) {
- $field = $this->get_field($field);
- return $field->get_value();
- }
- }
-
- /**
- * Checks if a field exists.
- */
- public function __isset($field) {
- if(!is_null($this->get_field($field))) {
- return !is_null($this->get_field($field)->get_value());
- }
- return FALSE;
- }
-
- /**
- * Sets the value of a field.
- */
- public function __set($field, $value) {
- if(!is_null($this->get_field($field))) {
- $this->get_field($field)->set_value($value);
- }
- }
-
- }
- ?>