00001 <?php
00026 include_once(ZIGROOT.DRS.MOD_KEY_NAME_EXPLICIT.DRS."orm".DRS."core.php");
00030 class zOrm_base extends zOrm_core{
00036 function limit($l_query){
00037 $this->lex['limit'] = trim($l_query);
00038 }
00044 function group_by($g_query){
00045 $this->lex['group'] = trim($g_query);
00046 }
00052 function having($h_query){
00053 $this->lex['having'] = trim($h_query);
00054 }
00060 function order($colAlias, $way=ZIGORM_ORDER_ASC){
00061 if(!$realColName = $this->alias2Real($colAlias)){
00062 perror("<code>zigmoyd.orm.order()</code><br />Invalid Column Alias Name $colAlias");
00063 return false;
00064 }
00065 switch($way){
00066 case ZIGORM_ORDER_ASC:
00067 $drc = 'ASC';
00068 break;
00069 case ZIGORM_ORDER_DESC:
00070 $drc = 'DESC';
00071 break;
00072 default:
00073 perror("<code>zigmoyd.orm.order()</code><br />Invalid Order the second Argument must be one of these two predefined Constants ZIGORM_ORDER_ASC or ZIGORM_ORDER_DESC");
00074 return false;
00075 }
00076 $this->lex['order'][] = array('col' => $realColName, 'way' => $drc);
00077 }
00083 function distinct($flag = true){
00084 $distinctFlag = $flag ? "Distinct" : "UnDistinct";
00085 zLogger::debug("$distinctFlag Query", 'orm.table.'.$this->realTableName);
00086 $this->lex['distinct'] = $flag;
00087 }
00096 function insert($cleanUp=true){
00097 if(method_exists($this, 'onInsert')){
00098 $ret = array();
00099 foreach($this->lex['attributes'] as $realColName => $val){
00100 $ret[$this->real2Alias($realColName)] = $val;
00101 }
00102 $this->onInsert(&$ret);
00103 foreach($ret as $colAliasName => $val){
00104 $this->lex['attributes'][$this->alias2Real($colAliasName)] = $val;
00105 }
00106 $this->trgInsert = true;
00107 }
00108 foreach($this->lex['attributes'] as $realColName => $val){
00109 if(method_exists($this, 'onInsert'.$this->real2Alias($realColName))){
00110 call_user_func(array(&$this, 'onInsert'.$this->real2Alias($realColName)), &$val);
00111 $this->lex['attributes'][$realColName] = $val;
00112 $this->trgInsertCol[$this->real2Alias($realColName)] = true;
00113 }
00114 }
00115 if($this->isValidEntry()){
00116 zLogger::debug("Insertingh Rows successful on Table $this->realTableName (data validatied before insert)", 'orm.insert');
00117 $this->queryType(ZIGORM_Q_INSERT);
00118 return $this->exec($cleanUp);
00119 }else{
00120 zLogger::debug("Inserting Row(s) failed on Table $this->realTableName due to invalid data entry", 'orm.insert');
00121 return false;
00122 }
00123 }
00132 function remove($cleanUp=true){
00133 if(method_exists($this, 'onDelete')){
00134 $this->onDelete();
00135 }
00136 $this->queryType(ZIGORM_Q_REMOVE);
00137 zLogger::debug("Removing Row(s) on Table $this->realTableName", 'orm.remove');
00138 return $this->exec($cleanUp);
00139 }
00148 function update($cleanUp=true){
00149 if(method_exists($this, 'onUpdate')){
00150 $ret = array();
00151 foreach($this->lex['attributes'] as $realColName => $val){
00152 $ret[$this->real2Alias($realColName)] = $val;
00153 }
00154 $this->onUpdate(&$ret);
00155 foreach($ret as $colAliasName => $val){
00156 $this->lex['attributes'][$this->alias2Real($colAliasName)] = $val;
00157 }
00158 $this->trgUpdate = true;
00159 }
00160 foreach($this->lex['attributes'] as $realColName => $val){
00161 if(method_exists($this, 'onUpdate'.$this->real2Alias($realColName))){
00162 call_user_func(array(&$this, 'onUpdate'.$this->real2Alias($realColName)), &$val);
00163 $this->lex['attributes'][$realColName] = $val;
00164 $this->trgUpdateCol[$this->real2Alias($realColName)] = true;
00165 }
00166 }
00167 if($this->isValidUpdate()){
00168 zLogger::debug("Updating Row(s) successful on Table $this->realTableName (data validatied before update)", 'orm.update');
00169 $this->queryType(ZIGORM_Q_UPDATE);
00170 return $this->exec($cleanUp);
00171 }else{
00172 zLogger::debug("Updating Row(s) failed on Table $this->realTableName due to invalid data entry", 'orm.update');
00173 $this->initLex();
00174 return false;
00175 }
00176 }
00182 function delete($cleanUp=true){
00183 if(method_exists($this, 'onDelete')){
00184 $this->onDelete();
00185 }
00186 $this->queryType(ZIGORM_Q_REMOVE);
00187 zLogger::debug("Removing Row(s) on Table $this->realTableName", 'orm.remove');
00188 return $this->exec($cleanUp);
00189 }
00206 function isValidEntry(){
00207 load_module('validation');
00211 $data = array();
00212 $rulesDict = array();
00213 foreach($this->lex['attributes'] as $realColName => $val){
00214 $data[$this->real2alias($realColName)] = $val;
00215 $this->lex['attributes'][$realColName] = ($this->colQuote[$realColName]==ZIGORM_QUOTE) ? "'$val'" : $val;
00216 $rulesDict[$this->real2alias($realColName)] = array();
00217 if(isset($this->__struct->Attributes[$realColName]->criteria) && is_array($this->__struct->Attributes[$realColName]->criteria)){
00218 foreach($this->__struct->Attributes[$realColName]->criteria as $ruleName => $errMsg){
00219 $rulesDict[$this->real2alias($realColName)][$ruleName] = $errMsg;
00220 }
00221 }
00222 }
00223 $ormVld = new validation($data);
00224 foreach($rulesDict as $colAliasName => $ruleArray){
00225 $ormVld->addCriteria($colAliasName, $ruleArray);
00226 }
00227 $this->validation->isValid = $ormVld->isValid();
00228 foreach($data as $colAliasName => $dummyValue){
00229 if(isset($this->__struct->Attributes[$this->alias2real($colAliasName)]->criteria)){
00230 $this->validation->{$colAliasName} = new zRom_fields($dummyValue, $ormVld->errtext($colAliasName), $ormVld->isValid($colAliasName), $ormVld->validationStatus($colAliasName));
00231 if(!$this->validation->{$colAliasName}->isValid())$this->validation->errCols[$colAliasName] = $this->validation->{$colAliasName}->errMsg();
00232 }else{
00233 $this->validation->{$colAliasName} = new zRom_fields($data[$dummyValue], true, true, true);
00234 }
00235 }
00236 return $this->validation->isValid;
00237 }
00244 function isValidUpdate(){
00245 load_module('validation');
00246 $data = array();
00247 $rulesDict = array();
00248 foreach($this->lex['attributes'] as $realColName => $val){
00249 if(!(is_bool($val) && !$val)){
00250 $colAlias = $this->real2Alias($realColName);
00251 $data[$colAlias] = $val;
00252 if(isset($this->__struct->Attributes[$realColName]->criteria) && is_array($this->__struct->Attributes[$realColName]->criteria)){
00253 $rulesDict[$colAlias] = array();
00254 foreach($this->__struct->Attributes[$realColName]->criteria as $ruleName => $errMsg){
00255 $rulesDict[$colAlias][$ruleName] = $errMsg;
00256 }
00257 }
00258 $this->lex['attributes'][$realColName] = ($this->colQuote[$realColName]==ZIGORM_QUOTE) ? "'$val'" : $val;
00259 }
00260 }
00261 $ormVld = new validation($data);
00262 foreach($rulesDict as $colAliasName => $ruleArray){
00263 zLogger::udebug("Adding Rules for Column $colAliasName");
00264 $ormVld->addCriteria($colAliasName, $ruleArray);
00265 }
00266 $this->validation->isValid = $ormVld->isValid();
00267 $this->validation->errCols = array();
00268 foreach($data as $colAliasName => $dummyValue){
00269 if(isset($this->__struct->Attributes[$this->alias2real($colAliasName)]->criteria)){
00270 $this->validation->{$colAliasName} = new zRom_fields($dummyValue, $ormVld->errtext($colAliasName), $ormVld->isValid($colAliasName), $ormVld->validationStatus($colAliasName));
00271 if(!$this->validation->{$colAliasName}->isValid())$this->validation->errCols[$colAliasName] = $this->validation->{$colAliasName}->errMsg();
00272 }else{
00273 $this->validation->{$colAliasName} = new zRom_fields($data[$dummyValue], true, true, true);
00274 }
00275 }
00276 return $this->validation->isValid;
00277 }
00284 function index($colAlias){
00285 zLogger::debug("indexing export data with $colAlias Column", 'orm.export.index');
00286 $this->__indexKey = $colAlias;
00287 }
00296 function export($cleanUp=true){
00297 zLogger::debug("Exporting result set", 'orm.export');
00298 if(!$this->__rdbmsJoin){
00299 $this->queryType(ZIGORM_Q_SELECT);
00300 }else{
00301 zLogger::debug("issuing Join before exporting", 'orm.export');
00302 $this->join();
00303 $this->queryType(ZIGORM_Q_JOIN);
00304 }
00305 $this->exec(false);
00306 if(method_exists($this, 'onExport')){
00307 $this->onExport(&$this->rawExported);
00308 }
00309 $rowSetClassName = get_class($this).'RowSet';
00310 $ret = array();
00311 if(!is_array($this->rawExported)){
00312 $this->__indexKey = null;
00313 $this->initLex();
00314 return false;
00315 }
00316 foreach($this->rawExported as $rowId => $rowObj){
00317 if(strlen(trim($this->__indexKey))){
00318 if(!isset($rowObj->colList[$this->__indexKey])){
00319 perror("<code>zigmoyd.orm.export.index</code><br />Invalid Index $this->__indexKey<br />Its worth to mention that you also need to fetch the Index column");
00320 return false;
00321 }
00322 $indx = $rowObj->colList[$this->__indexKey];
00323 }else{
00324 $indx = $rowId;
00325 }
00326 $ret[$indx] = new $rowSetClassName;
00327 foreach($rowObj->colList as $colAlias => $val){
00328 if(method_exists($this, 'onExport'.$colAlias)){
00329 call_user_func(array(&$this, 'onExport'.$colAlias), &$rowObj->colList[$colAlias]);
00330 }
00331 }
00332 $ret[$indx]->colList = $rowObj->colList;
00333
00334 unset($rowObj);
00335 }
00336 if($cleanUp){
00337 $this->__indexKey = null;
00338 $this->initLex();
00339 }
00340 return $ret;
00341 }
00342 }
00344 ?>