Fixes for the new behaviour and set forms methods

This commit is contained in:
Antonio de la Rosa 2015-08-22 05:48:21 +02:00
parent f75ac6d78d
commit 9f35b00fed
7 changed files with 150 additions and 15 deletions

View file

@ -30,7 +30,9 @@ function element_exists_method_class($class, $idrow, $field_search='')
settype($idrow, 'integer'); settype($idrow, 'integer');
$num_elements=$class->select_count('where '.$field_search.'=\''.$idrow.'\'', $class->idmodel); $class->set_conditions('where '.$field_search.'=\''.$idrow.'\'');
$num_elements=$class->select_count($class->idmodel);
return $num_elements; return $num_elements;

View file

@ -19,12 +19,12 @@
* @param string $field The field where search. * @param string $field The field where search.
*/ */
function select_a_field_method_class($class, $where, $field) function select_a_field_method_class($class, $field)
{ {
$arr_field=array(); $arr_field=array();
$query=$class->select($where, array($field), $raw_query=1); $query=$class->select(array($field), $raw_query=1);
while(list($field_choose)=$class->fetch_row($query)) while(list($field_choose)=$class->fetch_row($query))
{ {

View file

@ -27,7 +27,11 @@ function select_a_row_method_class($class, $idrow, $arr_select=array(), $raw_que
settype($idrow, 'integer'); settype($idrow, 'integer');
$query=$class->select('where '.$class->name.'.`'.$class->idmodel.'`=\''.$idrow.'\'', $arr_select, $raw_query); $class->set_conditions('where '.$class->name.'.`'.$class->idmodel.'`=\''.$idrow.'\'');
$class->set_limit('limit 1');
$query=$class->select($arr_select, $raw_query);
return $class->fetch_array($query, $assoc); return $class->fetch_array($query, $assoc);

View file

@ -23,12 +23,14 @@
*/ */
function select_a_row_where_method_class($class, $where, $arr_select=array(), $raw_query=0, $assoc=0) function select_a_row_where_method_class($class, $arr_select=array(), $raw_query=0, $assoc=0)
{ {
settype($idrow, 'integer'); settype($idrow, 'integer');
$query=$class->select($where, $arr_select, $raw_query); $query=$class->select($arr_select, $raw_query);
$class->set_limit('limit 1');
return $class->fetch_array($query, $assoc); return $class->fetch_array($query, $assoc);

View file

@ -22,7 +22,7 @@
* *
*/ */
function select_to_array_method_class($class, $where="", $arr_select=array(), $raw_query=0, $index_id='') function select_to_array_method_class($class, $arr_select=array(), $raw_query=0, $index_id='')
{ {
$arr_return=array(); $arr_return=array();
@ -43,7 +43,7 @@ function select_to_array_method_class($class, $where="", $arr_select=array(), $r
} }
$query=$class->select($where, $arr_select, $raw_query); $query=$class->select($arr_select, $raw_query);
while($arr_row=$class->fetch_array($query)) while($arr_row=$class->fetch_array($query))
{ {

View file

@ -221,7 +221,8 @@ class ModelForm {
$func_setvalue=$this->form.'Set'; $func_setvalue=$this->form.'Set';
$this->parameters[2]=$func_setvalue($this->parameters[2], $value, $form_type_set); //$this->parameters[2]=$func_setvalue($this->parameters[2], $value, $form_type_set);
$this->parameters[2]=call_user_func_array($func_setvalue, array($this->parameters[2], $value, $form_type_set));
} }

View file

@ -236,6 +236,34 @@ class Webmodel {
static public $model=array(); static public $model=array();
/**
* A string where is saved the conditions used for create queries
*
*/
protected $conditions='WHERE 1=1';
/**
* A string where is set the order of query
*
*/
protected $order_by='';
/**
* A string where is saved the limit of rows in query
*
*/
protected $limit='';
/**
* A string where is saved the limit of rows in query
*
*/
protected $reset_conditions=true;
/** /**
* Internal arrays for create new indexes in the tables * Internal arrays for create new indexes in the tables
* *
@ -494,6 +522,53 @@ class Webmodel {
return SQLClass::webtsys_query($sql_query, $this->db_selected); return SQLClass::webtsys_query($sql_query, $this->db_selected);
} }
public function set_conditions($conditions, $order_by='', $limit='')
{
$conditions=trim($conditions);
if($conditions=='')
{
$this->conditions="WHERE 1=1";
}
else
{
$this->conditions=$conditions;
}
$this->order_by=$order_by;
}
public function set_order($order_by)
{
$this->order_by=$order_by;
}
public function set_limit($limit)
{
$this->limit=$limit;
}
public function reset_conditions()
{
$this->conditions='WHERE 1=1';
$this->order_by='order by '.$this->idmodel.' ASC';
$this->limit='';
}
/** /**
* This method insert a row in database using model how mirage of table. * This method insert a row in database using model how mirage of table.
* *
@ -563,11 +638,20 @@ class Webmodel {
* @param $conditions is a string containing a sql string beginning by "where". Example: where id=1. * @param $conditions is a string containing a sql string beginning by "where". Example: where id=1.
*/ */
public function update($post, $conditions="") public function update($post)
{ {
$this->set_phango_connection(); $this->set_phango_connection();
$conditions=trim($this->conditions.' '.$this->order_by.' '.$this->limit);
if($this->reset_conditions()==true)
{
$this->reset_conditions();
}
//Make conversion from post //Make conversion from post
//Check if minimal fields are fill and if fields exists in components. //Check if minimal fields are fill and if fields exists in components.
@ -683,7 +767,7 @@ class Webmodel {
* @param $raw_query If set to 0, you obtain fields from table related if you selected a foreignkey field, if set to 1, you obtain an array without any join. * @param $raw_query If set to 0, you obtain fields from table related if you selected a foreignkey field, if set to 1, you obtain an array without any join.
*/ */
public function select($conditions="", $arr_select=array(), $raw_query=0) public function select($arr_select=array(), $raw_query=0)
{ {
//Check conditions.., script must check, i can't make all things!, i am not a machine! //Check conditions.., script must check, i can't make all things!, i am not a machine!
@ -801,7 +885,7 @@ class Webmodel {
//$conditions is a variable where store the result from $arr_select and $arr_extra_select //$conditions is a variable where store the result from $arr_select and $arr_extra_select
if(preg_match('/^where/', $conditions) || preg_match('/^WHERE/', $conditions)) /*if(preg_match('/^where/', $conditions) || preg_match('/^WHERE/', $conditions))
{ {
$conditions=str_replace('where', '', $conditions); $conditions=str_replace('where', '', $conditions);
@ -815,6 +899,22 @@ class Webmodel {
$conditions='WHERE '.$where.' '.$conditions; $conditions='WHERE '.$where.' '.$conditions;
}*/
if($where!='')
{
$where=' and '.$where;
}
$conditions=trim($this->conditions.$where.' '.$this->order_by.' '.$this->limit);
if($this->reset_conditions()==true)
{
$this->reset_conditions();
} }
//$this->create_extra_fields(); //$this->create_extra_fields();
@ -856,7 +956,7 @@ 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($conditions, $field='', $fields_for_count=array()) public function select_count($field='', $fields_for_count=array())
{ {
$this->set_phango_connection(); $this->set_phango_connection();
@ -915,6 +1015,23 @@ class Webmodel {
$where=implode(" and ", $arr_where); $where=implode(" and ", $arr_where);
if($where!='')
{
$where=' and '.$where;
}
$conditions=trim($this->conditions.$where.' '.$this->order_by.' '.$this->limit);
if($this->reset_conditions()==true)
{
$this->reset_conditions();
}
/*
if(preg_match('/^where/', $conditions) || preg_match('/^WHERE/', $conditions)) if(preg_match('/^where/', $conditions) || preg_match('/^WHERE/', $conditions))
{ {
@ -929,7 +1046,7 @@ class Webmodel {
$conditions='WHERE '.$where.' '.$conditions; $conditions='WHERE '.$where.' '.$conditions;
} }*/
$query=SQLClass::webtsys_query('select count('.$this->name.'.`'.$field.'`) from '.implode(', ', $arr_model).' '.$conditions, $this->db_selected); $query=SQLClass::webtsys_query('select count('.$this->name.'.`'.$field.'`) from '.implode(', ', $arr_model).' '.$conditions, $this->db_selected);
@ -947,11 +1064,20 @@ class Webmodel {
* @param string $conditions Conditions have same sintax that $conditions from $this->select method * @param string $conditions Conditions have same sintax that $conditions from $this->select method
*/ */
public function delete($conditions="") public function delete()
{ {
$this->set_phango_connection(); $this->set_phango_connection();
$conditions=trim($this->conditions.' '.$this->order_by.' '.$this->limit);
if($this->reset_conditions()==true)
{
$this->reset_conditions();
}
foreach($this->components as $name_field => $component) foreach($this->components as $name_field => $component)
{ {