Re: new keyword combined with other things...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Dec 7, 2010 at 1:55 PM, Paul M Foster <paulf@xxxxxxxxxxxxxxxxx> wrote:
>
> $this->db->select('title')->from('mytable')->where('id', $id)->limit(10,
> 20);
>
> What kind of internal magic they use to make this work, I don't know. I
> haven't examined their internals.
>
> Paul
>
> --
> Paul M. Foster
>

I've never seen the code nor used CodeIgniter but it might be
something like this:

class DB extends Abstraction
{
  private $sqlString;
  private $hasWhere;

  public function select($fields)
  {
    if (is_array($fields)) $this->sqlString = 'select '.implode(',' $fields);
    else $this->sqlString = 'select '.$fields;
    return $this;
  }
  public function from($from)
  {
    $this->sqlString .= ' from '.$from;
    return $this;
  }
  public function where($field, $value, $isAnd = false)
  {
    if ($this->hasWhere)  $this->sqlString .= $isAnd ? ' and ' : ' or ';
    else
    {
      $this->hasWhere = true;
      $this->sqlString .= ' where ';
    }
    $this->sqlString .= $field.' = '.$this->escape($value);
    return $this;
  }
  public function limit($start, $rows)
  {
    $this->sqlString .= " limit {$start}, {$rows}";
    return $this;
  }
  // other methods
}

The only need I see for this is to build the query dynamically...
which should be very rare since all those function calls are
expensive, IMO.

Regards,
Tommy

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux