NanoFramework 1.4.0
A tiny PHP Framework
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes
Model Class Reference

Connects to the database. More...

Inheritance diagram for Model:
ModelInterface

Public Member Functions

 __construct ()
 
 findAll ()
 
 find (int $id)
 
 insert (array $data)
 
 update (int $id, array $data)
 
 delete (int $id)
 
 where (string $key, mixed $value)
 
 getLastInsertedId ()
 
 validate (array $data, string $ruleset, array &$errors=[])
 
 findAll ()
 
 find (int $id)
 
 insert (array $data)
 
 update (int $id, array $data)
 
 delete (int $id)
 
 where (string $key, mixed $value)
 
 getLastInsertedId ()
 
 validate (array $data, string $ruleset, array &$errors=[])
 

Static Public Member Functions

static now ()
 

Protected Member Functions

 min_length (string $field, array $data, string $params, &$error)
 
 max_length (string $field, array $data, string $params, &$error)
 
 valid_email (string $field, array $data, string $params, &$error)
 
 match (string $field, array $data, string $params, &$error)
 
 required (string $field, array $data, string $params, &$error)
 

Protected Attributes

 $table
 
 $validation
 
 $useTimestamps = true
 

Detailed Description

Connects to the database.

By "database", understand "FakeDb", a extremely poor way to store data in JSON files. This is one of the main reasons why NanoFramework should never be used in production.

Definition at line 12 of file Model.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( )

This constructor will be called by the specialized class and will fail if no $table attribute exists.

Exceptions
exception"You must specify the table" if none is specified

Definition at line 44 of file Model.php.

Member Function Documentation

◆ delete()

delete ( int  $id)

Deletes data from the table.

Parameters
int$idthe id of the data to delete

Implements ModelInterface.

Definition at line 82 of file Model.php.

◆ find()

find ( int  $id)

Retrives the data with the given id or null if it doesn't exist.

Parameters
int$idthe id of the data to retrive
Returns
null|array The data found or null

Implements ModelInterface.

Definition at line 57 of file Model.php.

◆ findAll()

findAll ( )

Retrieves all the data in the table.

Returns
array All the data

Implements ModelInterface.

Definition at line 52 of file Model.php.

◆ getLastInsertedId()

getLastInsertedId ( )

Gives the last id inserted in the table.

Returns
int The last id inserted or 0 if the table is empty

Implements ModelInterface.

Definition at line 94 of file Model.php.

◆ insert()

insert ( array  $data)

Inserts the data in the table.

Parameters
array$datathe data to insert

Implements ModelInterface.

Definition at line 62 of file Model.php.

◆ match()

match ( string  $field,
array  $data,
string  $params,
$error 
)
protected

Returns true if the field contains the same value as the one passed as params.

Parameters
string$fieldThe name of the field to check
array$dataThe whole data
string$paramsThe name of the field to compare
&$errorA variable where to store the error message
Returns
bool If both fields contain the same value

Definition at line 220 of file Model.php.

◆ max_length()

max_length ( string  $field,
array  $data,
string  $params,
$error 
)
protected

Returns true if the field isn't smaller than the param.

Parameters
string$fieldThe name of the field to check
array$dataThe whole data
string$paramsThe maximum expected length of the field content
&$errorA variable where to store the error message
Returns
bool If the field is short enough

Definition at line 178 of file Model.php.

◆ min_length()

min_length ( string  $field,
array  $data,
string  $params,
$error 
)
protected

Returns true if the field isn't bigger than the param.

Parameters
string$fieldThe name of the field to check
array$dataThe whole data
string$paramsThe minimum expected length of the field content
&$errorA variable where to store the error message
Returns
bool If the field is long enough

Definition at line 157 of file Model.php.

◆ now()

static now ( )
static

Gives the current datetime.

Returns
string The current datetime formatted as YYYY-MM-DD HH:mm:ss

Definition at line 142 of file Model.php.

◆ required()

required ( string  $field,
array  $data,
string  $params,
$error 
)
protected

Returns true if the field isn't empty.

Parameters
string$fieldThe name of the field to check
array$dataThe whole data
string$paramsUnused
&$errorA variable where to store the error message
Returns
bool If the field contains something

Definition at line 241 of file Model.php.

◆ update()

update ( int  $id,
array  $data 
)

Updates the table with new data.

Parameters
int$idthe id of the element to update
array$datathe new version of the data

Implements ModelInterface.

Definition at line 72 of file Model.php.

◆ valid_email()

valid_email ( string  $field,
array  $data,
string  $params,
$error 
)
protected

Returns true if the field contains a valid email address.

Parameters
string$fieldThe name of the field to check
array$dataThe whole data
string$paramsUnused
&$errorA variable where to store the error message
Returns
bool If the field contains a value formed as a valid email address

Definition at line 199 of file Model.php.

◆ validate()

validate ( array  $data,
string  $ruleset,
array &  $errors = [] 
)

Validates the given data against a ruleset.

In order to use the model validator, one must first redefine the $validation attribute in the class. That attribute is an associative array having a ruleset name as a key and an associative array describing the rules as a value.

Example of a valid validation rulesets:

protected $validation = [
'user' => [
'username' => 'required|max_length:255',
'email' => 'required|valid_email',
'password' => 'required|min_length:8',
'password_retype' => 'match:password'
],
'login' => [
'username' => 'required',
'password' => 'required'
]
];
Parameters
array$dataThe data to validate as an associative array
string$rulesetThe name of the ruleset to use
array&$errorsAn array where to store the error data if any
Returns
bool If the given data is valid against the given ruleset

Implements ModelInterface.

Definition at line 99 of file Model.php.

◆ where()

where ( string  $key,
mixed  $value 
)

Adds a criteria about the request.

Parameters
string$keyThe key to filter
mixed$valueThe value to keep
Returns
ModelInterface $this for chaining

Implements ModelInterface.

Definition at line 88 of file Model.php.

Field Documentation

◆ $table

$table
protected

The table associated with this model.

Definition at line 17 of file Model.php.

◆ $useTimestamps

$useTimestamps = true
protected

Decide if timestamps must be used or not (default: yes).

Definition at line 27 of file Model.php.

◆ $validation

$validation
protected

The validation rules.

Definition at line 22 of file Model.php.


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