Exemplo n.º 1
0
function toSql(SelectQuery $obj)
{
    $_string = $obj->__toString();
    $_conditions = $obj->conditions();
    $_tables = $obj->getTables();
    $_fields = $obj->getFields();
    foreach ($_tables as $k => $t) {
        if (!empty($t['alias'])) {
            $_string = str_replace('{' . $t['table'] . '}', $t['table'] . ' as', $_string);
        } else {
            $_string = str_replace('{' . $t['table'] . '}', $t['table'], $_string);
        }
    }
    foreach ($_conditions as $k => $c) {
        if (is_int($c['value'])) {
            $_string = str_replace(':db_condition_placeholder_' . $k, $c['value'], $_string);
        } else {
            $_string = str_replace(':db_condition_placeholder_' . $k, "'" . $c['value'] . "'", $_string);
        }
    }
    return $_string;
}
Exemplo n.º 2
0
 /**
  * Loads a Model using SQL.
  * 
  * All field values will be loaded from DB.
  * @param string $where WHERE-part of the SQL statement.
  * @param array $arguments Arguments used in $where
  * @return boolean true if dataset was found, else false
  */
 public function Load($where, $arguments = false)
 {
     $q = new SelectQuery($this, $this->_ds);
     $sql = $q->__toString() . " WHERE " . $where;
     if ($arguments !== false && !is_array($arguments)) {
         $arguments = array($arguments);
     }
     $q = $this->_ds->ExecuteSql($sql, $arguments);
     if ($q->rowCount() > 0) {
         foreach ($this->GetColumnNames() as $col) {
             $this->{$col} = $q[$col];
         }
         $this->_query = false;
         $this->_results = false;
         $this->_index = 0;
         $this->__init_db_values();
         return true;
     } else {
         $this->__init_db_values(true);
     }
     return false;
 }