All pastes #581525 Raw Edit

k4st

public php v1 · immutable
·published 2007-06-21 15:46 UTC
rendered paste body
<?php/** * $header * @copyright Copyright 2007, Peter Goodman * @package valkyrie * @subpackage Database.activerecord * @author Peter Goodman */if(!defined('IN_VALKYRIE')) {	exit;}/** * Class that holds information about individual database tables. This is part of * the active records system. This also has a generic function to validate fields */abstract class ModelDefinition {		/**	 * An array holding information about the table.	 * <code>	 * protected $table = array(	 *  				'name' => 'images', // the name of this database table	 *  				'temporary' => FALSE, // is this table a temporary one?	 *  				'check_exists' => TRUE, // can/do we perform checks to see if this table exists for creates/drops?	 *						 * 					// table joins, eg: this table joins its own column 'user_id' on to the column users.user_id	 *  				'joins' => array(	 *  								'user_id' => array(	 *  												'table' => 'users',	 *  												'column' => 'user_id',	 *  												),	 *  								),	 *  				'engine' => '', // the engine that this table uses, eg: myisam or innodb	 *  				'charset' => '', // the charset of the table, eg: utf8	 *  				'auto_increment' => 1, // where to start auto incrementing from	 *  				);	 * </code>	 */	protected $table = array();		/**	 * The column name of this tables primary key, if any.	 */	protected $primary;		/**	 * A multidimensional array of all of the column names to their respective properties.	 * Each column in the array should be formatted as follows:	 * <code>	 * 'column_name' => array(	 *  					'type' => '', // int, tinyint, bigint, integer, number, char, varchar, text, blob	 * 									  // decimal, float, etc.	 *  					'length' => 0, // eg: the '255' in varchar(255) or the '10' in int(10)	 *  					'signed' => FALSE, // whether or not this column is signed, FALSE = unsigned	 *  					'auto_increment' => TRUE, // does this column auto increment?	 * 						'null' => FALSE, // FALSE = not null	 * 						'default' => '', // the default value of this column	 *  					),	 * </code>	 * Note: only put the keys/values that apply to the column.	 */	protected $fields = array();		/**	 * A multidimensional array of this tables keys. Each key should be formatted as follows:	 * <code>	 * 	'column_name' => array(	 * 					'primary' => FALSE, // true/false for if this is a primary key	 * 	 * 					// if this is a foreign key, it must be formatted like	 *  				'foreign' => array( 	 *  						'references' => array( 	 *  											'table' => 'table_name', 	 *  											'column' => 'column_name', 	 *  											), 	 *  						'on_delete' => 'no action', 	 *  						'on_update' => 'no action', 	 *  						)	 *  				),	 * </code>	 */	protected $keys = array();		/**	 * An array of check constraints	 * eg: 'column_id <> 0'	 */	protected $checks = array();		/**	 * Generic function to validate values against the info set in the $fields	 * array.	 * @see ModelDefinition::$fields	 * @see Record::save()	 * @param array $keys A reference to an array of column names to check.	 * @param array $vals A reference to an array of column values to check/enforce proper type.	 */	final public function validate(&$keys, &$vals) {		foreach($keys as $i => $column) {			if(isset($this->fields[$column])) {								switch(strtolower($this->fields[$column]['type'])) {					case 'float':					case 'double':					case 'decimal':						$vals[$i] = (float)$vals[$i];						break;					case 'number':					case 'int':					case 'integer':					case 'bigint':					case 'tinyint':						$vals[$i] = (int)$vals[$i];						break;				}								if(isset($this->fields[$column]['signed']) && !$this->fields[$column]['signed']) {					$vals[$i] = abs($vals[$i]);				}							} else {				unset($keys[$i], $vals[$i]);			}		}	}}