This is an implementation of the Sgss_Collection_Map_Interface using database.
The database schema to be used with this class requires two columns of text datatype. One column represents the keys in this map, and another represents the values associated with those keys. The key column must have either primary key or unique constraint to avoid duplication.
Keys and values will be serialized when stored into database, so that those retain its datatype or class when retrieved. Therefore, any elements (keys and values) must be serializable, in other words, not contain any resource object. The columns must have enough length to store serialized elements.
A simple query to create such table follows:
CREATE TABLE table_name (
key_column_name TEXT PRIMARY KEY
value_column_name TEXT
);The first parameter of the constructor specifies the database to be used with this map. This can be either config array or PDO object. The second parameter specifies table name, the third and fourth specifies name of the columns to store keys and values.
<?phprequire_once 'Sgss/Collection/Pdo/Map.php';array('dns' => 'mysql:dbname=testdb;host=127.0.0.1','username' => 'dbuser','password' => 'dbpass'),'table_name', 'key_column_name', 'value_column_name');is equivalent to:
<?phprequire_once 'Sgss/Collection/Pdo/Map.php';$db = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'dbuser', 'dbpass');$db, 'table_name', 'key_column_name', 'value_column_name');Do not specify quoted name in those parameters because all the names will be automatically quoted, otherwise it will indicate different table or column from what you intended.
Sgss_Collection_Map_Abstract
+ Sgss_Collection_Pdo_Map
|
$_config
Config to connect to the database |
|
$_db
Database handle used with this map |
|
$_driver
Driver name of the database handle |
|
$_isPrepared
Represents whether the prepared statements were created |
|
$_key
Name of the column containing keys that already exists in the database |
|
$_table
Name of the table that already exists in the database |
|
$_value
Name of the column containing values that already exists in the database |
|
__construct (array|PDO $config, string $table, string $key, string $value)
The constructor |
boolean |
clear ()
Removes all the keys and the associated values in this map |
mixed|null |
get (mixed $key)
Returns the value associated with the specified key |
boolean |
has (mixed $key)
Determines whether this map contains the associated value with the specified key |
boolean |
hasValue (mixed $value)
Determines whether this map contains the specified value |
boolean |
put (mixed $key, mixed $value)
Inserts or replaces the association between the specified key and value into this map |
boolean |
remove (mixed $key)
Removes the specified key and the associated value from this map |
int |
size ()
Returns the number of associations between key and value in this map |
array |
toArray ()
Converts this map to an associated array |
array |
toKeyArray ()
Returns an array containing all the keys in this map |
array |
toValueArray ()
Returns an array containing all the values in this map |
|
_connect ()
Connects to the database |
|
_invalidateStatements ()
Marks the prepared statements as no longer working |
|
_prepareStatements ()
Creates the prepared statements used with the database handle |
|
_quoteName (string $name)
Quotes the specified table or column name |
|
_sopite (mixed $obj, [boolean $key = false])
Converts the specified object to the corresponding scalar value |
|
_waken (scalar $scalar, [boolean $key = false])
Converts the specified scalar value to the corresponding object |
array |
__sleep ()
|
void |
__wakeup ()
|
Config to connect to the database
protected array $_config = array(...)
Database handle used with this map
protected PDO $_db
Driver name of the database handle
protected string $_driver = ''
Represents whether the prepared statements were created
protected boolean $_isPrepared = false
Name of the column containing keys that already exists in the database
protected string $_key
Name of the table that already exists in the database
protected string $_table
Name of the column containing values that already exists in the database
protected string $_value
The constructor
array|PDO $config
Config to connect to the database, or PDO object used with the map
string $table
Name of the table that already exists in the database
string $key
Name of the column containing keys that already exists in the database
string $value
Name of the column containing the values that already exists in the database
public __construct (array|PDO $config, string $table, string $key, string $value)
Removes all the keys and the associated values in this map
public boolean clear ()
Returns the value associated with the specified key
mixed $key
Key associated with the value to be retrieved
public mixed|null get (mixed $key)
Determines whether this map contains the associated value with the specified key
mixed $key
Key to determine
public boolean has (mixed $key)
Determines whether this map contains the specified value
mixed $value
Value to determine
public boolean hasValue (mixed $value)
Inserts or replaces the association between the specified key and value into this map
mixed $key
Key to be inserted
mixed $value
Value to be inserted and associated with the key
public boolean put (mixed $key, mixed $value)
Removes the specified key and the associated value from this map
mixed $key
Key associated with the value to be removed
public boolean remove (mixed $key)
Returns the number of associations between key and value in this map
public int size ()
Converts this map to an associated array
public array toArray ()
Returns an array containing all the keys in this map
public array toKeyArray ()
Returns an array containing all the values in this map
public array toValueArray ()
Connects to the database
When unable to connect to the database
protected void _connect ()
Marks the prepared statements as no longer working
protected void _invalidateStatements ()
Creates the prepared statements used with the database handle
When unable to prepare statements
protected void _prepareStatements ()
Quotes the specified table or column name
string $name
Column or table name to be quoted
protected string _quoteName (string $name)
Converts the specified object to the corresponding scalar value
mixed $obj
Object to be converted to scalar
boolean $key
Whether the object is used as the key
When the object is or contains unconvertable to scalar (resource or internal class)
protected scalar _sopite (mixed $obj, [boolean $key = false])
Converts the specified scalar value to the corresponding object
scalar $scalar
Scalar value to be converted to object
boolean $key
Whether the converted object is to be used as the key
protected mixed _waken (scalar $scalar, [boolean $key = false])
When this map is unserializable
public array __sleep ()
public void __wakeup ()