SlaxWeb Framework
 All Classes Namespaces Functions Variables Pages
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
SlaxWeb\Database\BaseModel Class Reference
Inheritance diagram for SlaxWeb\Database\BaseModel:
Inheritance graph
[legend]

Public Member Functions

 __construct (Logger $logger, Config $config, Inflector $inflector, Builder $queryBuilder, Database $db, HooksContainer $hooks)
 
 getPrimKey ()
 
 create (array $data)
 
 select (array $columns)
 
 update (array $columns)
 
 delete ()
 
 where (string $column, $value, string $opr="=")
 
 orWhere (string $column, $value, string $opr="=")
 
 groupWhere (\Closure $predicates)
 
 orGroupWhere (\Closure $predicates)
 
 nestedWhere (string $column,\Closure $nested, string $lOpr="IN")
 
 orNestedWhere (string $column,\Closure $nested, string $lOpr="IN")
 
 join (string $table, string $type="INNER JOIN")
 
 joinModel (BaseModel $model, string $forKey, string $type="INNER JOIN", string $cOpr="=")
 
 joinCond (string $primKey, string $forKey, string $cOpr="=")
 
 orJoinCond (string $primKey, string $forKey, string $cOpr="=")
 
 joinCols (array $cols)
 
 groupBy (string $col)
 
 orderBy (string $col, string $direction="ASC", string $func="")
 
 limit (int $limit, int $offset=0)
 
 lastError ()
 

Public Attributes

const TBL_NAME_CAMEL_UCFIRST = 1
 
const TBL_NAME_CAMEL_LCFIRST = 2
 
const TBL_NAME_UNDERSCORE = 3
 
const TBL_NAME_UPPERCASE = 4
 
const TBL_NAME_LOWERCASE = 5
 
const HOOK_BEFORE = "before"
 
const HOOK_AFTER = "after"
 
const JOIN_INNER = "INNER JOIN"
 
const JOIN_LEFT = "LEFT OUTER JOIN"
 
const JOIN_RIGHT = "RIGHT OUTER JOIN"
 
const JOIN_FULL = "FULL OUTER JOIN"
 
const JOIN_CROSS = "CROSS JOIN"
 
const SDEL_VAL_BOOL = 1
 
const SDEL_VAL_TIMESTAMP = 2
 
 $table = ""
 

Protected Member Functions

 invokeHook (string $modelMethod, string $before=self::HOOK_BEFORE)
 
 getClassName ()
 
 setTable ()
 
 setSoftDelete ()
 
 setTimestampConfig ()
 

Protected Attributes

 $primKey = ""
 
 $logger = null
 
 $config = null
 
 $inflector = null
 
 $qBuilder = null
 
 $db = null
 
 $error = null
 
 $softDelete = false
 
 $delCol = ""
 
 $delValType = 0
 
 $timestamps = null
 
 $createdColumn = ""
 
 $updatedColumn = ""
 
 $timestampFunction = ""
 
 $hooks = null
 
 $hookName = null
 

Constructor & Destructor Documentation

SlaxWeb\Database\BaseModel::__construct ( Logger  $logger,
Config  $config,
Inflector  $inflector,
Builder  $queryBuilder,
Database  $db,
HooksContainer  $hooks 
)

Class constructor

Initialize the Base Model, by storging injected dependencies into class properties.

Parameters
\Psr\Log\LoggerInterface$loggerPSR-7 compliant logger object
\SlaxWeb\Config\Container$configConfiguration container object
\ICanBoogie\Inflector$inflectorInflector object for pluralization and word transformations
\SlaxWeb\Database\Query\Builder$queryBuilderQuery Builder instance
\SlaxWeb\Database\Interface\Library$dbDatabase library object
\SlaxWeb\Hooks\Container$hooksHooksContainer hooks container object

Member Function Documentation

SlaxWeb\Database\BaseModel::create ( array  $data)

Create record

Creates a record row in the database with the supplied data and the help of the database library. It returns bool(true) on success, and bool(false) on failure.

Parameters
array$dataData for the create statement
Returns
bool
SlaxWeb\Database\BaseModel::delete ( )

Delete query

Run an delete query against the database. Returns bool(true) on success, and bool(false) on error.

Returns
bool
SlaxWeb\Database\BaseModel::getClassName ( )
protected

Returns curent, lowercased class name without namespace.

Returns
string Lowercased class name.
SlaxWeb\Database\BaseModel::getPrimKey ( )

Get Primary Key

Gets the primary key column name of the model.

Returns
string
SlaxWeb\Database\BaseModel::groupBy ( string  $col)

Group by

Add a column to the group by list.

Parameters
string$colColumn name to be added to the group by list.
Returns
self
SlaxWeb\Database\BaseModel::groupWhere ( \Closure  $predicates)

Grouped Where predicates

Adds a group of predicates to the the predicate list. The method must receive a closure as its input parameter. The closure in turn receives the builder object as its input parameter. Additional where predicates must be added to the builder through this object.

Parameters
Closure$predicatesGrouped predicates definition closure
Returns
self
SlaxWeb\Database\BaseModel::invokeHook ( string  $modelMethod,
string  $before = self::HOOK_BEFORE 
)
protected

Invoke hook

Invokes the hook specified by the name. The whole hook name consists of string model, class name or custom hook name stored in the protected property hookName, before or after concatenated with the modelMethod. Example: "model.user.before.init" for a user model.

Parameters
string$modelMethodModel method name.
string$beforeInvoke 'before' '$modelMethod' hook, default self::HOOK_BEFORE
Returns
void
SlaxWeb\Database\BaseModel::join ( string  $table,
string  $type = "INNER JOIN" 
)

Add table to join

Adds a new table to join with the main table to the list of joins. If only a table is added without a condition with the 'joinCond', an exception will be thrown when an attempt to create a query is made.

Parameters
string$tableTable name to which the join is to be made
string$typeJoin type, default string("INNER JOIN")
Returns
self
SlaxWeb\Database\BaseModel::joinCols ( array  $cols)

Join Columns

Add columns to include in the select column list. If no table for joining was yet added, an exception is raised. Same rules apply to the column list as in the 'select' method.

Parameters
array$colsColumn list
Returns
self
SlaxWeb\Database\BaseModel::joinCond ( string  $primKey,
string  $forKey,
string  $cOpr = "=" 
)

Add join condition

Adds a JOIN condition to the last join added. If no join was yet added, an exception is raised.

Parameters
string$primKeyKey of the main table for the condition
string$forKeyKey of the joining table
string$cOprComparison operator for the two keys
Returns
self
SlaxWeb\Database\BaseModel::joinModel ( BaseModel  $model,
string  $forKey,
string  $type = "INNER JOIN",
string  $cOpr = "=" 
)

Model Join

Construct a join statement from a model object. The method automatically obtains the primary key column name from the joining model, construcing a basic join statement for that models table automagically. If the joining model does not have a primary key column name set an exception is thrown.

Parameters
\SlaxWeb\Database\BaseModel$modelJoining model instance
string$forKeyForeign key against which the joining models primary key is matched
string$typeJoin type, default string("INNER JOIN")
string$cOprComparison operator for the two keys
Returns
self
SlaxWeb\Database\BaseModel::lastError ( )

Get last error

Retrieves the last occured error from the database library and returns it.

Returns
SlaxWeb\Database\BaseModel::limit ( int  $limit,
int  $offset = 0 
)

Limit

Limit number of rows to be returned from the database. Second parameter will also add an offset to the statement.

Parameters
int$limitNumber of rows to limit the result set to
int$offsetNumber of rows for the result to be offset from, default int(0)
Returns
self
SlaxWeb\Database\BaseModel::nestedWhere ( string  $column,
\Closure  $nested,
string  $lOpr = "IN" 
)

Where Nested Select

Add a nested select as a value to the where predicate.

Parameters
string$columnColumn name
closure$nestedNested builder
string$lOprLogical operator, default string("IN")
Returns
self
SlaxWeb\Database\BaseModel::orderBy ( string  $col,
string  $direction = "ASC",
string  $func = "" 
)

Order by

Add a column to the order by list.

Parameters
string$colColumn name to be added to the group by list
string$directionDirection of order, default self::ORDER_ASC
string$funcSQL function to use ontop of the column, default string("")
Returns
self
SlaxWeb\Database\BaseModel::orGroupWhere ( \Closure  $predicates)

Or Grouped Where predicates

Works the same way as 'Grouped Where predicates' method, except it adds the predicate group to the list with the "OR" comparison operator.

Parameters
Closure$predicatesGrouped predicates definition closure
Returns
self
SlaxWeb\Database\BaseModel::orJoinCond ( string  $primKey,
string  $forKey,
string  $cOpr = "=" 
)

Add OR join condition

Alias for the 'joinCond' with the "OR" logical operator.

Parameters
string$primKeyKey of the main table for the condition
string$forKeyKey of the joining table
string$cOprComparison operator for the two keys
Returns
self
SlaxWeb\Database\BaseModel::orNestedWhere ( string  $column,
\Closure  $nested,
string  $lOpr = "IN" 
)

Or Where Nested Select

Works the same way as "Where Nested Select" except that it links the nested select predicate with an "OR" comparisson operator instead of an "AND".

Parameters
string$columnColumn name
closure$nestedNested builder
string$lOprLogical operator, default string("IN")
Returns
self
SlaxWeb\Database\BaseModel::orWhere ( string  $column,
  $value,
string  $opr = "=" 
)

Or Where predicate

Works the same way as 'Where predicate' method, except it adds the predicate to the list with the "OR" comparison operator.

Parameters
string$columnColumn name
mixed$valueValue of the predicate
string$oprLogical operator
Returns
self
SlaxWeb\Database\BaseModel::select ( array  $columns)

Select query

Run a select query on the database with the previously assigned columns, joins, group bys, limits, etc. The column list is an array, if the key of an entry is of type string, then the name of that key is used as a SQL function. On success it returns the Result object, and on error it raises an Exception.

Parameters
array$columnsColumn list
Returns
SlaxWeb\Database\BaseModel::setSoftDelete ( )
protected

Set soft deletion

Sets the soft deletion options to class properties from the configuration.

Returns
void
SlaxWeb\Database\BaseModel::setTable ( )
protected

Set table name

Sets the table name based on the model class name. It discards the whole namespace, and uses only the class name. The class name is pluralized, if defined so by the 'pluralizeTableName' configuration option. It will also transform the name into the right format, based on the 'tableNameStyle' configuration option.

Returns
void
SlaxWeb\Database\BaseModel::setTimestampConfig ( )
protected

Set Timestamp Config

Sets the timestamp configuration to the class properties. Any previously user set values to the configuration properties are not overwritten.

Returns
void
SlaxWeb\Database\BaseModel::update ( array  $columns)

Update query

Run an update query against the database. The input array defines a list of columns and their new values that they should be updated to. The where predicates that are set before the call to this * method will be used in the update statement. Returns bool(true) on success, and bool(false) on error.

Parameters
array$columnsColumn list with values
Returns
bool
SlaxWeb\Database\BaseModel::where ( string  $column,
  $value,
string  $opr = "=" 
)

Where predicate

Adds a where predicate for the next query to be ran. The method takes 3 input arguments, where the first is the name of column, the second is the value of the predicate, and the 3rd is an logical operator linking the two. The logical operator defaults to the equals signs(=).

Parameters
string$columnColumn name
mixed$valueValue of the predicate
string$oprLogical operator
Returns
self

Member Data Documentation

const SlaxWeb\Database\BaseModel::HOOK_BEFORE = "before"

Hook invokation type

const SlaxWeb\Database\BaseModel::JOIN_INNER = "INNER JOIN"

Join types

const SlaxWeb\Database\BaseModel::SDEL_VAL_BOOL = 1

Soft delete options

const SlaxWeb\Database\BaseModel::TBL_NAME_CAMEL_UCFIRST = 1

Table name style


The documentation for this class was generated from the following file: