oxbase.php

Go to the documentation of this file.
00001 <?php
00002 
00006 DEFINE('ACTION_NA', 0);
00007 DEFINE('ACTION_DELETE', 1);
00008 DEFINE('ACTION_INSERT', 2);
00009 DEFINE('ACTION_UPDATE', 3);
00010 DEFINE('ACTION_UPDATE_STOCK', 4);
00011 
00016 class oxBase extends oxSuperCfg
00017 {
00022     protected $_sOXID = null;
00023 
00028     protected $_iShopId = null;
00029 
00035     protected $_blIsSimplyClonable = true;
00036 
00041     protected $_sClassName = 'oxbase';
00042 
00048     protected $_sCoreTable = null;
00049 
00054     protected $_sViewTable  = null;
00055 
00056 
00062     protected $_aFieldNames = array('oxid' => 0);
00063 
00069     protected $_sCacheKey = null;
00070 
00076     protected $_blUseLazyLoading = false;
00077 
00083     protected $_aSkipSaveFields = array('oxtimestamp');
00084 
00085 
00091     protected $_blUseSkipSaveFields = true;
00092 
00097     protected $_sExistKey = "oxid";
00098 
00105     protected $_blIsDerived = null;
00106 
00116     protected static $_blDisableFieldCaching = array();
00117 
00123     protected $_blIsSeoObject = false;
00124 
00130     protected $_blReadOnly = false;
00131 
00137     protected $_blIsInList = false;
00138 
00144     protected $_isLoaded = false;
00145 
00151     protected $_aInnerLazyCache = null;
00152 
00158     protected $_blEmployMultilanguage = false;
00159 
00165     public function getUseSkipSaveFields()
00166     {
00167         return $this->_blUseSkipSaveFields;
00168     }
00169 
00177     public function setUseSkipSaveFields( $blUseSkipSaveFields )
00178     {
00179         $this->_blUseSkipSaveFields = $blUseSkipSaveFields;
00180     }
00181 
00185     public function __construct()
00186     {
00187         // set active shop
00188         $myConfig = $this->getConfig();
00189         $this->_sCacheKey = $this->getViewName();
00190 
00191 
00192         if ( $this->_blUseLazyLoading ) {
00193             $this->_sCacheKey .= $myConfig->getActiveView()->getClassName();
00194         } else {
00195             $this->_sCacheKey .= "allviews";
00196         }
00197 
00198         //do not cache for admin?
00199         if ( $this->isAdmin() ) {
00200             $this->_sCacheKey = null;
00201         }
00202 
00203         $this->setShopId( $myConfig->getShopId() );
00204     }
00205 
00214     public function __set( $sName, $sValue )
00215     {
00216         $this->$sName = $sValue;
00217         if ( $this->_blUseLazyLoading && strpos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00218             $sFieldName = str_replace( $this->_sCoreTable . "__", '', $sName );
00219             if ( $sFieldName != 'oxnid' && ( !isset( $this->_aFieldNames[$sFieldName] ) || !$this->_aFieldNames[$sFieldName] ) ) {
00220                 $aAllFields = $this->_getAllFields(true);
00221                 if ( isset( $aAllFields[strtolower($sFieldName)] ) ) {
00222                     $iFieldStatus = $this->_getFieldStatus( $sFieldName );
00223                     $this->_addField( $sFieldName, $iFieldStatus );
00224                 }
00225             }
00226         }
00227     }
00228 
00236     public function __get( $sName )
00237     {
00238         switch ( $sName ) {
00239             case 'blIsDerived':
00240                 return $this->isDerived();
00241                 break;
00242             case 'sOXID':
00243                 return $this->getId();
00244                 break;
00245             case 'blReadOnly':
00246                 return $this->isReadOnly();
00247                 break;
00248         }
00249 
00250         // implementing lazy loading fields
00251         // This part of the code is slow and normally is called before field cache is built.
00252         // Make sure it is not called after first page is loaded and cache data is fully built.
00253         if ( $this->_blUseLazyLoading && stripos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00254 
00255             if ( $this->getId() ) {
00256 
00257                 //lazy load it
00258                 $sFieldName      = str_replace( $this->_sCoreTable . "__", '', $sName );
00259                 $sCacheFieldName = strtoupper( $sFieldName );
00260 
00261                 $iFieldStatus = $this->_getFieldStatus( $sFieldName );
00262                 $sViewName    = $this->getViewName();
00263                 $sId = $this->getId();
00264 
00265                 try {
00266                     if ( $this->_aInnerLazyCache === null ) {
00267 
00268                         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00269                         $sQ = "SELECT * FROM " . $sViewName . " WHERE `oxid` = " . $oDb->quote( $sId );
00270                         $rs = $oDb->select( $sQ );
00271                         if ( $rs && $rs->RecordCount() ) {
00272                             $this->_aInnerLazyCache = array_change_key_case( $rs->fields, CASE_UPPER );
00273                             if ( array_key_exists( $sCacheFieldName, $this->_aInnerLazyCache ) ) {
00274                                 $sFieldValue = $this->_aInnerLazyCache[$sCacheFieldName];
00275                             } else {
00276                                 return null;
00277                             }
00278                         } else {
00279                             return null;
00280                         }
00281                     } elseif ( array_key_exists( $sCacheFieldName, $this->_aInnerLazyCache ) ) {
00282                         $sFieldValue = $this->_aInnerLazyCache[$sCacheFieldName];
00283                     } else {
00284                         return null;
00285                     }
00286 
00287                     $this->_addField( $sFieldName, $iFieldStatus );
00288                     $this->_setFieldData( $sFieldName, $sFieldValue );
00289 
00290                     //save names to cache for next loading
00291                     if ($this->_sCacheKey) {
00292                         $myUtils = oxRegistry::getUtils();
00293                         $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00294                         $aFieldNames = $myUtils->fromFileCache( $sCacheKey );
00295                         $aFieldNames[$sFieldName] = $iFieldStatus;
00296                         $myUtils->toFileCache( $sCacheKey, $aFieldNames );
00297                     }
00298                 } catch ( Exception $e ) {
00299                     return null;
00300                 }
00301 
00302                 //do not use field cache for this page
00303                 //as if we use it for lists then objects are loaded empty instead of lazy loading.
00304                 self::$_blDisableFieldCaching[get_class( $this )] = true;
00305             }
00306 
00307             oxUtilsObject::getInstance()->resetInstanceCache(get_class($this));
00308         }
00309 
00310         //returns stdClass implementing __toString() method due to uknown scenario where this var should be used.
00311         if (!isset( $this->$sName ) ) {
00312             $this->$sName = null;
00313         }
00314 
00315         return $this->$sName;
00316     }
00317 
00325     public function __isset($mVar)
00326     {
00327         return isset($this->$mVar);
00328     }
00329 
00335     public function __clone()
00336     {
00337         if (!$this->_blIsSimplyClonable) {
00338             foreach ( $this->_aFieldNames as $sField => $sVal ) {
00339                 $sLongName = $this->_getFieldLongName( $sField );
00340                 if ( is_object($this->$sLongName)) {
00341                     $this->$sLongName = clone $this->$sLongName;
00342                 }
00343             }
00344         }
00345     }
00346 
00354     public function oxClone($oObject)
00355     {
00356         $aClasVars = get_object_vars( $oObject);
00357         while (list($name, $value) = each($aClasVars)) {
00358             if ( is_object( $oObject->$name ) ) {
00359                 $this->$name = clone $oObject->$name;
00360             } else {
00361                 $this->$name = $oObject->$name;
00362             }
00363         }
00364     }
00365 
00374     public function init( $sTableName = null, $blForceAllFields = false)
00375     {
00376 
00377         if ( !$sTableName ) {
00378             $sTableName = $this->_sCoreTable;
00379         } else {
00380             $this->_sCoreTable = $sTableName;
00381         }
00382 
00383         // reset view table
00384         $this->_sViewTable = false;
00385 
00386         if ( count( $this->_aFieldNames ) <= 1 ) {
00387             $this->_initDataStructure( $blForceAllFields );
00388         }
00389     }
00390 
00398     public function assign( $dbRecord )
00399     {
00400         if ( !is_array( $dbRecord ) ) {
00401             return;
00402         }
00403 
00404 
00405         reset($dbRecord );
00406         while ( list( $sName, $sValue ) = each( $dbRecord ) ) {
00407 
00408             // patch for IIS
00409             //TODO: test it on IIS do we still need it
00410             //if( is_array($value) && count( $value) == 1)
00411             //    $value = current( $value);
00412 
00413             $this->_setFieldData( $sName, $sValue );
00414         }
00415 
00416         $sOxidField = $this->_getFieldLongName( 'oxid' );
00417         $this->_sOXID = $this->$sOxidField->value;
00418 
00419     }
00420 
00426     public function getClassName()
00427     {
00428         return $this->_sClassName;
00429     }
00430 
00436     public function getCoreTableName()
00437     {
00438         return $this->_sCoreTable;
00439     }
00440 
00446     public function getId()
00447     {
00448         return $this->_sOXID;
00449     }
00450 
00458     public function setId($sOXID = null)
00459     {
00460         if ( $sOXID ) {
00461             $this->_sOXID = $sOXID;
00462         } else {
00463             $this->_sOXID = oxUtilsObject::getInstance()->generateUID();
00464         }
00465 
00466         $sIdVarName = $this->_sCoreTable . "__oxid";
00467         $this->$sIdVarName = new oxField($this->_sOXID, oxField::T_RAW);
00468 
00469         return $this->_sOXID;
00470     }
00471 
00479     public function setShopId($iShopId)
00480     {
00481         $this->_iShopId = $iShopId;
00482     }
00483 
00489     public function getShopId()
00490     {
00491         return $this->_iShopId;
00492     }
00493 
00501     public function getViewName($blForceCoreTableUsage = null)
00502     {
00503         if (!$this->_sViewTable || ($blForceCoreTableUsage !== null)) {
00504             if ( $blForceCoreTableUsage === true ) {
00505                 return $this->_sCoreTable;
00506             }
00507 
00508 
00509             if ( ( $blForceCoreTableUsage !== null ) && $blForceCoreTableUsage ) {
00510                 $iShopId = -1;
00511             } else {
00512                 $iShopId = oxRegistry::getConfig()->getShopId();
00513             }
00514 
00515 
00516             $sViewName = getViewName( $this->_sCoreTable, $this->_blEmployMultilanguage == false ? -1 : $this->getLanguage(), $iShopId );
00517             if ( $blForceCoreTableUsage !== null ) {
00518                 return $sViewName;
00519             }
00520             $this->_sViewTable = $sViewName;
00521         }
00522         return $this->_sViewTable;
00523     }
00524 
00533     public function modifyCacheKey( $sCacheKey, $blOverride = false )
00534     {
00535         if ( $blOverride ) {
00536             $this->_sCacheKey = $sCacheKey;
00537         } else {
00538             $this->_sCacheKey .= $sCacheKey;
00539         }
00540     }
00541 
00547     public function disableLazyLoading()
00548     {
00549         $this->_blUseLazyLoading = false;
00550         $this->_initDataStructure(true);
00551     }
00552 
00553 
00559     public function isDerived()
00560     {
00561 
00562         return $this->_blIsDerived;
00563     }
00564 
00572     public function setIsDerived($blVal)
00573     {
00574         $this->_blIsDerived = $blVal;
00575     }
00576 
00583     public function isMultilang()
00584     {
00585         return false;
00586     }
00587 
00597     public function load( $sOXID)
00598     {
00599         /*
00600         if( !isset($oxID)){
00601             $oEx = oxNew('oxObjectException','core');
00602             $oEx->setMessage('EXCEPTION_OBJECT_OXIDNOTSET');
00603             $oEx->setObject($this);
00604             throw $oEx;
00605         }*/
00606 
00607         $blExistingOldForceCoreTable = $this->_blForceCoreTableUsage;
00608 
00609         $this->_blForceCoreTableUsage = true;
00610 
00611         //getting at least one field before lazy loading the object
00612         $this->_addField('oxid', 0);
00613         $sSelect = $this->buildSelectString( array( $this->getViewName().".oxid" => $sOXID));
00614         $this->_isLoaded = $this->assignRecord( $sSelect );
00615 
00616         $this->_blForceCoreTableUsage = $blExistingOldForceCoreTable;
00617 
00618         return $this->_isLoaded;
00619     }
00620 
00626     public function isLoaded()
00627     {
00628         return $this->_isLoaded;
00629     }
00630 
00638     public function buildSelectString( $aWhere = null)
00639     {
00640         $oDB = oxDb::getDb();
00641         $myUtils = oxRegistry::getUtils();
00642 
00643         $sGet = $this->getSelectFields();
00644         $sSelect = "select $sGet from " . $this->getViewName() . " where 1 ";
00645 
00646         if ( $aWhere) {
00647             reset($aWhere);
00648             while (list($name, $value) = each($aWhere)) {
00649                 $sSelect .=  " and " . $name.' = '.$oDB->quote($value);
00650             }
00651         }
00652 
00653         // add active shop
00654 
00655         return $sSelect;
00656     }
00657 
00665     public function assignRecord( $sSelect )
00666     {
00667         $blRet = false;
00668 
00669         $rs = oxDb::getDb( oxDb::FETCH_MODE_ASSOC )->select( $sSelect );
00670 
00671         if ($rs != false && $rs->recordCount() > 0) {
00672             $blRet = true;
00673             $this->assign( $rs->fields);
00674         }
00675 
00676         return $blRet;
00677     }
00678 
00686     public function getFieldData( $sFieldName )
00687     {
00688         $sLongFieldName = $this->_getFieldLongName( $sFieldName );
00689             return $this->$sLongFieldName->value;
00690     }
00691 
00699     public function getSelectFields( $blForceCoreTableUsage = null )
00700     {
00701         $aSelectFields = array();
00702 
00703         $sViewName = $this->getViewName( $blForceCoreTableUsage );
00704 
00705         foreach ( $this->_aFieldNames as $sKey => $sField ) {
00706             $aSelectFields[] = $sViewName . '.' . $sKey;
00707         }
00708 
00709         $sSelectFields = join( ", ", $aSelectFields );
00710         return $sSelectFields;
00711     }
00712 
00720     public function delete( $sOXID = null)
00721     {
00722         if ( !$sOXID ) {
00723             $sOXID = $this->getId();
00724 
00725             //do not allow derived deletion
00726             if ( !$this->allowDerivedDelete() ) {
00727                 return false;
00728             }
00729         }
00730 
00731         if ( !$sOXID ) {
00732             return false;
00733         }
00734 
00735 
00736         $oDB = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00737         $sDelete = "delete from $this->_sCoreTable where oxid = ".$oDB->quote( $sOXID );
00738         $rs = $oDB->execute( $sDelete );
00739         if ( $blDelete = ( bool ) $oDB->affected_Rows() ) {
00740             $this->onChange(ACTION_DELETE, $sOXID);
00741         }
00742 
00743         return $blDelete;
00744     }
00745 
00746 
00752     public function save()
00753     {
00754         if ( !is_array( $this->_aFieldNames ) ) {
00755             return false;
00756         }
00757 
00758         $blRet = false;
00759 
00760         // #739A - should be executed here because of date/time formatting feature
00761         if ( $this->isAdmin() && !$this->getConfig()->getConfigParam( 'blSkipFormatConversion' ) ) {
00762             foreach ($this->_aFieldNames as $sName => $sVal) {
00763                 $sLongName = $this->_getFieldLongName($sName);
00764                 if ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "datetime" ) {
00765                     oxDb::getInstance()->convertDBDateTime( $this->$sLongName, true );
00766                 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "timestamp" ) {
00767                     oxDb::getInstance()->convertDBTimestamp( $this->$sLongName, true);
00768                 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "date" ) {
00769                     oxDb::getInstance()->convertDBDate( $this->$sLongName, true);
00770                 }
00771             }
00772         }
00773         if ( $this->exists() ) {
00774             //do not allow derived update
00775             if ( !$this->allowDerivedUpdate() ) {
00776                 return false;
00777             }
00778 
00779             $blRet = $this->_update();
00780             $sAction = ACTION_UPDATE;
00781         } else {
00782             $blRet = $this->_insert();
00783             $sAction = ACTION_INSERT;
00784         }
00785 
00786         $this->onChange($sAction);
00787 
00788         if ( $blRet ) {
00789             return $this->getId();
00790         } else {
00791             return false;
00792         }
00793     }
00794 
00800     public function allowDerivedUpdate()
00801     {
00802         return !$this->isDerived();
00803     }
00804 
00810     public function allowDerivedDelete()
00811     {
00812         return !$this->isDerived();
00813     }
00814 
00822     public function exists( $sOXID = null)
00823     {
00824         if ( !$sOXID ) {
00825             $sOXID = $this->getId();
00826         }
00827         if ( !$sOXID ) {
00828             return false;
00829         }
00830 
00831         $sViewName = $this->getCoreTableName();
00832         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00833         $sSelect= "select {$this->_sExistKey} from {$sViewName} where {$this->_sExistKey} = ".$oDb->quote( $sOXID );
00834 
00835         return ( bool ) $oDb->getOne( $sSelect );
00836     }
00837 
00845     public function getSqlActiveSnippet( $blForceCoreTable = null )
00846     {
00847         $sQ = '';
00848         $sTable = $this->getViewName($blForceCoreTable);
00849 
00850         // has 'active' field ?
00851         if ( isset( $this->_aFieldNames['oxactive'] ) ) {
00852             $sQ = " $sTable.oxactive = 1 ";
00853         }
00854 
00855         // has 'activefrom'/'activeto' fields ?
00856         if ( isset( $this->_aFieldNames['oxactivefrom'] ) && isset( $this->_aFieldNames['oxactiveto'] ) ) {
00857 
00858             $sDate = date( 'Y-m-d H:i:s', oxRegistry::get("oxUtilsDate")->getTime() );
00859 
00860             $sQ = $sQ?" $sQ or ":'';
00861             $sQ = " ( $sQ ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00862         }
00863 
00864         return $sQ;
00865     }
00866 
00875     public function beforeUpdate( $sOXID = null )
00876     {
00877     }
00878 
00889     public function onChange( $iAction = null, $sOXID = null)
00890     {
00891     }
00892 
00893 
00899     public function setInList()
00900     {
00901         $this->_blIsInList = true;
00902     }
00903 
00909     protected function _isInList()
00910     {
00911         return $this->_blIsInList;
00912     }
00913 
00922     protected function _getObjectViewName( $sTable, $sShopID = null)
00923     {
00924         return getViewName( $sTable, -1, $sShopID);
00925     }
00926 
00927 
00938     protected function _getTableFields($sTable, $blReturnSimple = false)
00939     {
00940         $myUtils = oxRegistry::getUtils();
00941 
00942         $sCacheKey   = $sTable . "_allfields_" . $blReturnSimple;
00943         $aMetaFields = $myUtils->fromFileCache( $sCacheKey );
00944 
00945         if ( $aMetaFields ) {
00946             return $aMetaFields;
00947         }
00948 
00949         $aMetaFields = oxDb::getInstance()->getTableDescription( $sTable );
00950 
00951         if ( !$blReturnSimple ) {
00952             $myUtils->toFileCache( $sCacheKey, $aMetaFields );
00953             return $aMetaFields;
00954         }
00955 
00956         //returning simple array
00957         $aRet = array();
00958         if (is_array($aMetaFields)) {
00959             foreach ( $aMetaFields as $oVal ) {
00960                 $aRet[strtolower( $oVal->name )] = 0;
00961             }
00962         }
00963 
00964         $myUtils->toFileCache( $sCacheKey, $aRet);
00965 
00966         return $aRet;
00967     }
00968 
00980     protected function _getAllFields($blReturnSimple = false)
00981     {
00982         if (!$this->_sCoreTable) {
00983             return array();
00984         }
00985         return $this->_getTableFields($this->_sCoreTable, $blReturnSimple);
00986     }
00987 
00996     protected function _initDataStructure($blForceFullStructure = false)
00997     {
00998         $myUtils = oxRegistry::getUtils();
00999 
01000         //get field names from cache
01001         $aFieldNames = null;
01002         $sFullCacheKey = 'fieldnames_' .$this->_sCoreTable . "_" . $this->_sCacheKey;
01003         if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01004             $aFieldNames = $myUtils->fromFileCache($sFullCacheKey);
01005         }
01006 
01007         if (!$aFieldNames) {
01008             $aFieldNames = $this->_getNonCachedFieldNames($blForceFullStructure);
01009             if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01010                 $myUtils->toFileCache($sFullCacheKey, $aFieldNames);
01011             }
01012         }
01013 
01014         if ( $aFieldNames !== false ) {
01015             foreach ( $aFieldNames as $sField => $sStatus ) {
01016                 $this->_addField($sField, $sStatus);
01017             }
01018         }
01019     }
01020 
01032     protected function _getNonCachedFieldNames($blForceFullStructure = false)
01033     {
01034         //T2008-02-22
01035         //so if this method is executed on cached version we see it when profiling
01036         startProfile("!__CACHABLE__!");
01037 
01038         //case 1. (admin)
01039         if ($this->isAdmin()) {
01040             $aMetaFields = $this->_getAllFields();
01041             foreach ( $aMetaFields as $oField ) {
01042                 if ( $oField->max_length == -1 ) {
01043                     $oField->max_length = 10;      // double or float
01044                 }
01045 
01046                 if ( $oField->type == "datetime" ) {
01047                     $oField->max_length = 20;
01048                 }
01049 
01050                 $this->_addField( $oField->name, $this->_getFieldStatus($oField->name), $oField->type, $oField->max_length );
01051             }
01052             stopProfile("!__CACHABLE__!");
01053             return false;
01054         }
01055 
01056         //case 2. (just get all fields)
01057         if ( $blForceFullStructure || !$this->_blUseLazyLoading ) {
01058             $aMetaFields = $this->_getAllFields(true);
01059             /*
01060             foreach ( $aMetaFields as $sFieldName => $sVal) {
01061                 $this->_addField( $sFieldName, $this->_getFieldStatus($sFieldName));
01062             }*/
01063             stopProfile("!__CACHABLE__!");
01064             return $aMetaFields;
01065         }
01066 
01067         //case 3. (get only oxid field, so we can fetch the rest of the fields over lazy loading mechanism)
01068         stopProfile("!__CACHABLE__!");
01069         return array("oxid" => 0);
01070     }
01071 
01080     protected function _getFieldStatus( $sFieldName )
01081     {
01082         return 0;
01083     }
01084 
01095     protected function _addField($sName, $iStatus, $sType = null, $sLength = null)
01096     {
01097         //preparation
01098         $sName = strtolower( $sName );
01099 
01100         //adding field names element
01101         $this->_aFieldNames[$sName] = $iStatus;
01102 
01103         //allready set?
01104         $sLongName = $this->_getFieldLongName($sName);
01105         if ( isset($this->$sLongName) ) {
01106             return;
01107         }
01108 
01109         //defining the field
01110         $oField = false;
01111 
01112         if ( isset( $sType ) ) {
01113             $oField = new oxField();
01114             $oField->fldtype = $sType;
01115             //T2008-01-29
01116             //can't clone as the fields are objects and are not fully cloned
01117             $this->_blIsSimplyClonable = false;
01118         }
01119 
01120         if ( isset( $sLength ) ) {
01121             if ( !$oField ) {
01122                 $oField = new oxField();
01123             }
01124             $oField->fldmax_length = $sLength;
01125             $this->_blIsSimplyClonable = false;
01126         }
01127 
01128         $this->$sLongName = $oField;
01129     }
01130 
01138     protected function _getFieldLongName( $sFieldName )
01139     {
01140         //trying to avoid strpos call as often as possible
01141         if ( $sFieldName[2] == $this->_sCoreTable[2] && strpos( $sFieldName, $this->_sCoreTable . "__" ) === 0 ) {
01142             return $sFieldName;
01143         }
01144 
01145         return $this->_sCoreTable . "__" . strtolower( $sFieldName );
01146     }
01147 
01157     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
01158     {
01159 
01160         $sLongFieldName = $this->_getFieldLongName( $sFieldName);
01161         //$sLongFieldName = $this->_sCoreTable . "__" . strtolower($sFieldName);
01162 
01163         //T2008-03-14
01164         //doing this because in lazy loaded lists on first load it is harmful to have initilised fields but not yet set
01165         //situation: only first article is loaded fully for "select oxid from oxarticles"
01166         /*
01167         if ($this->_blUseLazyLoading && !isset($this->$sLongFieldName))
01168             return;*/
01169 
01170         //in non lazy loading case we just add a field and do not care about it more
01171         if (!$this->_blUseLazyLoading && !isset($this->$sLongFieldName)) {
01172             $aFields = $this->_getAllFields(true);
01173             if ( isset( $aFields[strtolower($sFieldName)] ) ) {
01174                 $this->_addField($sFieldName, $this->_getFieldStatus($sFieldName));
01175             }
01176         }
01177         // if we have a double field we replace "," with "." in case somebody enters it in european format
01178         if (isset($this->$sLongFieldName) && isset($this->$sLongFieldName->fldtype) && $this->$sLongFieldName->fldtype == "double") {
01179             $sValue = str_replace( ",", ".", $sValue );
01180         }
01181 
01182         // isset is REQUIRED here not to use getter
01183         if (isset($this->$sLongFieldName) && is_object($this->$sLongFieldName)) {
01184             $this->$sLongFieldName->setValue($sValue, $iDataType);
01185         } else {
01186             $this->$sLongFieldName = new oxField($sValue, $iDataType);
01187         }
01188 
01189     }
01190 
01198     protected function _canFieldBeNull( $sFieldName )
01199     {
01200         $aMetaData = $this->_getAllFields();
01201         foreach ( $aMetaData as $oMetaInfo ) {
01202             if ( strcasecmp( $oMetaInfo->name, $sFieldName ) == 0 ) {
01203                 return !$oMetaInfo->not_null;
01204             }
01205         }
01206         return false;
01207     }
01208 
01209 
01217     protected function _getFieldDefaultValue( $sFieldName )
01218     {
01219         $aMetaData = $this->_getAllFields();
01220         foreach ( $aMetaData as $oMetaInfo ) {
01221             if ( strcasecmp( $oMetaInfo->name, $sFieldName ) == 0 ) {
01222                 return $oMetaInfo->default_value;
01223             }
01224         }
01225         return false;
01226     }
01227 
01228 
01237     protected function _getUpdateFieldValue( $sFieldName, $oField )
01238     {
01239         $mValue = null;
01240         if ( $oField instanceof oxField ) {
01241             $mValue = $oField->getRawValue();
01242         } elseif ( isset( $oField->value ) ) {
01243             $mValue = $oField->value;
01244         }
01245 
01246         $oDb = oxDb::getDb();
01247         //Check if this field value is null AND it can be null according if not returning default value
01248         if ( ( null === $mValue ) ) {
01249             if ( $this->_canFieldBeNull( $sFieldName ) ) {
01250                 return 'null';
01251             } elseif ( $mValue = $this->_getFieldDefaultValue( $sFieldName ) ) {
01252                 return $oDb->quote( $mValue );
01253             }
01254         }
01255 
01256         return $oDb->quote( $mValue );
01257     }
01258 
01267     protected function _getUpdateFields( $blUseSkipSaveFields = true )
01268     {
01269         $sSql = '';
01270         $blSep  = false;
01271 
01272         foreach (array_keys($this->_aFieldNames) as $sKey) {
01273             $sLongName = $this->_getFieldLongName($sKey);
01274             $oField = $this->$sLongName;
01275 
01276 
01277             if ( !$blUseSkipSaveFields || ($blUseSkipSaveFields && !in_array(strtolower($sKey), $this->_aSkipSaveFields)) ) {
01278                 $sSql .= (( $blSep) ? ',':'' ).$sKey." = ".$this->_getUpdateFieldValue($sKey, $oField);
01279                 $blSep = true;
01280             }
01281         }
01282 
01283         return $sSql;
01284     }
01285 
01295     protected function _update()
01296     {
01297         //do not allow derived item update
01298         if ( !$this->allowDerivedUpdate() ) {
01299             return false;
01300         }
01301 
01302 
01303         if ( !$this->getId() ) {
01304             $oEx = oxNew( 'oxObjectException' );
01305             $oEx->setMessage( 'EXCEPTION_OBJECT_OXIDNOTSET' );
01306             $oEx->setObject($this);
01307             throw $oEx;
01308         }
01309 
01310         $sIDKey = oxRegistry::getUtils()->getArrFldName( $this->_sCoreTable.".oxid");
01311         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01312         $oDb = oxDb::getDb();
01313 
01314         $sUpdate= "update {$this->_sCoreTable} set ".$this->_getUpdateFields()
01315                  ." where {$this->_sCoreTable}.oxid = ".$oDb->quote( $this->getId() );
01316 
01317         //trigger event
01318         $this->beforeUpdate();
01319 
01320         $blRet = (bool) $oDb->execute( $sUpdate);
01321 
01322         return $blRet;
01323     }
01324 
01332     protected function _insert()
01333     {
01334 
01335         $oDb      = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
01336         $myConfig = $this->getConfig();
01337         $myUtils  = oxRegistry::getUtils();
01338 
01339         // let's get a new ID
01340         if ( !$this->getId()) {
01341             $this->setId();
01342         }
01343 
01344         $sIDKey = $myUtils->getArrFldName( $this->_sCoreTable.".oxid");
01345         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01346         $sInsert= "Insert into {$this->_sCoreTable} set ";
01347 
01348         //setting oxshopid
01349         $sShopField = $myUtils->getArrFldName($this->_sCoreTable.".oxshopid");
01350 
01351         if (isset($this->$sShopField) && !$this->$sShopField->value) {
01352             $this->$sShopField = new oxField($myConfig->getShopId(), oxField::T_RAW);
01353         }
01354 
01355 
01356         $sInsert .= $this->_getUpdateFields( $this->getUseSkipSaveFields() );
01357 
01358         $blRet = (bool) $oDb->execute( $sInsert);
01359 
01360         return $blRet;
01361     }
01362 
01369     protected function _isDisabledFieldCache()
01370     {
01371         $sClass = get_class($this);
01372         if (isset(self::$_blDisableFieldCaching[$sClass]) && self::$_blDisableFieldCaching[$sClass]) {
01373             return true;
01374         }
01375 
01376         return false;
01377     }
01378 
01384     public function isOx()
01385     {
01386         $sOxId = $this->getId();
01387         if ( $sOxId[0] == 'o' && $sOxId[1] == 'x' ) {
01388             return true;
01389         }
01390         return false;
01391     }
01392 
01398     public function isReadOnly()
01399     {
01400         return $this->_blReadOnly;
01401     }
01402 
01410     public function setReadOnly( $blReadOnly )
01411     {
01412         $this->_blReadOnly = $blReadOnly;
01413     }
01414 
01420     public function getFieldNames()
01421     {
01422         return array_keys( $this->_aFieldNames );
01423     }
01424 
01432     public function addFieldName( $sName )
01433     {
01434         //preparation
01435         $sName = strtolower( $sName );
01436         $this->_aFieldNames[$sName] = 0;
01437     }
01438 
01439 
01445     public function getLanguage()
01446     {
01447         return -1;
01448     }
01449 }