\Nano4\DBModel

A base class for database-driven models.

It wraps around the PDO library, and provides some very simplistic ORM capabilities. You don't have to use them, but if you want them they are there. The built-in ORM represents a single table. For more advanced methods such a multi-table support, you can use the query() method directly and write methods in your extended class.

Summary

Methods
Properties
Constants
class_id()
__construct()
__sleep()
__wakeup()
get_table()
name()
database_name()
query()
wrapRow()
newChild()
is_known()
execute()
getRowByField()
buildWhere()
getRowByFields()
getRowWhere()
getRowById()
listRows()
listByFields()
listPage()
all()
newRow()
rowcount()
pagecount()
pager()
rewind()
current()
next()
key()
valid()
offsetGet()
offsetExists()
offsetUnset()
offsetSet()
cloneRow()
$parent
$known_fields
$sort_orders
$sort_items
$default_sort_order
$default_page_count
return_row
return_raw
return_key
success
dbconnect()
build_sort_orders()
handle_db_error()
handle_stmt_error()
get_cols()
$__classid
$db
$table
$childclass
$resultclass
$primary_key
$resultset
$dsn
$dbname
$dbuser
$dbpass
$get_fields
$default_null
$serialize_ignore
N/A
No private methods found
No private properties found
N/A

Constants

return_row

return_row

return_raw

return_raw

return_key

return_key

success

success

Properties

$parent

$parent : 

Type

$known_fields

$known_fields : 

Type

$sort_orders

$sort_orders : 

Type

$sort_items

$sort_items : 

Type

$default_sort_order

$default_sort_order : 

Type

$default_page_count

$default_page_count : 

Type

$__classid

$__classid : 

The constructor will be passed a '__classid' option.

Ensure this property is populated with its value.

Type

$db

$db : 

Type

$table

$table : 

Type

$childclass

$childclass : 

Type

$resultclass

$resultclass : 

Type

$primary_key

$primary_key : 

Type

$resultset

$resultset : 

Type

$dsn

$dsn : 

Type

$dbname

$dbname : 

Type

$dbuser

$dbuser : 

Type

$dbpass

$dbpass : 

Type

$get_fields

$get_fields : 

Type

$default_null

$default_null : 

Type

$serialize_ignore

$serialize_ignore : 

Type

Methods

class_id()

class_id() 

__construct()

__construct(  $opts = array()) 

Build a new Model object.

Parameters

$opts

__sleep()

__sleep() 

__wakeup()

__wakeup() 

get_table()

get_table() 

Return our table name.

name()

name() 

Return the name of our class.

This depends on the use of the Nano4 Models loader. If you are not using the models loader, override this.

database_name()

database_name() 

Look up the database name. We will cache the result.

query()

query(  $statement,   $assoc = True) 

Create a prepared statement, and set its default fetch style.

Parameters

$statement
$assoc

wrapRow()

wrapRow(  $rowhash) 

Wrap the results of fetch() in a nice object.

If the hash is not set, or is false it returns null. If our childclass is false, we return the unchanged row.

Parameters

$rowhash

newChild()

newChild(  $data = array()) 

Get an instance of our child class, representing a row.

This is used by wrapRow() for existing rows, and can be used directly to return a child row with no primary key set, that can be inserted into the database using $row->save();

If the known_fields class member is set, it will be used to ensure all of the known fields are passed to the child class with some form of default value (if not specified in the known_fields array, the default value depends on the $this->default_null setting.)

Parameters

$data

is_known()

is_known(  $field) 

Check and see if a field is known.

Parameters

$field

execute()

execute(  $statement,   $data = array(),   $class = Null) 

Return a ResultSet representing an executed query.

If it cannot find the resultclass, it instead executes the statement and returns the PDOResult object.

Parameters

$statement
$data
$class

getRowByField()

getRowByField(  $field,   $value,   $ashash = false,   $cols = '*') 

Get a single row based on the value of a field.

Parameters

$field
$value
$ashash
$cols

buildWhere()

buildWhere(  $where,   $data,   $join = 'AND') 

Build a WHERE statement.

This is a fairly simplistic WHERE builder, that supports custom comparison operators, multiple values, and a few other features.

If you need more than this, build your own custom query.

Parameters

$where
$data
$join

getRowByFields()

getRowByFields(  $fields,   $ashash = False,   $cols = '*') 

Get a single row based on the value of multiple fields.

This is a simple AND based approach, and uses = as the comparison. If you need anything more complex, write your own method, or use getRowWhere() instead.

Parameters

$fields
$ashash
$cols

getRowWhere()

getRowWhere(  $where,   $data = array(),   $ashash = False,   $cols = '*') 

Get a single row, specifying the WHERE clause and bound data.

Parameters

$where
$data
$ashash
$cols

getRowById()

getRowById(  $id,   $ashash = False,   $cols = '*') 

Get a single row based on the value of the primary key.

Parameters

$id
$ashash
$cols

listRows()

listRows(  $stmt,   $data,   $cols = Null) 

Return a result set using a hand crafted SQL statement.

Parameters

$stmt
$data
$cols

listByFields()

listByFields(  $fields,   $cols = Null,   $append = Null,   $data = array()) 

Return a result set using a map of fields.

Parameters

$fields
$cols
$append
$data

listPage()

listPage(  $where,   $pageopts,   $cols = Null,   $data = array()) 

Get a page of results.

Parameters

$where
$pageopts
$cols
$data

all()

all() 

Return a ResultSet (or other result class) for all rows in our table.

newRow()

newRow(  $row,   $opts = array()) 

Insert a new row.

Note this does no checking to ensure the specified fields are valid, so wrap this in your own classes with more specific versions. It's also not recommended that you include the primary key field, as it should be auto generated by the database. To this end, by default we disallow the primary key field. As the output of an insert is not consistent we just return the query object, if you're at all interested.

Parameters

$row
$opts

rowcount()

rowcount(  $where = Null,   $data = array(),   $colname = '*') 

Row count.

Parameters

$where
$data
$colname

pagecount()

pagecount(  $rowcount = Null,   $opts = array()) 

Page count.

Parameters

$rowcount
$opts

pager()

pager(  $opts = array()) 

Generate ORDER BY and LIMIT statements, based on a provided sort order, number of items to display per page, and what page you are currently on.

NOTE: pages start with 1, not 0.

Parameters

$opts

rewind()

rewind() 

current()

current() 

next()

next() 

key()

key() 

valid()

valid() 

offsetGet()

offsetGet(  $offset) 

Parameters

$offset

offsetExists()

offsetExists(  $offset) 

Parameters

$offset

offsetUnset()

offsetUnset(  $offset) 

Parameters

$offset

offsetSet()

offsetSet(  $offset,   $value) 

Parameters

$offset
$value

cloneRow()

cloneRow(  $rowid) 

Parameters

$rowid

dbconnect()

dbconnect() 

build_sort_orders()

build_sort_orders() 

handle_db_error()

handle_db_error(  $einfo,   $context,   $name = 'database') 

Parameters

$einfo
$context
$name

handle_stmt_error()

handle_stmt_error(  $einfo,   $context,   $name = 'statement') 

Parameters

$einfo
$context
$name

get_cols()

get_cols(  $cols) 

Parameters

$cols