Example #1
0
 function execute($additionalQuery = array())
 {
     if (is_array($additionalQuery)) {
         $query = new Query($this->__table__);
         $query->select = $this->__table__ . ".*";
         $query->order = $this->__table__ . ".id";
         if ($this->perPage) {
             $query->countFounds(TRUE);
             $query->limit = $this->page != null ? $this->page * $this->perPage . "," . $this->perPage : $this->perPage;
             //$query->limit = $this->perPage;
         }
         if (is_array($additionalQuery)) {
             foreach ($additionalQuery as $key => $value) {
                 $property = strtolower($key);
                 if ($property != "join") {
                     $query->{$property} = $value;
                 }
             }
         }
         if (count($this->_forceValues) > 0) {
             if ($query->where != "") {
                 $query->where .= " AND ";
             }
             $fValues = array();
             foreach ($this->_forceValues as $key => $value) {
                 $fValues[] = "{$this->__table__}.{$key} = '{$value}'";
             }
             $query->where .= implode(" AND ", $fValues);
         }
         $sameNameCounter = 1;
         // BELONGS TO
         foreach ($this->__belongs_to__ as $key => $value) {
             //$className = (is_numeric($key)) ? $value : $key;
             $options = is_numeric($key) ? array() : Options::parse($value);
             $className = is_numeric($key) ? $value : $key;
             $type = array_key_exists("type", $options) ? strToUpper($options["type"]) : "LEFT";
             //(is_numeric($key)) ? $options["connector"] : $key;
             $tableas = array_key_exists("as", $options) ? $options["as"] : false;
             $field = array_key_exists("field", $options) ? $options["field"] : strToLower($className);
             $from = array_key_exists("from", $options) ? $options["from"] : $this->__table__;
             $resolver = "";
             $as = "";
             if (class_exists($className)) {
                 $temp_model = new $className();
                 $temp_table_name = strToLower($temp_model->__table__);
                 //."s";
                 unset($temp_model);
                 //$temp_table_name = strToLower($className)."s";
                 if (strToLower($className) == strToLower($this->__class__)) {
                     $resolver = $sameNameCounter;
                     $as = " AS " . $temp_table_name . $resolver;
                     $sameNameCounter++;
                 }
                 if ($tableas) {
                     $query->select .= ", " . $tableas . ".*";
                     $query->join[] = array($type, $temp_table_name . " AS " . $tableas, $tableas . ".id = " . $from . ".id_" . $field);
                 } else {
                     $query->select .= ", " . $temp_table_name . $resolver . ".*";
                     $query->join[] = array($type, $temp_table_name . $as, $temp_table_name . $resolver . ".id = " . $from . ".id_" . $field);
                 }
                 $obj_name = strToLower($className);
             } else {
                 trigger_error("Can't load Class [{$className}]", E_USER_WARNING);
             }
         }
         // HAS ONE
         foreach ($this->__has_one__ as $key => $value) {
             //$className = (is_numeric($key)) ? $value : $key;
             $options = is_numeric($key) ? array() : Options::parse($value);
             $className = is_numeric($key) ? $value : $key;
             $type = array_key_exists("type", $options) ? strToUpper($options["type"]) : "LEFT";
             //(is_numeric($key)) ? $options["connector"] : $key;
             $resolver = "";
             $as = "";
             $temp_model = new $className();
             $temp_table_name = strToLower($temp_model->__table__);
             //."s";
             $where = array_key_exists("where", $options) ? " AND " . $temp_table_name . "." . $options["where"] : "";
             unset($temp_model);
             //$temp_table_name = strToLower($className)."s";
             if (strToLower($className) == strToLower($this->__class__)) {
                 $resolver = $sameNameCounter;
                 $as = " AS " . $temp_table_name . $resolver;
                 $sameNameCounter++;
             }
             $query->select .= ", " . $temp_table_name . $resolver . ".*";
             $query->join[] = array($type, $temp_table_name . $as, $temp_table_name . $resolver . ".id_" . strToLower($this->__class__) . " = " . $this->__table__ . ".id" . $where);
             $obj_name = strToLower($className);
         }
         // HAS MANY
         foreach ($this->__has_many__ as $key => $value) {
             $options = is_numeric($key) ? array() : Options::parse($value);
             $className = is_numeric($key) ? $value : $key;
             $type = array_key_exists("type", $options) ? strToUpper($options["type"]) : "LEFT";
             $link = array_key_exists("link", $options) ? $options["link"] : FALSE;
             $selectLink = array_key_exists("select", $options) ? $options["select"] == "true" : FALSE;
             if (!$link) {
                 $temp_model = new $className();
                 $temp_table_name = strToLower($temp_model->__table__);
                 //."s";
                 unset($temp_model);
                 $query->select .= ", " . $temp_table_name . ".*";
                 $query->join[] = array($type, $temp_table_name, $temp_table_name . ".id_" . strToLower($this->__class__) . "=" . $this->__table__ . ".id");
             } else {
                 $temp_model = new $className();
                 $temp_model_table_name = strToLower($temp_model->__table__);
                 unset($temp_model);
                 $temp_table_name = strToLower($link);
                 $query->select .= ", " . $temp_model_table_name . ".*";
                 if ($selectLink) {
                     $query->select .= ", " . $link . ".*";
                 }
                 $query->join[] = array($type, $temp_table_name, $temp_table_name . ".id_" . strToLower($this->__class__) . "=" . $this->__table__ . ".id");
                 $query->join[] = array($type, $temp_model_table_name, $temp_model_table_name . ".id = " . $temp_table_name . ".id_" . strToLower($className));
             }
         }
         if ($this->_order_by) {
             $query->order .= ", " . $this->_order_by;
         }
         if (is_array($additionalQuery)) {
             if (array_key_exists("join", $additionalQuery)) {
                 if (array_key_exists("join", $query)) {
                     $query->join = array_merge($query->join, $additionalQuery["join"]);
                 } else {
                     $query->join = $additionalQuery["join"];
                 }
             }
         }
     } else {
         if (is_string($additionalQuery)) {
             $query = $additionalQuery;
         }
     }
     //new dBug($query);
     // DATABASE
     $db = new DataBase();
     $db->connect();
     $res = $db->query($query);
     return $res;
     /*if($res){
     			$res->moveCursor(0);
     			$this->buildFromResultset($res);
     			$sameNameCounter = 1;
     			foreach($this->_belongs_to as $key=>$value){
     				$className = (is_numeric($key)) ? $value : $key;
     				$obj_name = strToLower($className);
     				$resolver = "";
     				if($obj_name==get_class($this)){
     					$resolver = $sameNameCounter;
     					$sameNameCounter++;
     				}
     				$this->$obj_name = new $className();
     				$this->$obj_name->buildFromResultset($res,$resolver);
     			}					
     			$res->beforeFirst();
     			$old_ids = array();
     			foreach($this->_has_many as $key=>$value){
     				$property_name = (is_numeric($key)) ? strToLower($value."s") : strToLower($key."s");
     				$this->$property_name = array();
     				$old_ids[$property_name] = array();
     			}
     			while($res->next()){
     				foreach($this->_has_many as $key=>$value){
     					$property_name = (is_numeric($key)) ? strToLower($value."s") : strToLower($key."s");
     					$class = (is_numeric($key)) ? $value : $key;
     					$temp_obj = new  $class();
     					$id = $res->getData($property_name.".id");
     					if(array_search($id,$old_ids[$property_name])===FALSE || $id==NULL){
     						$created = $temp_obj->buildFromResultset($res);
     						if($created){
     							array_push($this->$property_name, $temp_obj);
     							array_push($old_ids[$property_name],$id);
     						}
     					}
     				}
     			}
     		}
     		return ($res!=false);*/
 }