From 86547f8955ad6fd83af7db6bcef936aa2f7a45a6 Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Sun, 4 Sep 2016 06:51:45 +0200 Subject: [PATCH] Fixes in foreignkey and selec_count webmodel method --- extensions/select_to_array.php | 2 +- src/CoreFields/ForeignKeyField.php | 3 +- src/ModelForm.php | 6 +- src/Webmodel.php | 147 +++++++++++++++++------------ 4 files changed, 94 insertions(+), 64 deletions(-) diff --git a/extensions/select_to_array.php b/extensions/select_to_array.php index 6fa83e9..aac28a4 100644 --- a/extensions/select_to_array.php +++ b/extensions/select_to_array.php @@ -58,4 +58,4 @@ function select_to_array_method_class($class, $arr_select=array(), $raw_query=0, } -?> \ No newline at end of file +?> diff --git a/src/CoreFields/ForeignKeyField.php b/src/CoreFields/ForeignKeyField.php index 47cd3b7..ed5f4bb 100644 --- a/src/CoreFields/ForeignKeyField.php +++ b/src/CoreFields/ForeignKeyField.php @@ -40,7 +40,8 @@ class ForeignKeyField extends IntegerField{ $this->quot_open=''; $this->quot_close=''; $this->protected=0; - $this->parameters=array($this->related_model, $name_field); + //($name, $value, $model, $field_name, $field_value) + $this->parameters=array($this->related_model, $name_field, $name_field); } diff --git a/src/ModelForm.php b/src/ModelForm.php index e47721f..ab58758 100644 --- a/src/ModelForm.php +++ b/src/ModelForm.php @@ -359,7 +359,7 @@ class ModelForm { if($error==0 && $num_form>0) { - return [$post, $arr_form]; + return [$arr_form, $post]; } @@ -461,6 +461,8 @@ class ModelForm { } } + + return $model; } @@ -480,7 +482,7 @@ class ModelForm { } - static public function show_form($arr_form, $post, $pass_values=false) + static public function show_form($arr_form, $post, $pass_values=false, $check_values=false) { if($pass_values) diff --git a/src/Webmodel.php b/src/Webmodel.php index 7b3f931..76c75e8 100644 --- a/src/Webmodel.php +++ b/src/Webmodel.php @@ -711,6 +711,8 @@ class Webmodel { $str_conditions=$this->conditions; $args=$this->args; + + $raw_query=0; if(gettype($conditions)=='array') { @@ -729,6 +731,7 @@ class Webmodel { } else { + $raw_query=1; $str_conditions=$conditions; @@ -749,26 +752,39 @@ class Webmodel { //Check execution - $z=0; - - $arr_conditions=explode('?', $str_conditions); - - foreach($args as $key => $arg) + if($raw_query==0) { - $quot='"'; - - if(gettype($arg)==='integer' || gettype($arg)==='float') + + if(preg_match('/".*"/', $str_conditions) || preg_match('/\'.*\'/', $str_conditions)) { - - $quot=''; + + throw new \Exception('Sorry, i cannot load raw data betwen quotes'); } - $arr_conditions[$key]=trim($arr_conditions[$key].$quot.$this->escape_string($arg).$quot); + $z=0; + + $arr_conditions=explode('?', $str_conditions); + + foreach($args as $key => $arg) + { + $quot='"'; + + if(gettype($arg)==='integer' || gettype($arg)==='float') + { + + $quot=''; + + } + + $arr_conditions[$key]=trim($arr_conditions[$key].$quot.$this->escape_string($arg).$quot); + + } + + $this->conditions=trim(implode(' ', $arr_conditions)); + } - - $this->conditions=trim(implode(' ', $arr_conditions)); } @@ -787,10 +803,11 @@ class Webmodel { { $arr_order=[]; + $yes_order=0; foreach($order_by as $key => $order) { - + settype($order, 'integer'); $arr_set_order[$order]='ASC'; @@ -801,12 +818,17 @@ class Webmodel { { $arr_order[]=$key.' '.$arr_set_order[$order]; - + $yes_order++; + } } - $this->order_by='order by '.implode(',', $arr_order); + if($yes_order>0) + { + + $this->order_by='order by '.implode(',', $arr_order); + } } else @@ -1345,9 +1367,9 @@ class Webmodel { * @param string $fields_for_count Array for fields used for simple counts based on foreignkeyfields. */ - public function select_count($field='', $fields_for_count=array()) + public function select_count($field='', $fields_for_count=array(), $raw_query=true) { - + $this->set_phango_connection(); if($field=='') @@ -1361,46 +1383,51 @@ class Webmodel { $arr_where=array('1=1'); $arr_check_count=array(); - - foreach($fields_for_count as $key_component) - { - - if(isset($this->components[$key_component])) - { - - $component=$this->components[$key_component]; - - if(get_class($component)=='ForeignKeyField') - { - - $table_name=$component->related_model; - - if(isset($arr_check_count[$table_name])) - { - - $table_name.='_'.uniqid(); - - } - - $arr_model[]=$component->related_model.' as '.$table_name; - - $arr_where[]=$this->name.'.`'.$key_component.'`='.$table_name.'.`'.Webmodel::$model[$component->related_model]->idmodel.'`'; - - $arr_check_count[$table_name]=1; - - } - - } - } - - foreach($this->related_models as $model_name_related => $fields_related) - { - - $arr_model[]=$model_name_related; - - $arr_where[]=$this->name.'.`'.$this->idmodel.'`='.$model_name_related.'.`'.$fields_related[0].'`'; - - } + + if($raw_query==false) + { + + foreach($fields_for_count as $key_component) + { + + if(isset($this->components[$key_component])) + { + + $component=$this->components[$key_component]; + + if(get_class($component)=='ForeignKeyField') + { + + $table_name=$component->related_model; + + if(isset($arr_check_count[$table_name])) + { + + $table_name.='_'.uniqid(); + + } + + $arr_model[]=$component->related_model.' as '.$table_name; + + $arr_where[]=$this->name.'.`'.$key_component.'`='.$table_name.'.`'.Webmodel::$model[$component->related_model]->idmodel.'`'; + + $arr_check_count[$table_name]=1; + + } + + } + } + + foreach($this->related_models as $model_name_related => $fields_related) + { + + $arr_model[]=$model_name_related; + + $arr_where[]=$this->name.'.`'.$this->idmodel.'`='.$model_name_related.'.`'.$fields_related[0].'`'; + + } + + } $where=implode(" and ", $arr_where); @@ -1411,7 +1438,7 @@ class Webmodel { } - $conditions=trim($this->conditions.$where.' '.$this->order_by.' '.$this->limit); + $conditions=trim($this->conditions.$where); if($this->reset_conditions==1) { @@ -2213,7 +2240,7 @@ class Webmodel { public function __call($name_method, $arguments) { - + if(!isset(Webmodel::$cache_extension[$name_method])) {