Version
Version
Manages a connection to the database and stores information about its table structure
$DbManager : \DbManager
$DEV_MODE : boolean
Development mode toggle. When true, does extra verifications (ie data types, columns, etc) that are a must for development, but may slow things down a bit when running in production.
In DEV mode, extra type checking takes place to verify you're setting values correctly. This helps with mostly with debugging, but also helps to avoid data corruption if you do something wrong. Once things seem to be up and going pretty well, you can set this to false for some performance improvement.
$DefaultTimezone : string
$XmlSchemaDateModified : \date
The date the XML file used in LoadXmlSchema() was modified. If multiple XML Schemas are loaded, uses the latest of all.
Useful for serializing objects, storing them somewhere ($_SESSION, memcached, etc), then retrieving without needing to completely rebuild the database from xml
__construct(array $options = null, string $deprecated = null) : \SmartDatabase
Constructor for a new SmartDatabse object. Note that you can cache these objects within Memcached so we don't have to parse the XML and create the structure for every request (@see SmartDatabase::GetCached()).
$options = array(
'db-manager' => null, //DbManager - The DbManager instance that will be used to perform operations on the actual database. If null, no database operations can be performed (use for 'in-memory' instances)
'xml-schema-file-path' => null, //string - the database schema file to load. If left null, you will need to either build the database yourself useing ->AddTable() or load a schema from XML using ->LoadSchema()
'dev-mode' => true, //boolean - development mode toggle. When true, does extra verifications (ie data types, columns, etc) that are a must for development, but may slow things down a bit when running in production.
'dev-mode-warnings' => true, //boolean. if true and in $DEV_MODE, warnings will be shown for like missing classes and etc.
'default-timezone' => '' //string (i.e. "America/Indiana/Indianapolis"). if set, sets the DefaultTimezone to this value. using "date_default_timezone_get()" will use php's default date timezone that is currently set and can be changed with a call to "date_default_timezone_set(...);". empty will use default system time (not recommended). Ref: http://php.net/manual/en/timezones.php
)
array | $options | [optional] see description above |
string | $deprecated | [optional] [deprecated] Use $options instead. This used to be the 'xml-schema-file-path' option |
GetTable(string $tableName) : \SmartTable
Returns the Table instance matching the given $tableName. An exception is thrown if the table does not exist. Shortcut: use array notation- $databse['YOUR_TABLE_NAME']
string | $tableName | The name of the table to get. |
The Table instance matching the given $tableName. An exception is thrown if the table does not exist.
AddTable(\SmartTable $Table)
Adds a table to be managed by this Database. Replaces any table with the same name
\SmartTable | $Table |
LoadXmlSchema(string $xmlSchemaFilePath, array $options = null)
Loads a database schema from an XML file. This schema will replace any tables that are currently being managed by the Database instance.
$options is an assoc-array, as follows:
$options = array(
'clear-current-schema'=false, //if set to true, all tables currently loaded within this instance will be removed (unmanaged. the actual table still exists in the real db). if false, the given $xmlSchemaFilePath is simply added to whatever is currently in this database instance (tables/properties with the same name are overwritten with the new schema)
);
string | $xmlSchemaFilePath | The path of the XML database schema file to load. |
array | $options | [optional] See description |
WriteXmlSchema(string $xmlSchemaFilePath = null) : string
Gets the current database schema in XML format and optionally writes the XML to a file (specified in $xmlSchemaFilePath). The XML string is returned regardless.
string | $xmlSchemaFilePath | [optional] If provided, the new XML will overwrite the file at the specified path |
The current database schema in XML format
SyncStructureToDatabase(boolean $printResults = false, array $options = null) : string
Synchronizes the structure of this Database instance to the SQL database connection in the DbManager
$options = array(
'debug-mode' => false, //if true, prints all Sync SQL instead of executing it
'backup-tables' => true, //if true, creates a backup table before altering any existing tables
'create' => true, //if true, tables and columns will be created if they do not exist
'update' => true, //if true, existing SQL columns will be updated to match properties defined in the SmartDatabase
'delete' => true, //if true, columns that do not exist on a SmartDb managed table will be removed (note: unmanaged TABLES are never deleted!)
);
boolean | $printResults | [optional] If true, the results will be printed to the screen. (Results are returned regardless.) |
array | $options | [optional] See description above |
The results of the sync
ReadDatabaseStructure(array $options = null)
Reads the current connected database's structure ($this->DbManager)
$options = array(
'preserve-current' => false, //if true, the current smart database structure will be preserved. existing tables/column will be overwritten by the db definitions
'ignore-table-prefix' => 'backup_', //will not import tables that start with the given prefix
'table' => null, //string - if provided, will only read the structure of the given table name
)
array | $options |
GetCached(array $options = null) : \SmartDatabase
An alternative to calling "new SmartDatabase($options)" - uses memcached to get/save an entire SmartDb structure within cache. You must pass in at least the 'memcached-key' and 'xml-schema-file-path' options. Also, the returned SmartDb will not have it's DbManager set unless you pass the 'db-manager' option, so you may need to set it manually.
//options are same as SmartDatabase constructor, plus a few extra for memcached
$options = array(
'db-manager' => null, //DbManager - The DbManager instance that will be used to perform operations on the actual database. If null, no database operations can be performed (use for 'in-memory' instances)
'xml-schema-file-path' => null, //string - REQUIRED for caching - the database schema file to load. If left null, you will need to either build the database yourself useing ->AddTable() or load a schema from XML using ->LoadSchema()
'default-timezone' => '' //if set, $SmartDb->DefaultTimezone will be set to this value
'dev-mode' => true, //boolean - development mode toggle. When true, does extra verifications (ie data types, columns, etc) that are a must for development, but may slow things down a bit when running in production.
'dev-mode-warnings' => true, //boolean. if true and in $DEV_MODE, warnings will be shown for like missing classes and etc.
'memcached-key' => null, //string. REQUIRED for caching. memcached lookup key
'memcached-host' => 'localhost', //string. memcached connection host
'memcached-port' => 11211, //int. memcached connection port
'memcached-timeout' => 250, //int. failover fast-ish
'memcached-serializer' => Memcached::SERIALIZER_IGBINARY, //a much better serializer than the default one in PHP
'memcached-compression' => true, //actually makes things faster too
)
array | $options | [optional] see description above |
BuildInheritedTables(string $tableName, array $allInheritedTables, array $queuedTables, array $completedTables) :
Makes sure inherited tables get built in the correct order. Also assures there are no "inheritance loops"
string | $tableName | |
array | $allInheritedTables | |
array | $queuedTables | [optional] Leave empty. Only used for recursions. |
array | $completedTables | [optional] Leave empty. Only used for recursions. |
GetAssocFromXml(string $xmlPath, string $xsdFilePath = null) : array
parses the XML file into a structured assoc array
string | $xmlPath | path of the xml file to parse |
string | $xsdFilePath | path of the xsd schema file for verifying the xml is valid |
parses the XML file into a structured assoc array
IsXmlFileValid(string $xmlFilePath, string $schemaFilePath) : boolean
Returns true if the XML file given in the constructor of this class validates with the schema, false if the XML is invalid.
string | $xmlFilePath | path of the xml file to parse |
string | $schemaFilePath | path of the xsd schema file for verifying the xml is valid |
true if the XML file given in the constructor of this class validates with the schema, false if the XML is invalid.