Class TableAccess
Direct known subclasses
AutoLogin
,
Component
,
TableGuestbook
,
TableGuestbookComment
,
TableInventory
,
TableInventoryField
,
TableLists
,
TableMembers
,
TableMessage
,
TablePhotos
,
TableRoles
,
TableRooms
,
Organization
,
TableText
,
TableUserField
,
TableWeblink
,
User
,
RolesRights
,
Session
,
TableAnnouncement
,
TableCategory
,
TableDate
,
TableFile
,
TableFolder
Copyright:
2004-2016 The Admidio Team
License:
GNU General Public License v2.0 only
**********************************************************************************************
Brief:
Controls read and write access to datbase tables
This class should help you to read and write records of database tables.
You create an object for a special table and than you are able to read
a special record, manipulate him and write him back. Also new records can
be created with this class. The advantage of this class is that you are
independent from SQL. You can use @c getValue, @c setValue, @c readData
and @c save to handle the record.
Class:
TableAccess
Code:
// create an object for table adm_roles of role 4711
$roleId = 4177;
$role = new TableAccess($gDb, TBL_ROLES, 'rol', $roleId);
// read max. Members and add 1 to the count
$maxMembers = $role->getValue('rol_max_members');
$maxMembers = $maxMembers + 1;
$role->setValue('rol_max_members', $maxMembers);
$role->save(); @endcode
Par:
Examples
Located at tableaccess.php
Methods summary
public
|
#
__construct( Database & $database, string $tableName, string $columnPrefix, string|integer $id = '' )
Constructor that will create an object of a recordset of the specified table.
If the id is set than this recordset will be loaded.
Constructor that will create an object of a recordset of the specified table.
If the id is set than this recordset will be loaded.
Parameters
- $database
- Object of the class Database. This should be the default global object @b $gDb.
- $tableName
- The name of the database table. Because of specific praefixes this should be the define value e.g. @b TBL_USERS
- $columnPrefix
- The prefix of each column of that table. E.g. for table @b adm_roles this is @b rol
- $id
- The id of the recordset that should be loaded. If id isn't set than an empty object of the table is created.
|
public
string[]
|
#
__sleep( )
Called on serialization of this object. The database object could not
be serialized and should be ignored.
Called on serialization of this object. The database object could not
be serialized and should be ignored.
Returns
string[] Returns all class variables that should be serialized.
|
public
|
#
clear( )
Initializes all class parameters and deletes all read data.
Also the database structure of the associated table will be
read and stored in the arrays @b dbColumns and @b columnsInfos
Initializes all class parameters and deletes all read data.
Also the database structure of the associated table will be
read and stored in the arrays @b dbColumns and @b columnsInfos
|
protected
|
#
connectAdditionalTable( string $table, string $columnNameAdditionalTable, string $columnNameClassTable )
Adds a table with the connected fields to a member array. This table will be add to the
select statement if data is read and the connected record is avaiable in this class.
The connected table must have a foreign key in the class table.
Adds a table with the connected fields to a member array. This table will be add to the
select statement if data is read and the connected record is avaiable in this class.
The connected table must have a foreign key in the class table.
Parameters
- $table
- Database table name that should be connected. This can be the define of the table.
- $columnNameAdditionalTable
- Name of the column in the connected table that has the foreign key to the class table
- $columnNameClassTable
- Name of the column in the class table that has the foreign key to the connected table
Code
// Constructor of adm_dates object where the category (calendar) is connected
public function __construct(&$db, $dat_id = 0)
{
$this->connectAdditionalTable(TBL_CATEGORIES, 'cat_id', 'dat_cat_id');
parent::__construct($db, TBL_DATES, 'dat', $dat_id);
} @endcode
Par
Examples
|
public
integer
|
#
countAllRecords( )
Reads the number of all records of this table
Reads the number of all records of this table
Returns
integer Number of records of this table
|
public
true
|
#
delete( )
Deletes the selected record of the table and initializes the class
Deletes the selected record of the table and initializes the class
Returns
true Returns @b true if no error occurred
|
public
mixed
|
#
getValue( string $columnName, string $format = '' )
Get the value of a column of the database table.
If the value was manipulated before with @b setValue than the manipulated value is returned.
Get the value of a column of the database table.
If the value was manipulated before with @b setValue than the manipulated value is returned.
Parameters
- $columnName
- The name of the database column whose value should be read
- $format
For date or timestamp columns the format should be the date/time format e.g. @b d.m.Y = '02.04.2011'. @n
For text columns the format can be @b database that would return the original database value without any transformations
Returns
mixed Returns the value of the database column.
If the value was manipulated before with @b setValue than the manipulated value is returned.
See
TableAccess#setValue
|
public
boolean
|
#
hasColumnsValueChanged( )
If a column of the row in this object has changed throw setValue then this method
will return @b true otherwise @false
If a column of the row in this object has changed throw setValue then this method
will return @b true otherwise @false
Returns
boolean Returns @b true if at least one value of one column has changed
after the recordset was loaded otherwise @b false
|
public
boolean
|
#
isNewRecord( )
If the recordset is new and wasn't read from database or was not stored in database
then this method will return true otherwise false
If the recordset is new and wasn't read from database or was not stored in database
then this method will return true otherwise false
Returns
boolean Returns @b true if record is not stored in database
|
protected
boolean
|
#
readData( string $sqlWhereCondition )
Reads a record out of the table in database selected by the conditions of the param @b $sqlWhereCondition out of the table.
If the sql will find more than one record the method returns @b false.
Per default all columns of the default table will be read and stored in the object.
Reads a record out of the table in database selected by the conditions of the param @b $sqlWhereCondition out of the table.
If the sql will find more than one record the method returns @b false.
Per default all columns of the default table will be read and stored in the object.
Parameters
- $sqlWhereCondition
- Conditions for the table to select one record
Returns
boolean Returns @b true if one record is found
See
TableAccess#readDataById
TableAccess#readDataByColumns
|
public
boolean
|
#
readDataById( integer|string $id )
Reads a record out of the table in database selected by the unique id column in the table.
Per default all columns of the default table will be read and stored in the object.
Reads a record out of the table in database selected by the unique id column in the table.
Per default all columns of the default table will be read and stored in the object.
Parameters
- $id
- Unique id of id column of the table.
Returns
boolean Returns @b true if one record is found
See
TableAccess#readData
TableAccess#readDataByColumns
|
public
boolean
|
#
readDataByColumns( array $columnArray )
Reads a record out of the table in database selected by different columns in the table.
The columns are commited with an array where every element index is the column name and the value is the column value.
The columns and values must be selected so that they identify only one record.
If the sql will find more than one record the method returns @b false.
Per default all columns of the default table will be read and stored in the object.
Reads a record out of the table in database selected by different columns in the table.
The columns are commited with an array where every element index is the column name and the value is the column value.
The columns and values must be selected so that they identify only one record.
If the sql will find more than one record the method returns @b false.
Per default all columns of the default table will be read and stored in the object.
Parameters
- $columnArray
- An array where every element index is the column name and the value is the column value
Returns
boolean Returns @b true if one record is found
See
TableAccess#readData
TableAccess#readDataById
Code
// reads data not be mem_id but with combination of role and user id
$member = new TableAccess($gDb, TBL_MEMBERS, 'rol');
$member->readDataByColumn(array('mem_rol_id' => $roleId, 'mem_usr_id' => $userId)); @endcode
Par
Examples
|
public
boolean
|
#
save( boolean $updateFingerPrint = true )
Save all changed columns of the recordset in table of database. Therefore the class remembers if it's
a new record or if only an update is necessary. The update statement will only update the changed columns.
If the table has columns for creator or editor than these column with their timestamp will be updated.
Save all changed columns of the recordset in table of database. Therefore the class remembers if it's
a new record or if only an update is necessary. The update statement will only update the changed columns.
If the table has columns for creator or editor than these column with their timestamp will be updated.
Parameters
- $updateFingerPrint
Default @b true. Will update the creator or editor of the recordset
if table has columns like @b usr_id_create or @b usr_id_changed
Returns
boolean If an update or insert into the database was done then return true, otherwise false.
|
public
|
#
setArray( array $fieldArray )
The method requires an array with all fields of one recordset of the table object.
These fields will be add to the object as if you read one record with @b readDataById
but without a separate SQL. This method is useful if you have several recordsets of the
table and want to use an table object for each recordset. So you don't have to do an
separate sql read for each record. This is a performant way to fill the object with
the necessary data.
The method requires an array with all fields of one recordset of the table object.
These fields will be add to the object as if you read one record with @b readDataById
but without a separate SQL. This method is useful if you have several recordsets of the
table and want to use an table object for each recordset. So you don't have to do an
separate sql read for each record. This is a performant way to fill the object with
the necessary data.
Parameters
- $fieldArray
An array with all fields and their values of the table. If the object has
more connected tables than you should add the fields of these tables, too.
Code
// read all announcements with their categories
$sql = 'SELECT * FROM adm_announcements, adm_categories WHERE ann_cat_id = cat_id';
$announcementsStatement = $gDb->query($sql);
$announcement = new TableAnnouncements($gDb);
while ($row = $announcementsStatement->fetch())
{
// add each recordset to an object without a separate sql within the object
$announcement->clear();
$announcement->setArray($row);
...
} @endcode
Par
Examples
|
public
|
#
setDatabase( Database & $database )
Set the database object for communication with the database of this class.
Set the database object for communication with the database of this class.
Parameters
- $database
- An object of the class Database. This should be the global $gDb object.
|
public
boolean
|
#
setValue( string $columnName, mixed $newValue, boolean $checkValue = true )
Set a new value for a column of the database table. The value is only saved in the object.
You must call the method @b save to store the new value to the database. If the unique key
column is set to 0 than this record will be a new record and all other columns are marked as changed.
Set a new value for a column of the database table. The value is only saved in the object.
You must call the method @b save to store the new value to the database. If the unique key
column is set to 0 than this record will be a new record and all other columns are marked as changed.
Parameters
- $columnName
- The name of the database column whose value should get a new value
- $newValue
- The new value that should be stored in the database field
- $checkValue
- The value will be checked if it's valid. If set to @b false than the value will not be checked.
Returns
boolean Returns @b true if the value is stored in the current object and @b false if a check failed
See
TableAccess#getValue
|