|
| __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 () |
|
|
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 = "" |
|
|
| $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 |
|
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 | $logger | PSR-7 compliant logger object |
\SlaxWeb\Config\Container | $config | Configuration container object |
\ICanBoogie\Inflector | $inflector | Inflector object for pluralization and word transformations |
\SlaxWeb\Database\Query\Builder | $queryBuilder | Query Builder instance |
\SlaxWeb\Database\Interface\Library | $db | Database library object |
\SlaxWeb\Hooks\Container | $hooks | HooksContainer hooks container object |
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 | $data | Data 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 | $col | Column 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 | $predicates | Grouped 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 | $modelMethod | Model method name. |
string | $before | Invoke '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 | $table | Table name to which the join is to be made |
string | $type | Join 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
-
- 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 | $primKey | Key of the main table for the condition |
string | $forKey | Key of the joining table |
string | $cOpr | Comparison 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 | $model | Joining model instance |
string | $forKey | Foreign key against which the joining models primary key is matched |
string | $type | Join type, default string("INNER JOIN") |
string | $cOpr | Comparison 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 | $limit | Number of rows to limit the result set to |
int | $offset | Number 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 | $column | Column name |
closure | $nested | Nested builder |
string | $lOpr | Logical 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 | $col | Column name to be added to the group by list |
string | $direction | Direction of order, default self::ORDER_ASC |
string | $func | SQL 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 | $predicates | Grouped 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 | $primKey | Key of the main table for the condition |
string | $forKey | Key of the joining table |
string | $cOpr | Comparison 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 | $column | Column name |
closure | $nested | Nested builder |
string | $lOpr | Logical 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 | $column | Column name |
mixed | $value | Value of the predicate |
string | $opr | Logical 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
-
- 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 | $columns | Column 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 | $column | Column name |
mixed | $value | Value of the predicate |
string | $opr | Logical operator |
- Returns
- self
const SlaxWeb\Database\BaseModel::HOOK_BEFORE = "before" |
const SlaxWeb\Database\BaseModel::JOIN_INNER = "INNER JOIN" |
const SlaxWeb\Database\BaseModel::SDEL_VAL_BOOL = 1 |
const SlaxWeb\Database\BaseModel::TBL_NAME_CAMEL_UCFIRST = 1 |
The documentation for this class was generated from the following file:
- /opt/slax0rDev/slaxweb/database/src/BaseModel.php