-
Notifications
You must be signed in to change notification settings - Fork 120
PDORM Database
The MicroMVC system uses the PDORM database library to provide ORM like access to results and queries. PDORM is built on top of the PDO php library which means you have access to all the regular PDO methods plus many more helpful add-on functions.
Using the database DB::get() method will return FALSE or an array of results. Each element in the result array will be an array or object (depending on what you set DB::fetch_mode too) that corresponds to a row in the database. You can still access the PDOStatement object that is set in DB::result.
<?php
$users = $this->db->get('users');
foreach($users as $user) {
print $user->name . '<br />';
}
// If you need it
// $PDOStatement = $this->db->result;
When calling DB::query() the result will either be a PDOStatement object (or FALSE on failure). If the result is FALSE then the query was invalid or the DB didn’t like something. If a PDOStatement is returned we still don’t know if there are actual results found yet. After determining that the query did not fail you will have to call the fetch(), fetchColumn() or fetchAll() methods to see if actual results exist. Do not rely on the rowCount() function as it is very picky about when and where it works.
<?php
$result = $this->db->query('users');
if($result) {
if($row = $result->fetch()) {
print_r($row);
} else {
print 'There are no results';
}
} else {
print 'The query failed';
}
If you passed the auto_count value of TRUE to the query() or get() methods then an extra variable (PDOStatement→total_rows) and query will be run to count the total rows found in the database minus any LIMIT or OFFSET clauses.