RBFactory - rbertram90/core GitHub Wiki
This factory class should be extended as follows:
-
Provide
$tableName
property -
List
$fields
that matches database structure in array format with key being column name and value the datatype example:protected $fields = [ "id" => "int", "name" => "string", "dob" => "string", "gender" => "string" ]
Properties
protected string $tableName
Match the name of the database table
protected mixed[] $fields
Define the fields within the model (columns in the database)
protected rbwebdesigns\core\Database $db
Connection to the database
protected string $subClass
Name of the class that gets instantiated through PDO_FETCH_CLASS
Methods
public getFields()
Returns $this->fields
public count(array $where)
Get a count of rows
public get(mixed $what, array $where, string $order='', string $limit='', bool $multi=true)
Get data from the database.
$what
can be a string (e.g. '*') or an array of field names.
$where
is an array of key => value elements where key is the field name and value is desired constraint.
$order
is simply injected into the SQL string (e.g. 'name ASC', 'age DESC').
$limit
is again injected so can be a number or two numbers separated by a comma for paged result.
$multi
is a flag for if multiple results are expected in which case a multi-dimensional structure is returned.
public insert(array $what)
Creates a row in the database. $what
is array of key => value elements where key is field name and value is new field value.
public update(array $where, array $what)
Updates rows in the database. $where
is an array of key => value elements where key is the field name and value is desired limitation. $what
is array of key => value elements where key is field name and value is new field value.
public delete(array $where)
Deletes rows from the database. $where
is an array of key => value elements where key is the field name and value is desired limitation.
public sanitizeFields(array &$values)
Checks the data conforms to the type provided in the $fields
for multiple fields
public sanitizeField(string $fieldkey, mixed $value)
Checks the data conforms to the type provided in the $fields
for a single field