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

Language PHP Lines 107
MD5 Hash 3596105a0df4791c797e526e8daffbcd Estimated Cost $1,261 (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
<?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);
		}
	
	}
	
}


?>
Back to Top