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 $_aErrors = array();
00042 
00047     protected $_sClassName = 'oxbase';
00048 
00054     protected $_sCoreTable = null;
00055 
00060     protected $_sViewTable  = null;
00061 
00062 
00068     protected $_aFieldNames = array('oxid' => 0);
00069 
00074     protected $_blIsNewCache = false;
00075 
00081     protected $_sCacheKey = null;
00082 
00088     protected $_blUseLazyLoading = false;
00089 
00095     protected $_aSkipSaveFields = array();
00096 
00101     protected $_sExistKey = "oxid";
00102 
00109     protected $_blIsDerived = null;
00110 
00120     protected static $_blDisableFieldCaching = array();
00121 
00127     protected $_blIsSeoObject = false;
00128 
00134     protected $_blReadOnly = false;
00135 
00141     protected $_blIsInList = false;
00142 
00148     protected $_isLoaded = false;
00149 
00153     public function __construct()
00154     {
00155         // set active shop
00156         $myConfig = $this->getConfig();
00157         $this->_sCacheKey = $this->getViewName();
00158         if ( $this->_blUseLazyLoading ) {
00159             $this->_sCacheKey .= $myConfig->getActiveView()->getClassName();
00160         } else {
00161             $this->_sCacheKey .= "allviews";
00162         }
00163 
00164         //do not cache for admin?
00165         if ( $this->isAdmin() ) {
00166             $this->_sCacheKey = null;
00167         }
00168 
00169         $this->setShopId( $myConfig->getShopId() );
00170     }
00171 
00180     public function __set($sName, $sValue)
00181     {
00182         $this->$sName = $sValue;
00183         if ( $this->_blUseLazyLoading && strpos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00184             $sFieldName = str_replace( $this->_sCoreTable . "__", '', $sName );
00185             if ($sFieldName != 'oxnid' && !$this->_aFieldNames[$sFieldName]) {
00186                 $aAllFields = $this->_getAllFields(true);
00187                 if (isset($aAllFields[strtolower($sFieldName)])) {
00188                     $iFieldStatus = $this->_getFieldStatus($sFieldName);
00189                     $this->_addField($sFieldName, $iFieldStatus);
00190                 }
00191             }
00192         }
00193     }
00194 
00202     public function __get( $sName )
00203     {
00204         switch ($sName) {
00205             case 'blIsDerived':
00206                 return $this->isDerived();
00207                 break;
00208             case 'sOXID':
00209                 return $this->getId();
00210                 break;
00211             case 'blReadOnly':
00212                 return $this->isReadOnly();
00213                 break;
00214         }
00215 
00216         // implementing lazy loading fields
00217         // This part of the code is slow and normally is called before field cache is built.
00218         // Make sure it is not called after first page is loaded and cache data is fully built.
00219         if ( $this->_blUseLazyLoading && strpos( $sName, $this->_sCoreTable . "__" ) === 0 ) {
00220 
00221             //lazy load it
00222             $sFieldName = str_replace($this->_sCoreTable . "__", '', $sName);
00223             //$sFieldName = $this->getSqlFieldName($sFieldName);
00224             $iFieldStatus = $this->_getFieldStatus($sFieldName);
00225 
00226             $sSqlFieldName = $sFieldName;
00227             if ($iFieldStatus && $this->isMultilang() ) {
00228                 $sSqlFieldName =  $sFieldName.oxLang::getInstance()->getLanguageTag( $this->getLanguage() );
00229             }
00230 
00231             if ( $this->getId() ) {
00232                 $oDb = oxDb::getDb();
00233                 $sQ = "select $sSqlFieldName from " . $this->_sCoreTable . " where oxid = " . $oDb->quote($this->getId());
00234 
00235                 try {
00236                     //$sValue = $oDb->getOne( $sQ );
00237                     $rs = $oDb->execute( $sQ );
00238                     if ( $rs === false ) {
00239                         return null;
00240                     }
00241 
00242                     $this->_addField($sFieldName, $iFieldStatus);
00243                     $sValue = $rs->fields[0];
00244                     $this->_setFieldData( $sFieldName, $sValue );
00245 
00246                     //save names to cache for next loading
00247                     if ($this->_sCacheKey) {
00248                         $myUtils = oxUtils::getInstance();
00249                         $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00250                         $aFieldNames = $myUtils->fromFileCache($sCacheKey);
00251                         $aFieldNames[$sFieldName] = $iFieldStatus;
00252                         $myUtils->toFileCache($sCacheKey, $aFieldNames);
00253                     }
00254 
00255                 } catch ( Exception $e ) {
00256                     return null;
00257                 }
00258 
00259                 //do not use field cache for this page
00260                 //as if we use it for lists then objects are loaded empty instead of lazy lodaing.
00261                 self::$_blDisableFieldCaching[get_class($this)] = true;
00262             }
00263 
00264             /*
00265             //save names to cache for next loading
00266             if ($this->_sCacheKey) {
00267                 $sCacheKey = 'fieldnames_' . $this->_sCoreTable . "_" . $this->_sCacheKey;
00268                 $aFieldNames = oxUtils::getInstance()->fromFileCache($sCacheKey);
00269                 $aFieldNames[$sFieldName] = $iFieldStatus;
00270                 oxUtils::getInstance()->toFileCache($sCacheKey, $aFieldNames);
00271             }*/
00272 
00273             oxUtilsObject::getInstance()->resetInstanceCache(get_class($this));
00274         }
00275 
00276         //returns oxStdClass implementing __toString() method due to uknown scenario where this var should be used.
00277         if (!isset( $this->$sName ) ) {
00278             $this->$sName = null;
00279         }
00280 
00281         return $this->$sName;
00282     }
00283 
00291     public function __isset($mVar)
00292     {
00293         return isset($this->$mVar);
00294     }
00295 
00301     public function __clone()
00302     {
00303         if (!$this->_blIsSimplyClonable) {
00304             foreach ( $this->_aFieldNames as $sField => $sVal ) {
00305                 $sLongName = $this->_getFieldLongName( $sField );
00306                 if ( is_object($this->$sLongName)) {
00307                     $this->$sLongName = clone $this->$sLongName;
00308                 }
00309             }
00310         }
00311     }
00312 
00320     public function oxClone($oObject)
00321     {
00322         $aClasVars = get_object_vars( $oObject);
00323         while (list($name, $value) = each($aClasVars)) {
00324             if ( is_object( $oObject->$name ) ) {
00325                 $this->$name = clone $oObject->$name;
00326             } else {
00327                 $this->$name = $oObject->$name;
00328             }
00329         }
00330     }
00331 
00340     public function init( $sTableName = null, $blForceAllFields = false)
00341     {
00342 
00343         if ( !$sTableName ) {
00344             $sTableName = $this->_sCoreTable;
00345         } else {
00346             $this->_sCoreTable = $sTableName;
00347         }
00348 
00349         $this->_sViewTable = getViewName( $this->_sCoreTable );
00350 
00351         //do not use views?
00352 
00353         if ( count( $this->_aFieldNames ) <= 1 ) {
00354             $this->_initDataStructure( $blForceAllFields );
00355         }
00356     }
00357 
00365     public function assign( $dbRecord )
00366     {
00367         if ( !is_array( $dbRecord ) ) {
00368             return;
00369         }
00370 
00371 
00372         reset($dbRecord );
00373         while ( list( $sName, $sValue ) = each( $dbRecord ) ) {
00374 
00375             // patch for IIS
00376             //TODO: test it on IIS do we still need it
00377             //if( is_array($value) && count( $value) == 1)
00378             //    $value = current( $value);
00379 
00380             $this->_setFieldData( $sName, $sValue );
00381         }
00382 
00383         $sOxidField = $this->_getFieldLongName( 'oxid' );
00384         $this->_sOXID = $this->$sOxidField->value;
00385 
00386     }
00387 
00393     public function getClassName()
00394     {
00395         return $this->_sClassName;
00396     }
00397 
00403     public function getCoreTableName()
00404     {
00405         return $this->_sCoreTable;
00406     }
00407 
00413     public function getId()
00414     {
00415         return $this->_sOXID;
00416     }
00417 
00425     public function setId($sOXID = null)
00426     {
00427         if ( $sOXID ) {
00428             $this->_sOXID = $sOXID;
00429         } else {
00430             $this->_sOXID = oxUtilsObject::getInstance()->generateUID();
00431         }
00432 
00433         $sIdVarName = $this->_sCoreTable . "__oxid";
00434         $this->$sIdVarName = new oxField($this->_sOXID, oxField::T_RAW);
00435 
00436         return $this->_sOXID;
00437     }
00438 
00446     public function setShopId($iShopId)
00447     {
00448         $this->_iShopId = $iShopId;
00449     }
00450 
00456     public function getShopId()
00457     {
00458         return $this->_iShopId;
00459     }
00460 
00466     public function getViewName()
00467     {
00468         return $this->_sViewTable;
00469     }
00470 
00479     public function modifyCacheKey( $sCacheKey, $blOverride = false )
00480     {
00481         if ( $blOverride ) {
00482             $this->_sCacheKey = $sCacheKey;
00483         } else {
00484             $this->_sCacheKey .= $sCacheKey;
00485         }
00486     }
00487 
00493     public function disableLazyLoading()
00494     {
00495         $this->_blUseLazyLoading = false;
00496         $this->_initDataStructure(true);
00497     }
00498 
00499 
00505     public function isDerived()
00506     {
00507 
00508         return $this->_blIsDerived;
00509     }
00510 
00518     public function setIsDerived($blVal)
00519     {
00520         $this->_blIsDerived = $blVal;
00521     }
00522 
00529     public function isMultilang()
00530     {
00531         return false;
00532     }
00533 
00543     public function load( $sOXID)
00544     {
00545         /*
00546         if( !isset($oxID)){
00547             $oEx = oxNew('oxObjectException','core');
00548             $oEx->setMessage('EXCEPTION_OBJECT_OXIDNOTSET');
00549             $oEx->setObject($this);
00550             throw $oEx;
00551         }*/
00552 
00553         //getting at least one field before lazy loading the object
00554         $this->_addField('oxid', 0);
00555 
00556         $sSelect = $this->buildSelectString( array( $this->getViewName().".oxid" => $sOXID));
00557 
00558         return $this->_isLoaded = $this->assignRecord( $sSelect );
00559     }
00560 
00566     public function isLoaded()
00567     {
00568         return $this->_isLoaded;
00569     }
00570 
00578     public function buildSelectString( $aWhere = null)
00579     {
00580         $oDB = oxDb::getDb(true);
00581         $myUtils = oxUtils::getInstance();
00582 
00583         $sGet = $this->getSelectFields();
00584         $sSelect = "select $sGet from " . $this->_sViewTable . " where 1 ";
00585 
00586         if ( $aWhere) {
00587             reset($aWhere);
00588             while (list($name, $value) = each($aWhere)) {
00589                 $sSelect .=  " and " . $name.' = '.$oDB->quote($value);
00590             }
00591         }
00592 
00593         // add active shop
00594 
00595         //echo( "<br><br>$sSelect <br><br>\n\n\n");
00596 
00597         return $sSelect;
00598     }
00599 
00607     public function assignRecord( $sSelect)
00608     {
00609         $blRet = false;
00610 
00611         $oDB = oxDb::getDb(true);
00612 
00613         $rs = $oDB->execute( $sSelect);
00614         if ($rs != false && $rs->recordCount() > 0) {
00615             $blRet = true;
00616             $this->assign( $rs->fields);
00617         }
00618 
00619         return $blRet;
00620     }
00621 
00629     public function getFieldData( $sFieldName)
00630     {
00631         $sLongFieldName = $this->_getFieldLongName($sFieldName);
00632         return $this->$sLongFieldName->value;
00633     }
00634 
00640     public function getSelectFields()
00641     {
00642         $aSelectFields = array();
00643 
00644         foreach ( $this->_aFieldNames as $sKey => $sField ) {
00645             $sRealField = $this->getSqlFieldName($sKey);
00646             $sSelectSufix = "";
00647             if ( $sRealField !== $sKey ) {
00648                 $sSelectSufix = " as $sKey";
00649             }
00650             $aSelectFields[] = $this->_sViewTable . '.' . $sRealField . $sSelectSufix;
00651         }
00652 
00653         $sSelectFields = join( ", ", $aSelectFields );
00654         return $sSelectFields;
00655     }
00656 
00664     public function delete( $sOXID = null)
00665     {
00666         if ( !$sOXID ) {
00667             $sOXID = $this->getId();
00668 
00669             //do not allow derived deletion
00670             if ( !$this->allowDerivedDelete() ) {
00671                 return false;
00672             }
00673         }
00674 
00675         if ( !$sOXID ) {
00676             return false;
00677         }
00678 
00679 
00680         $oDB = oxDb::getDb(true);
00681         $sDelete = "delete from $this->_sCoreTable where oxid = ".$oDB->quote( $sOXID );
00682         $rs = $oDB->execute( $sDelete );
00683         if ( $blDelete = ( bool ) $oDB->affected_Rows() ) {
00684             $this->onChange(ACTION_DELETE, $sOXID);
00685         }
00686 
00687         return $blDelete;
00688     }
00689 
00690 
00696     public function save()
00697     {
00698         if ( !is_array( $this->_aFieldNames ) ) {
00699             return false;
00700         }
00701 
00702         $blRet = false;
00703 
00704         // #739A - should be executed here because of date/time formatting feature
00705         if ( $this->isAdmin() && !$this->getConfig()->getConfigParam( 'blSkipFormatConversion' ) ) {
00706             foreach ($this->_aFieldNames as $sName => $sVal) {
00707                 $sLongName = $this->_getFieldLongName($sName);
00708                 if ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "datetime" ) {
00709                     oxDb::getInstance()->convertDBDateTime( $this->$sLongName, true );
00710                 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "timestamp" ) {
00711                     oxDb::getInstance()->convertDBTimestamp( $this->$sLongName, true);
00712                 } elseif ( isset($this->$sLongName->fldtype) && $this->$sLongName->fldtype == "date" ) {
00713                     oxDb::getInstance()->convertDBDate( $this->$sLongName, true);
00714                 }
00715             }
00716         }
00717 
00718         if ( $this->exists() ) {
00719 
00720             //do not allow derived update
00721             if ( !$this->allowDerivedUpdate() ) {
00722                 return false;
00723             }
00724 
00725             $blRet = $this->_update();
00726             $sAction = ACTION_UPDATE;
00727         } else {
00728             $blRet = $this->_insert();
00729             $sAction = ACTION_INSERT;
00730         }
00731 
00732         $this->onChange($sAction);
00733 
00734         if ( $blRet ) {
00735             return $this->getId();
00736         } else {
00737             return false;
00738         }
00739     }
00740 
00746     public function allowDerivedUpdate()
00747     {
00748         return !$this->isDerived();
00749     }
00750 
00756     public function allowDerivedDelete()
00757     {
00758         return !$this->isDerived();
00759     }
00760 
00768     public function exists( $sOXID = null)
00769     {
00770         if ( !$sOXID ) {
00771             $sOXID = $this->getId();
00772         }
00773         if ( !$sOXID ) {
00774             return false;
00775         }
00776 
00777         $sViewName = $this->getViewName();
00778         $oDB = oxDb::getDb( true );
00779         $sSelect= "select {$this->_sExistKey} from {$sViewName} where {$this->_sExistKey} = ".$oDB->quote( $sOXID );
00780         return ( bool ) $oDB->getOne( $sSelect );
00781     }
00782 
00790     public function getSqlFieldName($sField)
00791     {
00792         return $sField;
00793     }
00794 
00802     public function getSqlActiveSnippet( $blForceCoreTable = false )
00803     {
00804         $sQ = '';
00805             $sTable = $this->getCoreTableName();
00806 
00807         // has 'active' field ?
00808         if ( isset( $this->_aFieldNames['oxactive'] ) ) {
00809             $sQ = " $sTable.oxactive = 1 ";
00810         }
00811 
00812         // has 'activefrom'/'activeto' fields ?
00813         if ( isset( $this->_aFieldNames['oxactivefrom'] ) && isset( $this->_aFieldNames['oxactiveto'] ) ) {
00814 
00815             $sDate = date( 'Y-m-d H:i:s', oxUtilsDate::getInstance()->getTime() );
00816 
00817             $sQ = $sQ?" $sQ or ":'';
00818             $sQ = " ( $sQ ( $sTable.oxactivefrom < '$sDate' and $sTable.oxactiveto > '$sDate' ) ) ";
00819         }
00820 
00821         return $sQ;
00822     }
00823 
00830     public function validate()
00831     {
00832         $this->_aErrors = array();
00833         foreach ($this->_aFieldNames as $fName => $iVal) {
00834 
00835             $fName = $this->_getFieldLongName($fName);
00836 
00837             if ( method_exists ( $this, "validate_$fName")) {
00838                 $validatorMethod = "validate_$fName";
00839                 if ( $error = $this->$validatorMethod()) {
00840                     $this->_aErrors[$fName] = $error;
00841                 }
00842             }
00843         }
00844         return !$this->hasErrors();
00845     }
00846 
00855     public function beforeUpdate( $sOXID = null )
00856     {
00857     }
00858 
00869     public function onChange( $iAction = null, $sOXID = null)
00870     {
00871     }
00872 
00873 
00879     public function hasErrors()
00880     {
00881         return count($this->_aErrors) > 0;
00882     }
00883 
00889     public function getErrors()
00890     {
00891         return $this->_aErrors;
00892     }
00893 
00901     public function getError( $sField)
00902     {
00903         if (isset($this->_aErrors[$sField])) {
00904             return $this->_aErrors[$sField];
00905         }
00906 
00907         //T2007-10-19
00908         //return array();
00909         return null;
00910     }
00911 
00919     public function getHtmlError( $sField)
00920     {
00921         if ( $error = $this->getError($sField) ) {
00922             return $error;
00923         }
00924     }
00925 
00931     public function setInList()
00932     {
00933         $this->_blIsInList = true;
00934     }
00935 
00941     protected function _isInList()
00942     {
00943         return $this->_blIsInList;
00944     }
00945 
00954     protected function _getObjectViewName( $sTable, $sShopID = null)
00955     {
00956             return $sTable;
00957 
00958     }
00959 
00960 
00970     protected function _getAllFields($blReturnSimple = false)
00971     {
00972         $myUtils = oxUtils::getInstance();
00973 
00974         if (!$this->_sCoreTable) {
00975             return array();
00976         }
00977 
00978         //T2008-09-04
00979         //The cache here saves almost 7% of execution time
00980         //But it seems that oxeec_tbdsc_oxarticles.txt and oxeec_oxarticles_allfields.txt tmp files are identical.
00981         //TODO: Check what's up with that.
00982         $sCacheKey   = $this->_sCoreTable . "_allfields_" . $blReturnSimple;
00983         $aMetaFields = $myUtils->fromFileCache( $sCacheKey );
00984 
00985         if ($aMetaFields) {
00986             return $aMetaFields;
00987         }
00988 
00989         $aMetaFields = oxDb::getInstance()->getTableDescription( $this->_sCoreTable );
00990 
00991         if ( !$blReturnSimple ) {
00992             $myUtils->toFileCache( $sCacheKey, $aMetaFields );
00993             return $aMetaFields;
00994         }
00995 
00996         //returning simple array
00997         $aRet = array();
00998         if (is_array($aMetaFields)) {
00999             foreach ( $aMetaFields as $oVal ) {
01000                 $aRet[strtolower( $oVal->name )] = 0;
01001             }
01002         }
01003 
01004         $myUtils->toFileCache( $sCacheKey, $aRet);
01005 
01006         return $aRet;
01007     }
01008 
01017     protected function _initDataStructure($blForceFullStructure = false)
01018     {
01019         $myUtils = oxUtils::getInstance();
01020 
01021         //get field names from cache
01022         $aFieldNames = null;
01023         $sFullCacheKey = 'fieldnames_' .$this->_sCoreTable . "_" . $this->_sCacheKey;
01024         if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01025             $aFieldNames = $myUtils->fromFileCache($sFullCacheKey);
01026         }
01027 
01028         if (!$aFieldNames) {
01029             $aFieldNames = $this->_getNonCachedFieldNames($blForceFullStructure);
01030             if ($this->_sCacheKey && !$this->_isDisabledFieldCache()) {
01031                 $myUtils->toFileCache($sFullCacheKey, $aFieldNames);
01032             }
01033         }
01034 
01035         if ( $aFieldNames !== false ) {
01036             foreach ( $aFieldNames as $sField => $sStatus ) {
01037                 $this->_addField($sField, $sStatus);
01038             }
01039         }
01040     }
01041 
01053     protected function _getNonCachedFieldNames($blForceFullStructure = false)
01054     {
01055         //T2008-02-22
01056         //so if this method is executed on cached version we see it when profiling
01057         startProfile("!__CACHABLE__!");
01058 
01059         //case 1. (admin)
01060         if ($this->isAdmin()) {
01061             $aMetaFields = $this->_getAllFields();
01062             foreach ( $aMetaFields as $oField ) {
01063                 if ( $oField->max_length == -1 ) {
01064                     $oField->max_length = 10;      // double or float
01065                 }
01066 
01067                 if ( $oField->type == "datetime" ) {
01068                     $oField->max_length = 20;
01069                 }
01070 
01071                 $this->_addField( $oField->name, $this->_getFieldStatus($oField->name), $oField->type, $oField->max_length );
01072             }
01073             stopProfile("!__CACHABLE__!");
01074             return false;
01075         }
01076 
01077         //case 2. (just get all fields)
01078         if ( $blForceFullStructure || !$this->_blUseLazyLoading ) {
01079             $aMetaFields = $this->_getAllFields(true);
01080             /*
01081             foreach ( $aMetaFields as $sFieldName => $sVal) {
01082                 $this->_addField( $sFieldName, $this->_getFieldStatus($sFieldName));
01083             }*/
01084             stopProfile("!__CACHABLE__!");
01085             return $aMetaFields;
01086         }
01087 
01088         //case 3. (get only oxid field, so we can fetch the rest of the fields over lazy loading mechanism)
01089         stopProfile("!__CACHABLE__!");
01090         return array("oxid" => 0);
01091     }
01092 
01101     protected function _getFieldStatus($sFieldName)
01102     {
01103         return 0;
01104     }
01105 
01116     protected function _addField($sName, $iStatus, $sType = null, $sLength = null)
01117     {
01118         //preparation
01119         $sName = strtolower($sName);
01120 
01121         //adding field names element
01122         $this->_aFieldNames[$sName] = $iStatus;
01123 
01124         //allready set?
01125         $sLongName = $this->_getFieldLongName($sName);
01126         if ( isset($this->$sLongName) ) {
01127             return;
01128         }
01129 
01130         //defining the field
01131         $oField = false;
01132 
01133         if (isset($sType)) {
01134             $oField = new oxField();
01135             $oField->fldtype = $sType;
01136             //T2008-01-29
01137             //can't clone as the fields are objects and are not fully cloned
01138             $this->_blIsSimplyClonable = false;
01139         }
01140 
01141         if (isset($sLength)) {
01142             if (!$oField) {
01143                 $oField = new oxField();
01144             }
01145             $oField->fldmax_length = $sLength;
01146             $this->_blIsSimplyClonable = false;
01147         }
01148 
01149         $this->$sLongName = $oField;
01150     }
01151 
01159     protected function _getFieldLongName( $sFieldName)
01160     {
01161         //trying to avoid strpos call as often as possible
01162         if ($sFieldName[2] == $this->_sCoreTable[2] && strpos($sFieldName, $this->_sCoreTable."__") === 0) {
01163             return $sFieldName;
01164         }
01165 
01166         $sLongName = $this->_sCoreTable."__".strtolower( $sFieldName);
01167         return $sLongName;
01168     }
01169 
01179     protected function _setFieldData( $sFieldName, $sValue, $iDataType = oxField::T_TEXT)
01180     {
01181 
01182 
01183         $sLongFieldName = $this->_getFieldLongName( $sFieldName);
01184         //$sLongFieldName = $this->_sCoreTable . "__" . strtolower($sFieldName);
01185 
01186         //T2008-03-14
01187         //doing this because in lazy loaded lists on first load it is harmful to have initilised fields but not yet set
01188         //situation: only first article is loaded fully for "select oxid from oxarticles"
01189         /*
01190         if ($this->_blUseLazyLoading && !isset($this->$sLongFieldName))
01191             return;*/
01192 
01193         //in non lazy loading case we just add a field and do not care about it more
01194         if (!$this->_blUseLazyLoading && !isset($this->$sLongFieldName)) {
01195             $aFields = $this->_getAllFields(true);
01196             if ( isset( $aFields[strtolower($sFieldName)] ) ) {
01197                 $this->_addField($sFieldName, $this->_getFieldStatus($sFieldName));
01198             }
01199         }
01200         // if we have a double field we replace "," with "." in case somebody enters it in european format
01201         if (isset($this->$sLongFieldName) && isset($this->$sLongFieldName->fldtype) && $this->$sLongFieldName->fldtype == "double") {
01202             $sValue = str_replace( ",", ".", $sValue );
01203         }
01204 
01205         // isset is REQUIRED here not to use getter
01206         if (isset($this->$sLongFieldName) && is_object($this->$sLongFieldName)) {
01207             $this->$sLongFieldName->setValue($sValue, $iDataType);
01208         } else {
01209             $this->$sLongFieldName = new oxField($sValue, $iDataType);
01210         }
01211 
01212     }
01213 
01222     protected function _getUpdateFieldValue($sFieldName, $oField)
01223     {
01224         $blPassNullValue = false;
01225         if ($oField instanceof oxField) {
01226             $value = $oField->getRawValue();
01227         } else {
01228             $value = $oField->value;
01229         }
01230         // R. check if this field value is null AND it can be null according to table description
01231         if (null === $value) {
01232             $aMetaData = $this->_getAllFields();
01233             foreach ($aMetaData as $oMetaInfo) {
01234                 if (!strcasecmp($oMetaInfo->name, $sFieldName)) {
01235                     $blPassNullValue = !$oMetaInfo->not_null;
01236                     break;
01237                 }
01238             }
01239         }
01240         if ($blPassNullValue) {
01241             return 'null';
01242         } else {
01243             return oxDb::getDb()->quote( $value );
01244         }
01245     }
01246 
01255     protected function _getUpdateFields( $blUseSkipSaveFields = true )
01256     {
01257         $sSql = '';
01258         $blSep  = false;
01259         foreach (array_keys($this->_aFieldNames) as $sKey) {
01260             $sLongName = $this->_getFieldLongName($sKey);
01261             $oField = $this->$sLongName;
01262 
01263 
01264             if ( !$blUseSkipSaveFields || ($blUseSkipSaveFields && !in_array(strtolower($sKey), $this->_aSkipSaveFields)) ) {
01265                 $sKey = $this->getSqlFieldName($sKey);
01266                 $sSql .= (( $blSep) ? ',':'' ).$sKey." = ".$this->_getUpdateFieldValue($sKey, $oField);
01267                 $blSep = true;
01268             }
01269         }
01270 
01271         return $sSql;
01272     }
01273 
01283     protected function _update()
01284     {
01285         //do not allow derived item update
01286         if ( !$this->allowDerivedUpdate() ) {
01287             return false;
01288         }
01289 
01290 
01291         if ( !$this->getId() ) {
01292             $oEx = oxNew( 'oxObjectException' );
01293             $oEx->setMessage( 'EXCEPTION_OBJECT_OXIDNOTSET' );
01294             $oEx->setObject($this);
01295             throw $oEx;
01296         }
01297 
01298         $sIDKey = oxUtils::getInstance()->getArrFldName( $this->_sCoreTable.".oxid");
01299         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01300 
01301         $sUpdate= "update {$this->_sCoreTable} set ".$this->_getUpdateFields()
01302                  ." where {$this->_sCoreTable}.oxid = '".$this->getId()."' ";
01303 
01304         //echo         $sUpdate."\n\n\n";
01305 
01306         //trigger event
01307         $this->beforeUpdate();
01308 
01309         $blRet = (bool) oxDB::getDb()->execute( $sUpdate);
01310         $this->_rebuildCache();
01311 
01312         return $blRet;
01313     }
01314 
01322     protected function _insert()
01323     {
01324 
01325         $oDB      = oxDb::getDb(true);
01326         $myConfig = $this->getConfig();
01327         $myUtils  = oxUtils::getInstance();
01328 
01329         // let's get a new ID
01330         if ( !$this->getId()) {
01331             $this->setId();
01332         }
01333 
01334         $sIDKey = $myUtils->getArrFldName( $this->_sCoreTable.".oxid");
01335         $this->$sIDKey = new oxField($this->getId(), oxField::T_RAW);
01336         $sInsert= "Insert into {$this->_sCoreTable} set ";
01337 
01338         //setting oxshopid
01339         $sShopField = $myUtils->getArrFldName($this->_sCoreTable.".oxshopid");
01340         if (isset($this->$sShopField) && !$this->$sShopField->value)
01341             $this->$sShopField = new oxField($myConfig->getShopId(), oxField::T_RAW);
01342         $sInsert .= $this->_getUpdateFields( false );
01343 
01344         $blRet = (bool) $oDB->execute( $sInsert);
01345 
01346         $this->_rebuildCache();
01347 
01348         return $blRet;
01349 
01350     }
01351 
01357     protected function _rebuildCache()
01358     {
01359         if ( !$this->_blIsNewCache) {
01360             oxUtils::getInstance()->rebuildCache();
01361             $this->_blIsNewCache = true;
01362         }
01363     }
01364 
01374     protected function _setRecordNumber( $sMaxField, $aWhere = null, $iMaxTryCnt = 5 )
01375     {
01376         // filtering
01377         $sWhere = "";
01378         if ( is_array( $aWhere ) && count( $aWhere ) > 0) {
01379             $sWhere = implode(" and ", $aWhere).' and ';
01380         }
01381 
01382         // SQL to set record number
01383         $sUpdate = "update {$this->getViewName()} as t1, (select (max($sMaxField)+1) as t2max from {$this->getViewName()} where $sWhere 1) as t2 set t1.$sMaxField=t2.t2max where t1.oxid = '".$this->getId()."'";
01384 
01385         // SQL to check record number dublicates
01386         //this should not happen normally but we prefer to take extra care in this case due to parallel script execution etc.
01387         $sMaxSelect = "select $sMaxField from ".$this->getViewName()." where oxid='".$this->getId()."' ";
01388         $sCheck = "select count(oxid) from ".$this->getViewName()." where $sMaxField = ($sMaxSelect) and $sWhere 1 ";
01389 
01390         do {
01391             if ( oxDb::getDb(true)->Execute( $sUpdate ) === false ) {
01392                 return false;
01393             }
01394 
01395             $iChkCnt = oxDb::getDb(true)->GetOne( $sCheck );
01396         } while ( ( $iChkCnt > 1 ) && $iMaxTryCnt-- );
01397 
01398         $sFieldName = $this->getViewName().'__'.$sMaxField;
01399         $this->$sFieldName = new oxField(oxDb::getDb(true)->GetOne( $sMaxSelect ), oxField::T_RAW);//int value
01400 
01401         return ( $iChkCnt == 1 );
01402     }
01403 
01410     protected function _isDisabledFieldCache()
01411     {
01412         $sClass = get_class($this);
01413         if (isset(self::$_blDisableFieldCaching[$sClass]) && self::$_blDisableFieldCaching[$sClass]) {
01414             return true;
01415         }
01416 
01417         return false;
01418     }
01419 
01425     public function isOx()
01426     {
01427         $sOxId = $this->getId();
01428         if ( $sOxId[0] == 'o' && $sOxId[1] == 'x' ) {
01429             return true;
01430         }
01431         return false;
01432     }
01433 
01439     public function isReadOnly()
01440     {
01441         return $this->_blReadOnly;
01442     }
01443 
01451     public function setReadOnly( $blReadOnly )
01452     {
01453         $this->_blReadOnly = $blReadOnly;
01454     }
01455 
01456 }