Direct queries

All the models contain an object of "Database" class, which allows making direct queries to database. This object is accessible in the model object as a property "$this -> db". Model table is accessible inside the model as "$this -> table". To make safe SQL queries use "secure()" method, as described in Security section.

There are the following methods to send requests :

query() - performs the SQL query received

//All published records of current table
$result = $this -> db -> query("SELECT *
                                FROM `".$this -> table."` 
                                WHERE `public`='1'
                                ORDER BY `date` DESC");

while($row = $this -> db -> fetch($result, "ASSOC"))
{
   ...
}

getAll() - extracts all records and returns an associative array (if there is "id" field in a table, then this field will become a key in resulting array)

//All published comments to news with id=32
$rows = $this -> db -> getAll("SELECT `name`,`date`,`content` 
                               FROM `news_comments` 
                               WHERE `news_id`='32'
                               AND `public`='1'
                               ORDER BY `date` DESC");

foreach($rows as $row)
{
   ...
}

getCount() - counts the number of records in a table, returns an integer number

//Total quantity of articles
$total = $this -> db -> getCount("articles");
 
//Number of active products in a certain catalog
$total = $this -> db -> getCount("products", "`catalog`='98' AND `active`='1'");

getRow() - extracts the first row from a table, returns an associated array of fields

//All fields of product with id=527
$row = $this -> db -> getRow("SELECT * FROM `products` WHERE `id`='527'");

getColumn() - extracts the single column from a table, returns a regular array of fields

//Titles of events on the defined date
$titles = $this -> db -> getColunm("SELECT `title` FROM `events` WHERE `date`='2011-03-24'");

getCell() - extracts one cell of a table

//Returns product price with the name "New product" 
$price = $this -> db -> getCell("SELECT `price` FROM `products` WHERE `name`='New product'");