/tanora.org/www/framework/system/column.php
PHP | 107 lines | 56 code | 26 blank | 25 comment | 6 complexity | 3596105a0df4791c797e526e8daffbcd MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
- <?php
- /**
- * File: framework/system/column.php
- *
- * Defines a base class for ORM Fields when mapping to a database.
- */
- abstract class Column {
-
- protected $field;
- protected $db;
-
- protected $type;
-
- protected $constraints = array();
-
- /**
- * Sets the field member variable.
- */
- public function __construct(&$db, &$field) {
- $this->db = $db;
- $this->field = $field;
- }
-
- /**
- * Applies itself to a CreateQuery object.
- */
- public function create(&$query) {
-
- $column = $query->column();
- $column->name($this->field->get_name());
-
- $column->type($this->type);
-
- if(!empty($this->constraints)) {
- $handle = $column->constrain();
- foreach($this->constraints as $name=>$parameters) {
- // make sure method exists
- if(method_exists($handle, $name)) {
- call_user_func_array(array($handle, $name), $parameters);
- }
- }
- }
-
- }
-
- /**
- * Appends to the constraints member variable.
- */
- public function constrain($name) {
- $args = func_get_args();
- $this->constraints[array_shift($args)] = $args;
- }
-
- /**
- * Abstracts constrain() for convenience.
- */
- public function set_primary() {
- $this->constrain('primary');
- }
-
- /**
- * Gets the field value by default. Overwrite this to format values
- * specially for the database.
- */
- public function get_value() {
- return $this->field->get_value();
- }
-
- /**
- * Returns the Type object for this column from the respective database.
- */
- public function get_type() {
-
- $db_name = get_class($this->db);
- $db_name = substr($db_name, 0, -8);
- $dir_name = strtolower($db_name);
- $file_name = strtolower($this->type);
-
- $file = FRAMEWORK_PATH.'system/database/'.$dir_name.'/types.php';
-
- if(file_exists($file)) {
-
- require_once(FRAMEWORK_PATH.'system/types.php');
- require_once($file);
-
- $class = $db_name.'Types';
- $classes = new $class();
- $function = $this->type;
-
- if(function_exists(array($classes, $function))) {
- return $classes->$function();
- } else {
- Framework::error('Type does not exist: '.$this->type);
- }
-
- } else {
- Framework::error('File does not exist: '.$file);
- }
-
- }
-
- }
- ?>