class Table extends SimpleTable

Class Table allows access to a table using an active record.

It is recommended to create a descendant for each table of the database, defining its tablename and structure.

Properties

DebugTool $debugTool The debug tool used. from Entity
string $shortName Class short name. from Entity
bool $exists from Entity
string $id from Entity
string $idField from Entity
string $nameField from Entity
array $newData from Entity
array $oldData from Entity
string $tableName It is the name of the table. from FlatTable
string $modelName It's the name of the model associated with the table from FlatTable
string $locked This field can exist or not (added here to avoid scrutinizer "property not exists")

Methods

__construct(string $tableName, array $params = [])

Build a Table model. $table is the name of the table in the database.

string
getId()

Return the value of id.

from Entity
string
getNameField()

Returns the name of the identification field of the record. By default it will be name.

from Entity
$this
setData(array $data)

Assign newData from $data.

from Entity
string
getIdField()

Returns the name of the main key field of the table (PK-Primary Key). By default it will be id.

from Entity
array
getData()

Return newData details.

from Entity
array
getOldData()

Return oldData details.

from Entity
$this
setOldData(array $data)

Assign oldData from an array.

from Entity
$this|mixed|null
__call(string $method, array $params)

Execute a magic method of the setField or getField style

from Entity
mixed
__get(string $propertyName)

Magic getter.

from Entity
$this
__set(string $propertyName, mixed $propertyValue)

Magic setter.

from Entity
bool
__isset(string $propertyName)

Magic isset.

from Entity
get(string $id)

Returns a new instance of the table with the requested record.

from FlatTable
bool
getDataById(string $id)

This method is private. Use load instead.

from FlatTable
string
getQuotedTableName(bool $usePrefix = true)

Get the name of the table (with prefix)

from FlatTable
newRecord(string|null $id = null)

Sets the active record in a new record.

from FlatTable
array
defaultData()

TODO: Undocummented.

from SimpleTable
string
getTableName(bool $usePrefix = true)

Get the name of the table (with prefix)

from FlatTable
array
getDataArray(string $id = null)

Return an array with the current active record.

from FlatTable
bool
load(string $id)

Establishes a record as an active record.

from FlatTable
bool
save()

Saves the changes made to the active record.

from FlatTable
bool
updateRecord(array $data)

Update the modified fields in the active record.

from FlatTable
bool
insertRecord(array $values)

Insert a new record.

from FlatTable
bool
delete()

Deletes the active record.

from FlatTable
setStructure()

Execute a call to setTableStructure with an array containing 3 arrays with the fields, keys and default values for the table.

from SimpleTable
setTableStructure(string $table, array $structure)

Save the structure of the table in a static array, so that it is available at all times.

from SimpleTable
array
getStructureArray()

A raw array is built with all the information available in the table, configuration files and code.

from SimpleTable
array
getFieldsFromTable()

Return a list of fields and their table structure. Each final model that needed, must overwrite it.

from SimpleTable
bool
getBy(string $key, mixed $value)

Returns a new instance of the table with the requested record.

from SimpleTable
bool
getDataBy(string $key, mixed $value)

This method is private. Use getBy instead.

from SimpleTable
array
getStructure()

Returns the structure of the normalized table

from SimpleTable
array
getAllRecords()

Get an array with all data in table.

from SimpleTable
int
countAllRecords()

Count all registers in table.

from SimpleTable
array
getAllRecordsPaged(int $offset = 0)

Get an array with all data per page.

from SimpleTable
array
search(string $query, array $columns = [], int $offset = 0, string $order = '')

Do a search to a table.

from SimpleTable
string
searchQuery(string $query, array $columns = [])

Return the main part of the search SQL query.

from SimpleTable
int
searchCount(string $query, array $columns = [])

Do a search to a table.

from SimpleTable
checkStructure(bool $create = false)

Create a new table if it does not exist and it has been passed true as a parameter.

string
getIdByName(string $name)

Perform a search of a record by the name, returning the id of the corresponding record, or '' if it is not found or does not have a name field.

array
getAllRecordsBy(string $key, mixed $value, string $comparison = '=', string $orderBy = '')

Get an array with all data.

array
getIndexesFromTable()

Return a list of key indexes.

array
getDefaultValues()

Return a list of default values.

bool|false|int|string
getDefaultValue(array $valueData)

Get default value data for this valueData.

array
getChecksFromTable()

TODO: Undocumented

bool
saveRecord(array $data)

Save the data to a record if pass the test and returns true/false based on the result.

bool
testData($data)

TODO: Undocumented

bool
saveData(array $data)

Try to save the data and return true/false based on the result.

Details

at line 35
__construct(string $tableName, array $params = [])

Build a Table model. $table is the name of the table in the database.

$params is a parameters array: - create is true if the table is to be created if it does not exist (false by default) - idField is the name of the primary key (default id) - nameField is the name of the descriptive field (name by default)

Parameters

string $tableName
array $params

in Entity at line 106
string getId()

Return the value of id.

Return Value

string

in Entity at line 116
string getNameField()

Returns the name of the identification field of the record. By default it will be name.

Return Value

string

in Entity at line 128
$this setData(array $data)

Assign newData from $data.

Parameters

array $data

Return Value

$this

in Entity at line 143
string getIdField()

Returns the name of the main key field of the table (PK-Primary Key). By default it will be id.

Return Value

string

in Entity at line 153
array getData()

Return newData details.

Return Value

array

in Entity at line 163
array getOldData()

Return oldData details.

Return Value

array

in Entity at line 175
$this setOldData(array $data)

Assign oldData from an array.

Parameters

array $data

Return Value

$this

in Entity at line 191
$this|mixed|null __call(string $method, array $params)

Execute a magic method of the setField or getField style

Parameters

string $method
array $params

Return Value

$this|mixed|null

Exceptions

RuntimeException

in Entity at line 220
mixed __get(string $propertyName)

Magic getter.

It allows access to a field of the record using the attribute. To access the name field, we should use $this->getName(), but thanks to this, we can also use $this->name.

Parameters

string $propertyName

Return Value

mixed

in Entity at line 236
$this __set(string $propertyName, mixed $propertyValue)

Magic setter.

Allows you to assign value to a field in the record using the attribute. To assign a value to the name field, we should use $this->setName('Pepe'), but thanks to this, we can also use $this->name='Pepe'.

Parameters

string $propertyName
mixed $propertyValue

Return Value

$this

in Entity at line 252
bool __isset(string $propertyName)

Magic isset.

Parameters

string $propertyName

Return Value

bool

in FlatTable at line 61
SimpleTable get(string $id)

Returns a new instance of the table with the requested record.

As a previous step, a getData of the current instance is made, so both will point to the same record. Makes a getData and returns a new instance of the model.

Parameters

string $id

Return Value

SimpleTable

in FlatTable at line 80
private bool getDataById(string $id)

This method is private. Use load instead.

Establishes a record as an active record. If found, return true and the $id will be in $this->id and the data in $this->newData. If it is not found, return false, and have not effect into the instance.

Parameters

string $id

Return Value

bool

in FlatTable at line 102
string getQuotedTableName(bool $usePrefix = true)

Get the name of the table (with prefix)

Parameters

bool $usePrefix

Return Value

string

in FlatTable at line 111
newRecord(string|null $id = null)

Sets the active record in a new record.

Note that changes made to the current active record will be lost.

Parameters

string|null $id

in SimpleTable at line 105
array defaultData()

TODO: Undocummented.

Return Value

array

in FlatTable at line 139
string getTableName(bool $usePrefix = true)

Get the name of the table (with prefix)

Parameters

bool $usePrefix

Return Value

string

in FlatTable at line 153
array getDataArray(string $id = null)

Return an array with the current active record.

If an $id is indicated, it searches to change the active record before returning the value. Warning: If an $id is set, any unsaved data will be lost when searching for the new record.

Parameters

string $id

Return Value

array

in FlatTable at line 170
bool load(string $id)

Establishes a record as an active record.

If found, the $id will be in $this->id and the data in $this->newData. If it is not found, $this->id will contain '' and $this->newData will contain the data by default.

Parameters

string $id

Return Value

bool

in FlatTable at line 180
bool save()

Saves the changes made to the active record.

Return Value

bool

in FlatTable at line 213
private bool updateRecord(array $data)

Update the modified fields in the active record.

$data is an array of assignments of type field=value.

Parameters

array $data

Return Value

bool

in FlatTable at line 239
private bool insertRecord(array $values)

Insert a new record.

$fields is an array of fields and $values an array with the values for each field in the same order.

Parameters

array $values

Return Value

bool

in FlatTable at line 281
bool delete()

Deletes the active record.

Return Value

bool

in SimpleTable at line 54
setStructure()

Execute a call to setTableStructure with an array containing 3 arrays with the fields, keys and default values for the table.

The development will be more ambitious than what is defined.

Currently Table includes a single table, but the idea is to be able to relate tables to form complex data models.

in SimpleTable at line 67
protected setTableStructure(string $table, array $structure)

Save the structure of the table in a static array, so that it is available at all times.

Parameters

string $table
array $structure

in SimpleTable at line 79
protected array getStructureArray()

A raw array is built with all the information available in the table, configuration files and code.

Return Value

array

in SimpleTable at line 95
array getFieldsFromTable()

Return a list of fields and their table structure. Each final model that needed, must overwrite it.

Return Value

array

in SimpleTable at line 124
bool getBy(string $key, mixed $value)

Returns a new instance of the table with the requested record.

As a previous step, a getDataBy of the current instance is made, so both will point to the same record. Makes a getDataBy and returns a new instance of the model.

Parameters

string $key
mixed $value

Return Value

bool

in SimpleTable at line 140
private bool getDataBy(string $key, mixed $value)

This method is private. Use getBy instead.

Establishes a record as an active record. If found the pair $key-$value the data will be in $this->newData. If it is not found, $this->id will contain '' and $this->newData will contain the data by default.

Parameters

string $key
mixed $value

Return Value

bool

in SimpleTable at line 161
array getStructure()

Returns the structure of the normalized table

Return Value

array

in SimpleTable at line 172
array getAllRecords()

Get an array with all data in table.

WARNING: This could be dangerous with a big table.

Return Value

array

in SimpleTable at line 183
int countAllRecords()

Count all registers in table.

Return Value

int

in SimpleTable at line 198
array getAllRecordsPaged(int $offset = 0)

Get an array with all data per page.

Parameters

int $offset

Return Value

array

Do a search to a table.

Returns the result per page.

Parameters

string $query What to look for
array $columns For example: [0 => 'name']
int $offset By default 0
string $order By default the main name field if defined

Return Value

array

in SimpleTable at line 234
string searchQuery(string $query, array $columns = [])

Return the main part of the search SQL query.

Parameters

string $query
array $columns

Return Value

string

in SimpleTable at line 268
int searchCount(string $query, array $columns = [])

Do a search to a table.

Returns the result per page.

Parameters

string $query What to look for
array $columns For example: [0 => 'name']

Return Value

int

at line 52
checkStructure(bool $create = false)

Create a new table if it does not exist and it has been passed true as a parameter.

This should check if there are differences between the defined in dbStructure and the physical table, correcting the differences if true is passed as parameter.

Parameters

bool $create

at line 68
string getIdByName(string $name)

Perform a search of a record by the name, returning the id of the corresponding record, or '' if it is not found or does not have a name field.

Parameters

string $name

Return Value

string

at line 94
array getAllRecordsBy(string $key, mixed $value, string $comparison = '=', string $orderBy = '')

Get an array with all data.

Parameters

string $key
mixed $value
string $comparison By default is '='
string $orderBy

Return Value

array

at line 119
array getIndexesFromTable()

Return a list of key indexes.

Each final model that needed, must overwrite it.

TODO: Why "*FromTable()" need to be overwrited on final model? Is not from model definition.

Return Value

array

at line 130
array getDefaultValues()

Return a list of default values.

Each final model that needed, must overwrite it.

Return Value

array

at line 146
private bool|false|int|string getDefaultValue(array $valueData)

Get default value data for this valueData.

Parameters

array $valueData

Return Value

bool|false|int|string

at line 188
array getChecksFromTable()

TODO: Undocumented

Return Value

array

at line 200
bool saveRecord(array $data)

Save the data to a record if pass the test and returns true/false based on the result.

Parameters

array $data

Return Value

bool

at line 215
protected bool testData($data)

TODO: Undocumented

Parameters

$data

Return Value

bool

at line 266
protected bool saveData(array $data)

Try to save the data and return true/false based on the result.

Parameters

array $data

Return Value

bool