⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 model_php5.php.svn-base

📁 j2me is based on j2mepolish, client & server for mobile application. server part
💻 SVN-BASE
📖 第 1 页 / 共 3 页
字号:
		} else {			$data = array($this->name => array($one => $two));		}		foreach($data as $n => $v) {			if (is_array($v)) {				foreach($v as $x => $y) {					if ($n == $this->name) {						if (isset($this->validationErrors[$x])) {							unset ($this->validationErrors[$x]);						}						if ($x == $this->primaryKey) {							$this->id = $y;						}					}					$this->data[$n][$x] = $y;				}			}		}		return $data;	}/** * Returns an array of table metadata (column names and types) from the database. * * @return array Array of table metadata * @access public */	function loadInfo() {		$db =& ConnectionManager::getDataSource($this->useDbConfig);		if (!is_object($this->_tableInfo) && $db->isInterfaceSupported('describe')) {			uses('neat_array');			$this->_tableInfo = new NeatArray($db->describe($this));		}		return $this->_tableInfo;	}/** * Returns an associative array of field names and column types. * * @return array * @access public */	function getColumnTypes() {		$columns = $this->loadInfo();		$columns = $columns->value;		$db =& ConnectionManager::getDataSource($this->useDbConfig);		$cols = array();		foreach($columns as $col) {			$cols[$col['name']] = $col['type'];		}		return $cols;	}/** * Returns the column type of a column in the model * * @param string $column The name of the model column * @return string * @access public */	function getColumnType($column) {		$columns = $this->loadInfo();		$columns = $columns->value;		$cols = array();		foreach($columns as $col) {			if ($col['name'] == $column) {				return $col['type'];			}		}		return null;	}/** * Returns true if this Model has given field in its database table. * * @param string $name Name of field to look for * @return boolean * @access public */	function hasField($name) {		if (empty($this->_tableInfo)) {			$this->loadInfo();		}		if ($this->_tableInfo != null) {			return $this->_tableInfo->findIn('name', $name);		}		return null;	}/** * Initializes the model for writing a new record. * * @return boolean True * @access public */	function create() {		$this->id = false;		unset ($this->data);		$this->data = array();		return true;	}/** * @deprecated */	function setId($id) {		$this->id = $id;	}/** * Use query() instead. * @deprecated */	function findBySql($sql) {		return $this->query($sql);	}/** * Returns a list of fields from the database * * @param mixed $id The ID of the record to read * @param mixed $fields String of single fieldname, or an array of fieldnames. * @return array Array of database fields * @access public */	function read($fields = null, $id = null) {		$this->validationErrors = array();		if ($id != null) {			$this->id = $id;		}		$id = $this->id;		if (is_array($this->id)) {			$id = $this->id[0];		}		if ($this->id !== null && $this->id !== false) {			$db =& ConnectionManager::getDataSource($this->useDbConfig);			$field = $db->name($this->name) . '.' . $db->name($this->primaryKey);			return $this->find($field . ' = ' . $db->value($id, $this->getColumnType($this->primaryKey)), $fields);		} else {			return false;		}	}/** * Returns contents of a field in a query matching given conditions. * * @param string $name Name of field to get * @param array $conditions SQL conditions (defaults to NULL) * @param string $order SQL ORDER BY fragment * @return field contents * @access public */	function field($name, $conditions = null, $order = null) {		if ($conditions === null) {			$conditions = array($this->name . '.' . $this->primaryKey => $this->id);		}		if ($data = $this->find($conditions, $name, $order, 0)) {			if (strpos($name, '.') === false) {				if (isset($data[$this->name][$name])) {					return $data[$this->name][$name];				} else {					return false;				}			} else {				$name = explode('.', $name);				if (isset($data[$name[0]][$name[1]])) {					return $data[$name[0]][$name[1]];				} else {					return false;				}			}		} else {			return false;		}	}/** * Saves a single field to the database. * * @param string $name Name of the table field * @param mixed $value Value of the field * @param boolean $validate Whether or not this model should validate before saving (defaults to false) * @return boolean True on success save * @access public */	function saveField($name, $value, $validate = false) {		return $this->save(array($this->name => array($name => $value)), $validate);	}/** * Saves model data to the database. * By default, validation occurs before save. * * @param array $data Data to save. * @param boolean $validate If set, validation will be done before the save * @param array $fieldList List of fields to allow to be written * @return boolean success * @access public */	function save($data = null, $validate = true, $fieldList = array()) {		$db =& ConnectionManager::getDataSource($this->useDbConfig);		if ($data) {			if (countdim($data) == 1) {				$this->set(array($this->name => $data));			} else {				$this->set($data);			}		}		$whitelist = !(empty($fieldList) || count($fieldList) == 0);		if ($validate && !$this->validates()) {			return false;		}		if (!$this->beforeSave()) {			return false;		}		$fields = $values = array();		$count = 0;		if (count($this->data) > 1) {			$weHaveMulti = true;			$joined = false;		} else {			$weHaveMulti = false;		}		$newID = null;		foreach($this->data as $n => $v) {			if (isset($weHaveMulti) && isset($v[$n]) && $count > 0 && count($this->hasAndBelongsToMany) > 0) {				$joined[] = $v;			} else {				if ($n === $this->name) {					foreach (array('created', 'updated', 'modified') as $field) {						if (array_key_exists($field, $v) && (empty($v[$field]) || $v[$field] === null)) {							unset($v[$field]);						}					}					foreach($v as $x => $y) {						if ($this->hasField($x) && ($whitelist && in_array($x, $fieldList) || !$whitelist)) {							$fields[] = $x;							$values[] = $y;							if ($x == $this->primaryKey && !empty($y)) {								$newID = $y;							}						}					}				}			}			$count++;		}		$exists = $this->exists();		if (!$exists && $this->hasField('created') && !in_array('created', $fields) && ($whitelist && in_array('created', $fieldList) || !$whitelist)) {			$fields[] = 'created';			$values[] = date('Y-m-d H:i:s');		}		if ($this->hasField('modified') && !in_array('modified', $fields) && ($whitelist && in_array('modified', $fieldList) || !$whitelist)) {			$fields[] = 'modified';			$values[] = date('Y-m-d H:i:s');		}		if ($this->hasField('updated') && !in_array('updated', $fields) && ($whitelist && in_array('updated', $fieldList) || !$whitelist)) {			$fields[] = 'updated';			$values[] = date('Y-m-d H:i:s');		}		if (!$exists) {			$this->id = false;		}		if (count($fields)) {			if (!empty($this->id)) {				if ($db->update($this, $fields, $values)) {					if (!empty($joined)) {						$this->__saveMulti($joined, $this->id);					}					$this->afterSave();					$this->data = false;					$this->_clearCache();					return true;				} else {					return false;				}			} else {				if ($db->create($this, $fields, $values)) {					$this->__insertID = $db->lastInsertId($this->tablePrefix . $this->table, $this->primaryKey);					if (!$this->__insertID && $newID != null) {						$this->__insertID = $newID;						$this->id = $newID;					} else {						$this->id = $this->__insertID;					}					if (!empty($joined)) {						$this->__saveMulti($joined, $this->id);					}					$this->afterSave();					$this->data = false;					$this->_clearCache();					$this->validationErrors = array();					return true;				} else {					return false;				}			}		} else {			return false;		}	}/** * Saves model hasAndBelongsToMany data to the database. * * @param array $joined Data to save. * @param string $id * @return void * @access private */	function __saveMulti($joined, $id) {		$db =& ConnectionManager::getDataSource($this->useDbConfig);		foreach($joined as $x => $y) {			foreach($y as $assoc => $value) {				$joinTable[$assoc] = $this->hasAndBelongsToMany[$assoc]['joinTable'];				$mainKey[$assoc] = $this->hasAndBelongsToMany[$assoc]['foreignKey'];				$keys[] = $this->hasAndBelongsToMany[$assoc]['foreignKey'];				$keys[] = $this->hasAndBelongsToMany[$assoc]['associationForeignKey'];				$fields[$assoc]  = join(',', $keys);				unset($keys);				foreach($value as $update) {					if (!empty($update)) {						$values[]  = $db->value($id, $this->getColumnType($this->primaryKey));						$values[]  = $db->value($update);						$values    = join(',', $values);						$newValues[] = "({$values})";						unset ($values);					}				}				if (!empty($newValues)) {					$newValue[$assoc] = $newValues;					unset($newValues);				} else {					$newValue[$assoc] = array();				}			}		}		$total = count($joinTable);		if(is_array($newValue)){			foreach ($newValue as $loopAssoc=>$val) {				$db =& ConnectionManager::getDataSource($this->useDbConfig);				$table = $db->name($db->fullTableName($joinTable[$loopAssoc]));				$db->execute("DELETE FROM {$table} WHERE {$mainKey[$loopAssoc]} = '{$id}'");				if (!empty($newValue[$loopAssoc])) {					$secondCount = count($newValue[$loopAssoc]);					for($x = 0; $x < $secondCount; $x++) {						$db->execute("INSERT INTO {$table} ({$fields[$loopAssoc]}) VALUES {$newValue[$loopAssoc][$x]}");					}				}			}		}	}/** * Synonym for del(). * * @param mixed $id * @see function del * @return boolean True on success * @access public */	function remove($id = null, $cascade = true) {		return $this->del($id, $cascade);	}/** * Removes record for given id. If no id is given, the current id is used. Returns true on success. * * @param mixed $id Id of record to delete * @return boolean True on success * @access public */	function del($id = null, $cascade = true) {		if ($id) {			$this->id = $id;		}		$id = $this->id;		if ($this->exists() && $this->beforeDelete()) {			$db =& ConnectionManager::getDataSource($this->useDbConfig);			if ($this->id && $db->delete($this)) {				$this->_deleteMulti($id);				$this->_deleteHasMany($id, $cascade);				$this->_deleteHasOne($id, $cascade);				$this->afterDelete();				$this->_clearCache();				$this->id = false;				return true;			}		}		return false;	}/** * Alias for del() * * @param mixed $id Id of record to delete * @return boolean True on success * @access public */	function delete($id = null, $cascade = true) {		return $this->del($id, $cascade);	}/** * Cascades model deletes to hasMany relationships. * * @param string $id * @return null * @access protected */	function _deleteHasMany($id, $cascade) {		if (isset($this->__backAssociation)) {			$savedAssociatons = $this->__backAssociation;			unset ($this->__backAssociation);		}		foreach($this->hasMany as $assoc => $data) {			if ($data['dependent'] === true && $cascade === true) {				$model =& $this->{$data['className']};				$field = $model->escapeField($data['foreignKey']);				$model->recursive = 0;				$records = $model->findAll("$field = '$id'", $model->primaryKey, null, null);				if($records != false){					foreach($records as $record) {						$model->del($record[$data['className']][$model->primaryKey]);					}				}			}		}		if (isset($savedAssociatons)) {			$this->__backAssociation = $savedAssociatons;		}	}/** * Cascades model deletes to hasOne relationships. * * @param string $id * @return null * @access protected */	function _deleteHasOne($id, $cascade) {		if (isset($this->__backAssociation)) {			$savedAssociatons = $this->__backAssociation;			unset ($this->__backAssociation);		}		foreach($this->hasOne as $assoc => $data) {			if ($data['dependent'] === true && $cascade === true) {				$model =& $this->{$data['className']};				$field = $model->escapeField($data['foreignKey']);				$model->recursive = 0;				$records = $model->findAll("$field = '$id'", $model->primaryKey, null, null);				if($records != false){					foreach($records as $record) {						$model->del($record[$data['className']][$model->primaryKey]);					}				}			}		}		if (isset($savedAssociatons)) {			$this->__backAssociation = $savedAssociatons;		}	}/** * Cascades model deletes to HABTM join keys. * * @param string $id * @return null * @access protected */	function _deleteMulti($id) {		$db =& ConnectionManager::getDataSource($this->useDbConfig);		foreach($this->hasAndBelongsToMany as $assoc => $data) {			$db->execute("DELETE FROM " . $db->name($db->fullTableName($data['joinTable'])) . " WHERE " . $db->name($data['foreignKey']) . " = '{$id}'");		}	}/** * Returns true if a record with set id exists. * * @return boolean True if such a record exists * @access public */	function exists() {		if ($this->id) {			$id = $this->id;			if (is_array($id)) {				$id = $id[0];			}			$db =& ConnectionManager::getDataSource($this->useDbConfig);			return $db->hasAny($this, array($this->primaryKey => $id));		}		return false;	}/** * Returns true if a record that meets given conditions exists * * @param array $conditions SQL conditions array * @return boolean True if such a record exists * @access public */	function hasAny($conditions = null) {		return ($this->findCount($conditions) != false);	}/** * Return a single row as a resultset array. * By using the $recursive parameter, the call can access further "levels of association" than * the ones this model is directly associated to. * * @param array $conditions SQL conditions array * @param mixed $fields Either a single string of a field name, or an array of field names * @param string $order SQL ORDER BY conditions (e.g. "price DESC" or "name ASC") * @param int $recursive The number of levels deep to fetch associated records * @return array Array of records * @access public */	function find($conditions = null, $fields = null, $order = null, $recursive = null) {		$data = $this->findAll($conditions, $fields, $order, 1, null, $recursive);		if (empty($data[0])) {			return false;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -