Location: PHPKode > projects > Php simple acces data mysql > phpsimpledb_v2/manage/projets/generation/templates-files/extends.objects.class.php
<?php
	class PHPSIMPLEDB_extends_object{
		
		var $_oParentObject = NULL;
		
	    function __construct(){
	    }
	    
	    public function getData_field( $sName ){
			if( array_key_exists( $sName, $this->_oFields ) ){
				if( !is_object( $this->_oFields[ $sName ][ 'value' ] ) )
					return $this->_oPHPSIMPLEDB->bGetTo_utf8_encode ? utf8_encode( $this->_oFields[ $sName ][ 'value' ] ) : $this->_oFields[ $sName ][ 'value' ];
				return $this->_oPHPSIMPLEDB->bGetTo_utf8_encode ? utf8_encode( $this->_oFields[ $sName ][ 'value' ]->getData_key() ) : $this->_oFields[ $sName ][ 'value' ]->getData_key();
			}
	    	return NULL;
	    }
	    
	    public function setData_field( $sName, $sValue ){
	    	if( $this->_oPHPSIMPLEDB->bSetTo_utf8_decode )
	    		$sValue = utf8_decode( $sValue );
	    	if( array_key_exists( $sName, $this->_oFields ) ){
	    		$this->_oFields[ $sName ][ 'value' ] = $sValue;
	    		return true;
	    	}
	    	return false;
	    }
	    
	    public function getData_object( $sName ){
	    	if( array_key_exists( $sName, $this->_oFields ) ){
				if( $this->_oFields[ $sName ][ 'typefield' ] != 'object' )
					return NULL;
				if( !is_object( $this->_oFields[ $sName ][ 'value' ] ) ){
					$oObject = $this->_oPHPSIMPLEDB->getObject( $this->_oFields[ $sName ][ 'linkobject' ] );
					$oObject->load( $this->_oFields[ $sName ][ 'value' ] );
					$oObject->_oParentObject = $this;
					$this->_oFields[ $sName ][ 'value' ] = $oObject;
				}
				return $this->_oFields[ $sName ][ 'value' ];
	    	}
	    	return NULL;
	    }
	    
	    public function setData_object( $sName, $oValue ){
	    	if( !is_object( $oValue ) )
	    		return false;
	    	
	    	if( array_key_exists( $sName, $this->_oFields ) ){
	    		if( $oValue->_oInfos[ 'name' ] != $this->_oFields[ $sName ][ 'linkobject' ] )
					return false;
				$oValue->_oParentObject = $this;
				$this->_oFields[ $sName ][ 'value' ] = $oValue;
				return true;
	    	}
	    	return false;
	    }
	    
	    public function getData_collection( $sName ){
	    	$iThisId = $this->getData_key();
	    	if( is_null( $iThisId ) || $iThisId == '' )
	    		return NULL;
	   
	    	$bCollectionExist = false;
	    	for( $i = 0; $i < count( $this->_oCollections ); $i++ ){
	    		if( $this->_oCollections[ $i ] == $sName ){
	    			$bCollectionExist = true;
	    			break;
	    		}
	    	}
			
	    	if( !$bCollectionExist )
	    		return NULL;
	    	
	    	$oCollection = $this->_oPHPSIMPLEDB->getCollection( $sName );
	    	$oCollection->_oParentObject = $this;
	    	
			//recherche du champ de link
	    	$sFieldLink = '';
			foreach( $oCollection->_oFields as $sKey=>$oValues ){
				if( $oValues[ 'typefield' ] == 'object' && $this->_oInfos[ 'table' ] == $oValues[ 'linkobject' ] ){
					$sFieldLink = $sKey;
				}
			}
			if( $sFieldLink == '' ){
				return NULL;
			}

			$oCollection->load( $sFieldLink.' = \''.$iThisId.'\'' );
			
			return $oCollection;
	    }
	    
	    public function load( $oData = NULL ){
	    	if( is_null( $oData ) )
	    		return false;
	    	
	    	$oQuery = NULL;
	    	if( is_array( $oData ) ){
				$bKeyIsDefine = false;
				foreach( $oData as $sKey=>$oValue )
					if( array_key_exists( $sKey, $this->_oFields ) ){
						if( !$bKeyIsDefine && $sKey == $this->_oInfos[ 'primarykey'] )
							$bKeyIsDefine = true;
						$this->_oFields[ $sKey ][ 'value' ] = $oValue;
					}

				if( $bKeyIsDefine )
					return true;
				
				//chargement de la premiere occurence trouvee dans la bdd
				$sQuery = 'SELECT * FROM '.$this->_oInfos[ 'table'];
				$sWhere = '';
				$oAddValues = array();
				foreach( $oData as $sKey=>$oValue )
					if( array_key_exists( $sKey, $this->_oFields ) ){
						$sWhere .= $sKey.' = :'.$sKey.' AND ';
						$oAddValues[ $sKey ] = $oValue;
					}
				if( $sWhere != '' )
					$sQuery .= ' WHERE '.rtrim( $sWhere, 'AND ' );

				$oQuery = $this->_oPHPSIMPLEDB->_oPDO->prepare( $sQuery );
				
				$oQuery->execute( $oAddValues );
	    	}else{
	    		$sQuery = 'SELECT * FROM '.$this->_oInfos[ 'table'].' WHERE '.$this->_oInfos[ 'primarykey'].' = :data';
	    		$oQuery = $this->_oPHPSIMPLEDB->_oPDO->prepare( $sQuery );
				$oQuery->execute( array( 'data' => $oData ) );
	    	}
			$oResult = $oQuery->fetch( PDO::FETCH_ASSOC );
			if( !$oResult )
				return false;
			
			foreach( $this->_oFields as $sKey=>$oValue )
				if( isset( $oResult[ $sKey ] ) )
					$this->_oFields[ $sKey ][ 'value' ] = $oResult[ $sKey ];
			return true;
	    }
	    
	    public function save( $oData = NULL, $bForceCreate = false ){
	    	if( is_array( $oData ) ){
	    		foreach( $oData as $sNameField => $oValue )
	    			if( array_key_exists( $sNameField, $this->_oFields ) )
	    				if( !is_object( $oValue ) || ( is_object( $oValue ) && $oValue->_oInfos[ 'name' ] == $this->_oFields[ $sNameField ][ 'linkobject' ] ) )
	    						$this->_oFields[ $sNameField ][ 'value' ] = $oValue;
	    	}
	    	foreach( $this->_oFields as $sKey => $oValue ){
	    		//applanir les objets
	    		if( is_object( $this->_oFields[ $sKey ][ 'value' ] ) ){
	    			$iIdObject = $this->_oFields[ $sKey ][ 'value' ]->getData_key();
	    			if( is_null( $iIdObject ) || $iIdObject == '' )
	    				$this->_oFields[ $sKey ][ 'value' ]->save();
	    			$this->_oFields[ $sKey ][ 'value' ] = $this->_oFields[ $sKey ][ 'value' ]->getData_key();
	    		}
	    	}
	    	$iThisId = $this->getData_key();
	    	if( is_null( $iThisId ) || $iThisId == '' || $bForceCreate ){
	    		//creation
	    		
	    		$sQuery = 'INSERT INTO '.$this->_oInfos[ 'table'].' ( ';
	    		foreach( $this->_oFields as $sKey => $oValue ){
	    			if( ( is_numeric( $iThisId ) && $sKey != $this->_oInfos[ 'primarykey' ] ) || !is_numeric( $iThisId ) )
	    				$sQuery .= $sKey.', ';
	    		}
	    		$sQuery = rtrim( $sQuery, ', ' );
	    		$sQuery .= ' ) VALUES ( ';
	    		foreach( $this->_oFields as $sKey => $oValue ){
	    			if( ( is_numeric( $iThisId ) && $sKey != $this->_oInfos[ 'primarykey' ] ) || !is_numeric( $iThisId ) ){
	    				if( is_null( $this->_oFields[ $sKey ][ 'value' ] ) )
	    					$sQuery .= 'NULL, ';
	    				else
	    					$sQuery .= ':'.$sKey.', ';
	    			}
	    		}
	    		$sQuery = rtrim( $sQuery, ', ' );
	    		$sQuery .= ' )';
	    		
	    		$oQuery = $this->_oPHPSIMPLEDB->_oPDO->prepare( $sQuery );
	    		foreach( $this->_oFields as $sKey => $oValue ){
	    			if( ( is_numeric( $iThisId ) && $sKey != $this->_oInfos[ 'primarykey' ] ) || !is_numeric( $iThisId ) )
						if( !is_null( $this->_oFields[ $sKey ][ 'value' ] ) ){
							if( !is_null( $this->_oFields[ $sKey ][ 'size' ] ) &&
				    				$this->_oFields[ $sKey ][ 'type' ] == 'varchar' &&
				    				strlen( $this->_oFields[ $sKey ][ 'value' ] ) > $this->_oFields[ $sKey ][ 'size' ] )
			    				$this->_oFields[ $sKey ][ 'value' ] = substr( $this->_oFields[ $sKey ][ 'value' ], 0, $this->_oFields[ $sKey ][ 'size' ] );
							$oQuery->bindParam( ':'.$sKey, $this->_oFields[ $sKey ][ 'value' ] );
						}
	    		}
	    		$oQuery->execute();
	    		
	    		//sauvegarde de l'id
	    		$this->_oFields[ $this->_oInfos[ 'primarykey' ] ][ 'value' ] = $this->_oPHPSIMPLEDB->_oPDO->lastInsertId();
	    	}else if( !is_null( $iThisId ) && $iThisId != '' ){
	    		//modfication
	    		
	    		$sQuery = 'UPDATE '.$this->_oInfos[ 'table'].' SET ';
	    		foreach( $this->_oFields as $sKey => $oValue ){
	    			if( $sKey != $this->_oInfos[ 'primarykey' ] ){
	    				if( is_null( $this->_oFields[ $sKey ][ 'value' ] ) )
	    					$sQuery .= $sKey.' = NULL, ';
	    				else	
	    					$sQuery .= $sKey.' = :'.$sKey.', ';
	    			}
	    		}
	    		$sQuery = rtrim( $sQuery, ', ' );
	    		$sQuery .= ' WHERE '.$this->_oInfos[ 'primarykey' ].' = :'.$this->_oInfos[ 'primarykey' ];
	    		
	    		$oQuery = $this->_oPHPSIMPLEDB->_oPDO->prepare( $sQuery );
	    		foreach( $this->_oFields as $sKey => $oValue ){
	    			if( $sKey != $this->_oInfos[ 'primarykey' ] )
						if( !is_null( $this->_oFields[ $sKey ][ 'value' ] ) )
							$oQuery->bindParam( ':'.$sKey, $this->_oFields[ $sKey ][ 'value' ] );
	    		}

				$oQuery->bindParam( ':'.$this->_oInfos[ 'primarykey' ], $iThisId );
	    		$oQuery->execute();
	    	}
	    	
	    	return true;
	    }
	    
	    //attention le parametre $bDeleteCollections provoque une suppression recursive
	    public function delete( $bDeleteCollections = false ){
	    	$iThisId = $this->getData_key();
	    	if( is_null( $iThisId ) || $iThisId == '' )
	    		return false;
	    	
	    	if( $bDeleteCollections )
	    		for( $i = 0; $i < count( $this->_oCollections ); $i++ ){
	    			$oCollection = $this->getData_collection( $this->_oCollections[ $i ] );
	    			$oCollection->delete( $bDeleteCollections );
	    		}
	    	
	    	$sQuery = 'DELETE FROM '.$this->_oInfos[ 'table'].' WHERE '.$this->_oInfos[ 'primarykey' ].' = \''.$iThisId.'\'';
	    	$oQuery = $this->_oPHPSIMPLEDB->_oPDO->prepare( $sQuery );
	    	$oQuery->execute();
	    	
	    	$this->_oFields[ $this->_oInfos[ 'primarykey' ] ][ 'value' ] = '';
	    	
	    	return true;
	    }
	    
	    public function getData_infos(){
	    	return $this->_oInfos;
	    }
	    
	    public function getData_values(){
	    	$oValues = array();
			foreach( $this->_oFields as $sKey => $oValue ){
				if( is_object( $this->_oFields[ $sKey ][ 'value' ] ) )
	    			$oValues[ $sKey ] = $this->_oFields[ $sKey ][ 'value' ]->getData_key();
	    		else
	    			$oValues[ $sKey ] = $this->_oFields[ $sKey ][ 'value' ];
			}
	    	return $oValues;
	    }
	    
	    public function getData_key(){
	    	if( $this->_oInfos[ 'primarykey' ] != '' && !is_null( $this->_oInfos[ 'primarykey' ] ) )
	    		return $this->_oFields[ $this->_oInfos[ 'primarykey' ] ][ 'value' ];
	    	return NULL;
	    }
	}
?>
Return current item: Php simple acces data mysql