Fixes in foreignkey and selec_count webmodel method

This commit is contained in:
Antonio de la Rosa 2016-09-04 06:51:45 +02:00
parent faa3f8198b
commit 86547f8955
4 changed files with 94 additions and 64 deletions

View file

@ -58,4 +58,4 @@ function select_to_array_method_class($class, $arr_select=array(), $raw_query=0,
} }
?> ?>

View file

@ -40,7 +40,8 @@ class ForeignKeyField extends IntegerField{
$this->quot_open=''; $this->quot_open='';
$this->quot_close=''; $this->quot_close='';
$this->protected=0; $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);
} }

View file

@ -359,7 +359,7 @@ class ModelForm {
if($error==0 && $num_form>0) 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) if($pass_values)

View file

@ -711,6 +711,8 @@ class Webmodel {
$str_conditions=$this->conditions; $str_conditions=$this->conditions;
$args=$this->args; $args=$this->args;
$raw_query=0;
if(gettype($conditions)=='array') if(gettype($conditions)=='array')
{ {
@ -729,6 +731,7 @@ class Webmodel {
} }
else else
{ {
$raw_query=1;
$str_conditions=$conditions; $str_conditions=$conditions;
@ -749,26 +752,39 @@ class Webmodel {
//Check execution //Check execution
$z=0; if($raw_query==0)
$arr_conditions=explode('?', $str_conditions);
foreach($args as $key => $arg)
{ {
$quot='"';
if(preg_match('/".*"/', $str_conditions) || preg_match('/\'.*\'/', $str_conditions))
if(gettype($arg)==='integer' || gettype($arg)==='float')
{ {
$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=[]; $arr_order=[];
$yes_order=0;
foreach($order_by as $key => $order) foreach($order_by as $key => $order)
{ {
settype($order, 'integer'); settype($order, 'integer');
$arr_set_order[$order]='ASC'; $arr_set_order[$order]='ASC';
@ -801,12 +818,17 @@ class Webmodel {
{ {
$arr_order[]=$key.' '.$arr_set_order[$order]; $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 else
@ -1345,9 +1367,9 @@ class Webmodel {
* @param string $fields_for_count Array for fields used for simple counts based on foreignkeyfields. * @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(); $this->set_phango_connection();
if($field=='') if($field=='')
@ -1361,46 +1383,51 @@ class Webmodel {
$arr_where=array('1=1'); $arr_where=array('1=1');
$arr_check_count=array(); $arr_check_count=array();
foreach($fields_for_count as $key_component) if($raw_query==false)
{ {
if(isset($this->components[$key_component])) foreach($fields_for_count as $key_component)
{ {
$component=$this->components[$key_component]; if(isset($this->components[$key_component]))
{
if(get_class($component)=='ForeignKeyField')
{ $component=$this->components[$key_component];
$table_name=$component->related_model; if(get_class($component)=='ForeignKeyField')
{
if(isset($arr_check_count[$table_name]))
{ $table_name=$component->related_model;
$table_name.='_'.uniqid(); 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_model[]=$component->related_model.' as '.$table_name;
$arr_check_count[$table_name]=1;
$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; foreach($this->related_models as $model_name_related => $fields_related)
{
$arr_where[]=$this->name.'.`'.$this->idmodel.'`='.$model_name_related.'.`'.$fields_related[0].'`';
$arr_model[]=$model_name_related;
}
$arr_where[]=$this->name.'.`'.$this->idmodel.'`='.$model_name_related.'.`'.$fields_related[0].'`';
}
}
$where=implode(" and ", $arr_where); $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) if($this->reset_conditions==1)
{ {
@ -2213,7 +2240,7 @@ class Webmodel {
public function __call($name_method, $arguments) public function __call($name_method, $arguments)
{ {
if(!isset(Webmodel::$cache_extension[$name_method])) if(!isset(Webmodel::$cache_extension[$name_method]))
{ {