[TYPO3-ect] tx_lib_modelBase

Steve Ryan stever at syntithenai.com
Sun Feb 11 23:48:40 CET 2007


Is this in theme? Minimal and simple implementation of Propel ideas. 
Working on relationships now.

<?php
require_once(t3lib_extMgm::extPath('div') . 'class.tx_div.php');
require_once(t3lib_extMgm::extPath('lib') . 'class.tx_lib_object.php');

class tx_lib_modelBase extends tx_lib_object {
	var $table;
	var $fieldList='*';
	var $primaryKey='uid';
	var $orderBy='';
	var $maxRows='';
	var $groupBy='';
	
	function dump() {
		debug(array($this->table,$this->fieldList,$this->getArrayCopy()));	
	}
	
	function setTable($value) {
		$this->table=$value;	
	}
	
	function getTable() {
		return $this->table;	
	}
	
	function setFieldList($value) {
		$this->fieldList=$value;	
	}
	
	function getFieldList() {
		return $this->fieldList;	
	}
	
	function setPrimaryKey($value) {
		$this->primaryKey=$value;	
	}
	
	function getPrimaryKey() {
		return $this->primaryKey;	
	}
	
	function setOrderBy($value) {
		$this->orderBy=$value;	
	}
	
	function getOrderBy() {
		return $this->orderBy;	
	}
	
	function setMaxRows($value) {
		$this->maxRows=$value;	
	}
	
	function getMaxRows() {
		return $this->maxRows;	
	}
	
	function setGroupBy($value) {
		$this->groupBy=$value;	
	}
	
	function getGroupBy() {
		return $this->groupBy;	
	}
	
	function save($data=array()) {
		//$this->_preSave($data);
		if (count($data)>0)  {
			$this->exchangeArray($data);	
		}
		if ($this->get[$this->primaryKey]>0) {
			$this->_update();	
		} else {
			$this->_insert();
		}
		//$this->_postSave();
	}
	
	
	function load($uid=0) {
		//$this->_preLoad($uid);
		if ($uid==0) $uid=$this->get($this->primaryKey);
		if ($uid>0) { 	
			$this->clear();
		 
$res=$GLOBALS['TYPO3_DB']->exec_SELECTquery($this->fieldList,$this->table,$this->primaryKey.'='.$uid,$this->getGroupBy(),$this->getOrderBy(),1);
			$row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
			$this->setArray($row);
		}
		///$this->_postLoad();
		return $this;
	}
	function delete($uid=0) {
		//$this->_preDelete($uid);
		if ($uid==0) $uid=$this->get($this->primaryKey);
		if ($uid>0) { 	
			$res = 
$GLOBALS['TYPO3_DB']->exec_DELETEquery($this->table,$this->primaryKey.'='.$uid);
	
		}
		//$this->_postDelete();
	}
	
	function findBy($fieldName,$values) {
		$criteria['search'][$fieldName]=$values;
		$configuration['search.'][$fieldName.'.']['fields']=$fieldName;
		return $this->search($criteria,$configuration);	
	}
	
	function search($criteria=array(),$configuration=array()) {
		//$this->_preSearch($criteria,$configuration);
		$where=$this->_createWhereClause($criteria,$configuration);
		$rows=array();
	 
$res=$GLOBALS['TYPO3_DB']->exec_SELECTquery($this->fieldList,$this->table,$where,$this->getGroupBy(),$this->getOrderBy(),$this->getMaxRows());
		$className=$this->getClassName();
		while ($row=$GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
			$tmp=new $className($row);
			$tmp->setTable($this->table);
			$tmp->setFieldList($this->fieldList);
			$tmp->setPrimaryKey($this->primaryKey);
			$tmp->setController($this->controller);
			$rows[]=$tmp;	
		}
		//$this->_postSearch();
		return new tx_lib_object($rows);
	}
	
	/**********************************************
	 * Generate a sql fragment for the where clause
	 * TS Configuration for the search criteria
	 * eg
	 * search.namesearch.fields=first_name,last_name
	 * search.titlesearch.fields=title
	 * criteria directly from posted array
	 * eg form field name
	 * tx_mycontroller_main[search][namesearch]=comma/space seperated 
search values (treated as logical AND)
	 * or
	 * tx_mycontroller_main[search][namesearch][]=array
	 * tx_mycontroller_main[search][namesearch][]=seperated
	 * tx_mycontroller_main[search][namesearch][]=search
	 * tx_mycontroller_main[search][namesearch][]=values (treated as 
logical OR)
	 * each configuration key is treated as a logical AND where there are 
criteria parameters
	 * comma seperated fields are treated as logical OR
	 * In summary
	 * Each search configuration section introduces another potential 
filter criteria. It is activated if
	 * there are criteria parameters.
	 * The criteria parameters are matched based on the search 
configuration key eg POST parameters
	 * tx_mycontroller_main[search][namesearch] enables the extra filter 
for the configuration key
	 * tx_mycontroller_main.search.namesearch.
	 * Where multiple comma seperated fields fields are specified by the 
fields value eg tx_mycontroller_main.search.namesearch.fields=aaa,bbb a 
match in either field for this criteria will pass.
	 * Where the incoming criteria parameters are comma or space seperated, 
a match of all the tokens is required..
	 * Where the incoming criteria parameters are in arrays, any one of the 
values will match.
	
	 **********************************************/
	function _createWhereClause($criteria,$configuration) {
		// if no parameters/config, try to extract from controller
		if (is_object($this->controller)) {
			if (count($criteria)==0) {
				$criteria=$this->controller->getParameter('search');
			}
			if (count($configuration)==0) {
				$configuration=$this->controller->getConfiguration('search.');
			}
		}	
		// generate conditions
		$configurations=new tx_lib_object($configuration);
		$where='';
		// iterate search configuration sections
		$perConfigWhere=array();
			
		for ($configurations->rewind(); $configurations->valid(); 
$configurations->next()) {
			$configSection=$configurations->key();
		 
$configSectionNoDot=substr($configurations->key(),0,strlen($configurations->key())-1);
			$configDetails=$configurations->current();
			$fieldNames=$configDetails['fields'];
			$fieldNames=new tx_lib_object(tx_div::explode($fieldNames,','));
			// iterate possibly multiple fields for a given search configuration
			$perFieldWhere=array();
			for ($fieldNames->rewind(); $fieldNames->valid(); $fieldNames->next()) {
				$values=new tx_lib_object();
				$fieldName=$fieldNames->current();
				// array criteria
				if (is_array($criteria[$configSectionNoDot])) {
					foreach ($criteria[$configSectionNoDot] as $k => $v) {
						// explode comma/space seperated criteria into distinct child array
						$commaValues=tx_div::explode($v,', ;:');
						if (count($commaValues)>0) $values->append($commaValues);
					}
				} else {
					// explode comma seperated criteria into distinct child array
					$commaValues=tx_div::explode($criteria[$configSectionNoDot],', ;:');
					if (count($commaValues)>0) $values->append($commaValues);
				}
				if ($values->isNotEmpty()) {
					$perValueWhere=array();
					for ($values->rewind(); $values->valid(); $values->next()) {
						if (is_array($values->current())) {
							$perCriteriaSetWhere=array();
							$perCriteriaValues=new tx_lib_object($values->current());
							for ($perCriteriaValues->rewind(); $perCriteriaValues->valid(); 
$perCriteriaValues->next()) {
								if (strlen(trim($perCriteriaValues->current()))>0)  {
									$perCriteriaSetWhere[]=$fieldName." like 
".$GLOBALS['TYPO3_DB']->fullQuoteStr('%'.$perCriteriaValues->current().'%',$this->table);
								}
							}
							$perValueWhere[]='('.implode(' and ',$perCriteriaSetWhere).')';
						}
					}
					if (count($perValueWhere)>0) $perFieldWhere[]=' ('.implode(' or 
',$perValueWhere).')';
				}
			}
			if (count($perFieldWhere)>0) $perConfigWhere[]=' ('.implode(' or 
',$perFieldWhere).')';
		}
		if (count($perConfigWhere)>0) $where=' ('.implode(' and 
',$perConfigWhere).')';
		return $where;
	}
	
	
	function _insert() {
		//$this->_preInsert();
		$dataToSave=array();
		// limit data to fieldList
		if (trim($this->fieldList)=='*') {
			for ($this->rewind(); $this->valid(); $this->next()) {
				$dataToSave[$this->key()]=$this->current();		
			}		
		} else {
			$fields=explode(',',$this->fieldList);
			for ($this->rewind(); $this->valid(); $this->next()) {
				if (in_array($this->key(),$fields)) {
					$dataToSave[$this->key()]=$this->current();		
				}
			}
		}
		$query = $GLOBALS['TYPO3_DB']->INSERTquery($this->table, $dataToSave);
		$res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);
		$this->set($this->primaryKey,$GLOBALS['TYPO3_DB']->sql_insert_id());
		//$this->_postInsert();
	}
	function _update() {
		//$this->_preUpdate();
		$dataToSave=array();
		// limit data to fieldList
		if (trim($this->fieldList)=='*') {
			for ($this->rewind(); $this->valid(); $this->next()) {
				$dataToSave[$this->key()]=$this->current();		
			}		
		} else {
			$fields=tx_lib::explode($this->fieldList,',');
			for ($this->rewind(); $this->valid(); $this->next()) {
				if (in_array($this->key(),$fields)) {
					$dataToSave[$this->key()]=$this->current();		
				}
			}
		}
		$query = 
$GLOBALS['TYPO3_DB']->UPDATEquery($this->table,'uid='.$this->get['uid'], 
$dataToSave);
		$res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);
		//$this->_postUpdate();
	}
	
	
	/*
	// hidden function hooks are unnecessary
	// pre/post hooks can be done by overriding method and calling 
parent::save($data);
	
	
	function _preLoad($uid) {
		
	}
	
	function _postLoad() {
		
	}
	
	function _preSave($data) {
		
	}
	
	function _postSave() {
		
	}
	
	function _preInsert() {
		
	}
	
	function _postInsert() {
		
	}
	
	function _preUpdate() {
		
	}
	
	function _postUpdate() {
		
	}
	
	function _preDelete($uid) {
		
	}
	
	function _postDelete() {
		
	}
	
	function _preSearch($criteria,$configuration) {
		
	}
	
	function _postSearch() {
		
	}
	*/
}


?>


More information about the TYPO3-team-extension-coordination mailing list