ORM::_execute PHP Method

_execute() protected static method

Internal helper method for executing statments. Logs queries, and stores statement object in ::_last_statment, accessible publicly through ::get_last_statement()
protected static _execute ( string $query, array $parameters = [], string $connection_name = self::DEFAULT_CONNECTION ) : boolean
$query string
$parameters array An array of parameters to be bound in to the query
$connection_name string Which connection to use
return boolean Response of PDOStatement::execute()
    protected static function _execute($query, $parameters = array(), $connection_name = self::DEFAULT_CONNECTION)
    {
        $statement = self::get_db($connection_name)->prepare($query);
        self::$_last_statement = $statement;
        $time = microtime(true);
        foreach ($parameters as $key => &$param) {
            if (is_null($param)) {
                $type = PDO::PARAM_NULL;
            } else {
                if (is_bool($param)) {
                    $type = PDO::PARAM_BOOL;
                } else {
                    if (is_int($param)) {
                        $type = PDO::PARAM_INT;
                    } else {
                        $type = PDO::PARAM_STR;
                    }
                }
            }
            $statement->bindParam(is_int($key) ? ++$key : $key, $param, $type);
        }
        $q = $statement->execute();
        self::_log_query($query, $parameters, $connection_name, microtime(true) - $time);
        return $q;
    }

Usage Example

 /**
  * Execute the SELECT query that has been built up by chaining methods
  * on this class. Return an array of rows as associative arrays.
  */
 protected function _run()
 {
     // allow parent method to run
     if (!$this->cnt_query) {
         // need a way to make sure this is set
         if ($this->_is_raw_query) {
             if (is_array($this->_raw_parameters)) {
                 foreach ($this->_raw_parameters as $k => $v) {
                     if (!is_numeric($v)) {
                         $v = '"' . $v . '"';
                     }
                     $this->_raw_query = str_replace(':' . $k, $v, $this->_raw_query);
                 }
             }
         }
         return parent::_run();
     }
     // we are not caching the COUNT - @todo - implement caching
     $query = $this->_build_select();
     parent::_execute($query, $this->_values, $this->_connection_name);
     $statement = parent::get_last_statement();
     $rows = array();
     while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
         $rows[] = $row;
     }
     // reset Idiorm bound values
     $this->_values = array();
     return $rows;
 }
ORM