\Anax\DatabaseActiveRecordModel

An implementation of the Active Record pattern to be used as base class for database driven models.

Summary

Methods
Properties
Constants
setDb()
find()
findById()
findWhere()
findAll()
findAllWhere()
save()
saveWhere()
delete()
deleteWhere()
No public properties found
No constants found
checkDb()
getProperties()
create()
update()
updateWhere()
$db
$tableName
$tableIdColumn
N/A
No private methods found
No private properties found
N/A

Properties

$tableName

$tableName : string

Type

string — name of the database table.

$tableIdColumn

$tableIdColumn : string

Type

string — name of the id column in the database table.

Methods

setDb()

setDb(\Anax\Database\DatabaseQueryBuilder  $db) : void

Set the database object to use for accessing storage.

Parameters

\Anax\Database\DatabaseQueryBuilder $db

as database access object.

find()

find(string  $column, mixed  $value) : \Anax\Database\this

Find and return first object found by search criteria and use its data to populate this instance.

Parameters

string $column

to use in where statement.

mixed $value

to use in where statement.

Returns

\Anax\Database\this

findById()

findById(integer  $id = null) : \Anax\Database\this

Find and return first object by its tableIdColumn and use its data to populate this instance.

Parameters

integer $id

to find or use $this->{$this->tableIdColumn} as default.

Returns

\Anax\Database\this

findWhere()

findWhere(string  $where, mixed  $value) : \Anax\Database\this

Find and return first object found by search criteria and use its data to populate this instance.

The search criteria $where of can be set up like this: id = ? id1 = ? and id2 = ?

The $value can be a single value or an array of values.

Parameters

string $where

to use in where statement.

mixed $value

to use in where statement.

Returns

\Anax\Database\this

findAll()

findAll() : array

Find and return all.

Returns

array —

of object of this class

findAllWhere()

findAllWhere(string  $where, mixed  $value) : array

Find and return all matching the search criteria.

The search criteria $where of can be set up like this: id = ? id IN [?, ?]

The $value can be a single value or an array of values.

Parameters

string $where

to use in where statement.

mixed $value

to use in where statement.

Returns

array —

of object of this class

save()

save() : void

Save current object/row, insert if id is missing and do an update if the id exists.

saveWhere()

saveWhere(string  $where, mixed  $value) : void

Save/update current object/row using a custom where-statement.

The criteria $where of can be set up like this: id = ? id1 = ? AND id2 = ?

The $value can be a single value or an array of values.

Parameters

string $where

to use in where statement.

mixed $value

to use in where statement.

delete()

delete(integer  $id = null) : void

Update row using $tableIdColumn as where and clear value of `$tableIdColumn`.

Parameters

integer $id

to delete or use $this->{$this->tableIdColumn} as default.

deleteWhere()

deleteWhere(string  $where, mixed  $value) : void

Delete row using a custom where-statement and leave value of `$tableIdColumn` as it is.

The criteria $where of can be set up like this: id = ? id1 = ? AND id2 = ? id IN (?, ?)

The $value can be a single value or an array of values.

Parameters

string $where

to use in where statement.

mixed $value

to use in where statement.

checkDb()

checkDb() : void

Check if database is injected or throw an exception.

Throws

\Anax\Database\Exception\ActiveRecordException

when database is not set.

getProperties()

getProperties() : array

Get essential object properties.

Returns

array —

with object properties.

create()

create() : void

Create new row.

update()

update() : void

Update row using $tableIdColumn as where.

updateWhere()

updateWhere(string  $where, mixed  $value) : void

Update row using a custom where-statement.

The criteria $where of can be set up like this: id = ? id1 = ? AND id2 = ? id IN (?, ?)

The $value can be a single value or an array of values.

Parameters

string $where

to use in where statement.

mixed $value

to use in where statement.