Welcome Guest, Not a member yet? Register   Sign In
Active Record Class
#1

[eluser]Unknown[/eluser]
propose to make a few useful methods. this will give the opportunity to make requests of the form:

Code:
SELECT * FROM (`fr_users`) WHERE ( `id1` = 1 AND `id3` = 1 ) OR ( `id2` = 1 AND `id4` = 1 )

Code:
var $ar_where_exp  = array();
var $ar_where_exp_type  = array();

Code:
public function where_exp($key, $value = NULL, $escape = TRUE, $exp = '', $exp_type = 'AND ')
{
return $this->_where_exp($key, $value, 'AND ', $escape, $exp, $exp_type);
}

Code:
public function or_where_exp($key, $value = NULL, $escape = TRUE, $exp = '', $exp_type = 'AND ')
{
return $this->_where_exp($key, $value, 'OR ', $escape, $exp, $exp_type);
}

Code:
protected function _where_exp($key, $value = NULL, $type = 'AND ', $escape = NULL, $exp, $exp_type = 'AND ')
{
  if ( ! is_array($key))
  {
   $key = array($key => $value);
  }

  // If the escape value was not set will will base it on the global setting
  if ( ! is_bool($escape))
  {
   $escape = $this->_protect_identifiers;
  }

  foreach ($key as $k => $v)
  {
   $prefix = (!isset($this->ar_where_exp[$exp])) ? '' : $type;

   if (!isset($this->ar_where_exp_type[$exp])) $this->ar_where_exp_type[$exp] = $exp_type;

   if (is_null($v) && ! $this->_has_operator($k))
   {
    // value appears not to have been set, assign the test to IS NULL
    $k .= ' IS NULL';
   }

   if ( ! is_null($v))
   {
    if ($escape === TRUE)
    {
     $k = $this->_protect_identifiers($k, FALSE, $escape);

     $v = ' '.$this->escape($v);
    }
    
    if ( ! $this->_has_operator($k))
    {
     $k .= ' = ';
    }
   }
   else
   {
    $k = $this->_protect_identifiers($k, FALSE, $escape);
   }

   $this->ar_where_exp[$exp][] = $prefix.$k.$v;

  }

  return $this;
}


in function _compile_select

Code:
// Write the "WHERE EXP" portion of the query

if (count($this->ar_where_exp) > 0 and count($this->ar_where) == 0)
{
$sql .= "\nWHERE ";
}

$t = false;

foreach ($this->ar_where_exp as $k => $v) {
  if ($t or count($this->ar_where) > 0 OR count($this->ar_like) > 0)
  {
$sql .= $this->ar_where_exp_type[$k];
  } else {
$t = true;
  }
  $sql .= '( '.implode("\n", $this->ar_where_exp[$k]).' ) ';
}
#2

[eluser]Unknown[/eluser]
example1:

Code:
$this->db->where_exp('advert.obj_type =', 3, true, '1');
$this->db->or_where_exp('advert.obj_type IS NULL', null, true, '1');

Code:
WHERE ( `fr_advert`.`obj_type` = 3 OR `fr_advert`.`obj_type` IS NULL )

example2:

Code:
$this->db->where_exp('advert.obj_type =', 3, true, '1');
$this->db->or_where_exp('advert.obj_type IS NULL', null, true, '1');
$this->db->where_exp('advert.balans >', 0, true, '2');
$this->db->or_where_exp('advert.balans IS NULL', null, true, '2');

Code:
( `fr_advert`.`obj_type` = 3 OR `fr_advert`.`obj_type` IS NULL ) AND ( `fr_advert`.`balans` > 0 OR `fr_advert`.`balans` IS NULL )
#3

[eluser]InsiteFX[/eluser]
In the future please wrap your code in code tags or no one will even bother to read it.




Theme © iAndrew 2016 - Forum software by © MyBB