tanora /tanora.org/www/framework/system/activerecord.php

Language PHP Lines 266
MD5 Hash 3594cad5bc1d8726a6a740ab8e157202 Estimated Cost $3,660 (why?)
Repository https://bitbucket.org/ekkl/tanora View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?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);
		}
	}
	
}

?>
Back to Top