Database

Phork was built using MySQL, although it can be extended to use numerous other SQL databases. In order to add another database class it must implement the Sql interface and extend the DatabaseAdaptor class. To use a new database class change $arrDatabase['Type'] to the new class name in the database configuration.

The bootstrap automatically sets up the database using the DatabaseFactory class. The database classes are in phork/php/database/. The database configuration file is used to define usernames, password, hosts, database names, etc. for the read and write databases.

Query Generator

Each database class should come with a corresponding query generator class (eg. MySqlQuery). Using the query generator isn't required, but it handles things like escaping strings automatically and helps make the application more portable.


Example Read Query

//connect to the database and set it up for reads
$objDb = AppRegistry::get('Database');
if ($objDb->initRead(true)) {

    //run the select query
    if (($mxdResult = $objDb->read("SELECT * FROM mytable LIMIT 10")) !== false) {
        while ($arrRow = $objDb->fetchRow($mxdResult)) {

            //do something with $arrRow here
        }
        $objDb->freeResult($mxdResult);
    }
}

Example Write Query

//connect to the database and set it up for writes
$objDb = AppRegistry::get('Database');
if ($objDb->initWrite(true)) {

    //run the insert query
    if (($mxdResult = $objDb->write("INSERT INTO mytable (foo, bar) VALUES ('abc', 'def')")) !== false) {
        
        //make sure a result was returned and get the inserted ID
        if ($blnResult = ($mxdResult != false)) {
            $intId = $objDb->getInsertedId();
        }

        $objDb->freeResult($mxdResult);
        return $blnResult;
    }
}

Example Test Query

//connect to the test database
$objDb = AppRegistry::get('Database');
if ($objDb->initConnection('Test', true)) {

    //run the select query
    if (($mxdResult = $objDb->read("SELECT * FROM mytable LIMIT 10")) !== false) {
        while ($arrRow = $objDb->fetchRow($mxdResult)) {

            //do something with $arrRow here
        }
        $objDb->freeResult($mxdResult);
    }
}