Example #1
0
 public function getFromArray($rData, $source = 'cache')
 {
     $r = new self();
     foreach ($rData as $col => $val) {
         if ($val != '') {
             $r->{$col} = $val;
         }
     }
     $r->created = $r->created * 1000;
     if ($r->uID > 0) {
         $u = User::getByUserID($r->uID);
         $r->username = $u->getUsername();
     }
     switch ($r->approval) {
         case 1:
             $r->status = 'approved';
             break;
         case 1:
             $r->status = 'rejected';
             break;
         default:
             $r->status = 'pending';
     }
     $json = Loader::helper('json');
     if ($source == 'db') {
         $r->cache();
     } else {
         $r->answers = $json->decode($r->cache);
     }
     return $r;
 }
Example #2
0
File: Orm.php Project: schpill/thin
 public function __call($method, $args)
 {
     if (empty($this->_datas['foreignFields'])) {
         $this->_datas['foreignFields'] = array();
     }
     if (!Arrays::is($this->_datas['foreignFields'])) {
         $this->_datas['foreignFields'] = array($this->_datas['foreignFields']);
     }
     if (substr($method, 0, 3) == 'get') {
         $vars = array_values($this->fields());
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if (Arrays::in($var, $vars) || ake($var, $this->_datas['foreignFields']) || true === $this->hasForeignRelation($var)) {
             if (ake($var, $this->_datas['foreignFields']) || true === $this->hasForeignRelation($var)) {
                 if (ake($var, $this->_datas['foreignFields']) && 'true' != $this->_datas['foreignFields'][$var]) {
                     return $this->_datas['foreignFields'][$var];
                 }
                 $var = true === $this->hasForeignRelation($var) ? $this->fkFieldName($var) : $var;
                 $rs = $this->_datas['configModel']['relationship'][$var];
                 $field = $rs['fieldName'];
                 $classModel = $this->_datas['classModel'];
                 $obj = new $classModel();
                 if (Arrays::inArray($field, $this->_datas['keys'])) {
                     $modelField = $rs['foreignTable'];
                     if (isset($this->_datas['configModel']['relationship']) && ake($field, $this->_datas['configModel']['relationship'])) {
                         $m = $this->_datas['configModel']['relationship'][$field];
                         if (ake("entity", $rs)) {
                             $entity = $rs['entity'];
                         } else {
                             $entity = $obj->_entity;
                         }
                         if (null !== $m['type']) {
                             switch ($m['type']) {
                                 case 'manyToOne':
                                 case 'oneToOne':
                                     $nObj = new self($entity, $modelField);
                                     if (ake("relationshipKeys", $rs)) {
                                         $field = $rs['relationshipKeys'];
                                     }
                                     if (!is_null($this->{$field})) {
                                         if (false === $this->_cache) {
                                             $result = $nObj->find($this->{$field});
                                         } else {
                                             $result = $nObj->cache($this->_cache)->find($this->{$field});
                                             $this->_cache = false;
                                         }
                                         $this->_datas['foreignFields'][$var] = $result;
                                         return $result;
                                     }
                                     break;
                                 case 'manyToMany':
                                 case 'oneToMany':
                                     $nObj = new self($entity, $modelField);
                                     $getter = $this->pk();
                                     $fk = ake("relationshipKeys", $rs) ? $rs['relationshipKeys'][$field] : $rs['foreignKey'];
                                     if (false === $this->_cache) {
                                         $result = $nObj->where($nObj->_dbName . '.' . $nObj->_tableName . "." . $fk . " = " . $nObj->quote($this->{$getter}))->select();
                                     } else {
                                         $result = $nObj->cache($this->_cache)->where($nObj->_dbName . '.' . $nObj->_tableName . "." . $fk . " = " . $nObj->quote($this->{$getter}))->select();
                                         $this->_cache = false;
                                     }
                                     $this->_datas['foreignFields'][$var] = $result;
                                     return $result;
                                     break;
                             }
                         }
                     }
                 }
             }
             if (isset($this->{$var}) || is_null($this->{$var})) {
                 return $this->{$var};
             } else {
                 throw new Exception("Unknown field {$var} in " . get_class($this) . " class.");
             }
         }
         return null;
     } elseif (substr($method, 0, 3) == 'set') {
         $vars = array_values($this->fields());
         $value = $args[0];
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if (Arrays::in($var, $vars) || ake($var, $this->_datas['foreignFields']) || true === $this->hasForeignRelation($var)) {
             if (ake($var, $this->_datas['foreignFields']) || true === $this->hasForeignRelation($var)) {
                 $var = true === $this->hasForeignRelation($var) ? $this->fkFieldName($var) : $var;
                 $this->_datas['foreignFields'][$var] = $value;
                 $rs = $this->_datas['configModel']['relationship'][$var];
                 $setField = $rs['fieldName'];
                 $getter = $rs['foreignKey'];
                 if (Arrays::in($setField, $vars) && isset($value->{$getter})) {
                     $this->{$setField} = $value->{$getter};
                     return $this;
                 } else {
                     return $this;
                 }
             } else {
                 $this->{$var} = $value;
                 return $this;
             }
         } else {
             throw new Exception("Unknown field {$var} in " . get_class($this) . " class.");
         }
     } elseif (substr($method, 0, 3) == 'min') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if ($var == 'id') {
             $var = $this->pk();
         }
         $vars = array_values($this->fields());
         if (Arrays::in($var, $vars)) {
             return $this->min($var);
         }
     } elseif (substr($method, 0, 3) == 'max') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if ($var == 'id') {
             $var = $this->pk();
         }
         $vars = array_values($this->fields());
         if (Arrays::in($var, $vars)) {
             return $this->max($var);
         }
     } elseif (substr($method, 0, 3) == 'avg') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if ($var == 'id') {
             $var = $this->pk();
         }
         $vars = array_values($this->fields());
         if (Arrays::in($var, $vars)) {
             return $this->avg($var);
         }
     } elseif (substr($method, 0, 3) == 'sum') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 3)));
         $var = Inflector::lower($uncamelizeMethod);
         if ($var == 'id') {
             $var = $this->pk();
         }
         $vars = array_values($this->fields());
         if (Arrays::inArray($var, $vars)) {
             return $this->sum($var);
         }
     } elseif (substr($method, 0, 5) == 'count') {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 5)));
         $var = Inflector::lower($uncamelizeMethod);
         if ($var == 'id') {
             $var = $this->pk();
         }
         $vars = array_values($this->fields());
         if (Arrays::inArray($var, $vars)) {
             return $this->count($var);
         }
     } elseif (substr($method, 0, 6) == 'findBy') {
         $vars = array_values($this->fields());
         $value = $args[0];
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 6)));
         $var = Inflector::lower($uncamelizeMethod);
         $var = true === $this->hasForeignRelation($var) ? $this->fkFieldName($var) : $var;
         $rs = $this->_datas['configModel']['relationship'][$var];
         $this->_datas['foreignFields'][$var] = $value;
         if (is_object($value) && null !== $this->_datas['configModel']['relationship'][$var]) {
             switch ($this->_datas['configModel']['relationship'][$var]) {
                 case 'manyToOne':
                 case 'oneToOne':
                     $field = $rs['fieldName'];
                     $q = $this->_dbName . '.' . $this->_tableName . "." . $field . " = " . $this->quote($value->{$field});
                     if (false === $this->_cache) {
                         return $this->where($q)->select();
                     } else {
                         return $this->cache($this->_cache)->where($q)->select();
                         $this->_cache = false;
                     }
                 case 'oneToMany':
                 case 'manyToMany':
                     $field = $rs['fieldName'];
                     $pk = $this->pk();
                     $pkValue = $value->{$pk};
                     $q = $this->_dbName . '.' . $this->_tableName . "." . $pk . " = " . $this->quote($pkValue);
                     if (false === $this->_cache) {
                         return $this->where($q)->select();
                     } else {
                         return $this->cache($this->_cache)->where($q)->select();
                         $this->_cache = false;
                     }
             }
         } else {
             if (Arrays::in($var, $vars) || $var == 'id') {
                 if ($var != 'id') {
                     return $this->findBy($var, $value);
                 } else {
                     return $this->find($value);
                 }
             } else {
                 throw new Exception("Unknown field {$var} in " . get_class($this) . " class.");
             }
         }
     } elseif (substr($method, 0, 5) == 'where' && strlen($method) > 5) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 5)));
         $var = Inflector::lower($uncamelizeMethod);
         if ($var == 'id') {
             $var = $this->pk();
         }
         $var = $this->_dbName . '.' . $this->_tableName . '.' . $var;
         $condition = $args[0];
         $operator = isset($args[1]) ? $args[1] : 'AND';
         return $this->where("{$var} {$condition}", $operator);
     } elseif (substr($method, 0, 7) == 'groupBy' && strlen($method) > 7) {
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 7)));
         $var = Inflector::lower($uncamelizeMethod);
         if ($var == 'id') {
             $var = $this->pk();
         }
         return $this->groupBy($var);
     } elseif (substr($method, 0, 7) == 'orderBy') {
         $direction = count($args) ? $args[0] : 'ASC';
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 7)));
         $var = Inflector::lower($uncamelizeMethod);
         return $this->order($var, $direction);
     } elseif (substr($method, 0, 9) == 'findOneBy') {
         $vars = array_values($this->fields());
         $value = $args[0];
         $uncamelizeMethod = Inflector::uncamelize(lcfirst(substr($method, 9)));
         $var = Inflector::lower($uncamelizeMethod);
         if (Arrays::inArray($var, $vars) || $var == 'id') {
             if ($var != 'id') {
                 return $this->findBy($var, $value, true);
             } else {
                 return $this->find($value);
             }
         } else {
             throw new Exception("Unknown field {$var} in " . get_class($this) . " class.");
         }
     } else {
         $vars = array_values($this->fields());
         $uncamelizeMethod = Inflector::uncamelize(lcfirst($method));
         $var = Inflector::lower($uncamelizeMethod);
         $var = true === $this->hasForeignRelation($var) ? $this->fkFieldName($var) : $var;
         $rs = $this->_datas['configModel']['relationship'][$var];
         $this->_datas['foreignFields'][$var] = $value;
         if (Arrays::in($var, $vars) || ake($var, $this->_datas['foreignFields']) || true === $this->hasForeignRelation($var)) {
             if (ake($var, $this->_datas['foreignFields'])) {
                 return $this->_datas['foreignFields'][$var];
             }
             if (isset($this->{$var})) {
                 return $this->{$var};
             } else {
                 if (!method_exists($this, $method)) {
                     $this->{$method} = $args[0];
                 }
             }
         }
     }
 }