Stash Queries is an eloquent and expressive PHP library that simplifies database interactions.
It acts as a lightweight wrapper around PHP's native PDO (PHP Data Objects) extension, providing a fluent API to easily execute SELECT, INSERT, UPDATE, and DELETE operations using your actual SQL files.
Originally developed as the DB service for the Skyfire PHP framework, this library is specifically designed as a native PDO replacement rather than a full Object-Relational Mapper (ORM).
Note: Currently, Stash Queries supports MySQL and PostgreSQL databases. Support for additional database are up for request.
- PHP 5.6 or higher
PDO_MYSQLPHP extension
(Note: PHP 5.6 is the minimum required version for security and modern language features.)
// setting the DB display encoding type (if needed)
FixCollation::charset('utf-8', FixCollation::TEXT_HTML);
// setting Database credentials
DB::define('stash_dir', getcwd()); // or dirname(__FILE__)
DB::define('host', 'localhost');
DB::define('dbname', 'test_db1');
DB::define('dbuser', 'root');
DB::define('dbpassword', '');
// SQL select query (with prepare variables)
$prepare = array
(
'label' => 'test'
);
$data = DB::select('get.HomeTextByLabel')->prepare($prepare);
var_dump($data);
// SQL simple select query
$data = DB::select('get.AllHomeTextData')->execute();
var_dump($data);
// raw SQL query (with prepare variables)
$data = DB::query('SELECT * FROM test WHERE data IS NOT NULL AND id > :count AND data != :text', array
(
':id' => 10,
':text' => 'test'
))->execute();
var_dump($data);
// displays the prepare update statement in plain text (ideal for debugging queries)
$query = DB::update('PostfromTestById')->text($prepare);
echo $query;You can securely inject dynamic variables directly into your queries (e.g., table names or column fields) when standard PDO bindings cannot be used:
$data = DB::select('get.fieldData.byId')->inject(array
(
'field' => $data->field,
'table' => $table_name
))->prepare(array('id' => (int) $record->id));To enable persistent database connections for all queries, define the persistent configuration setting as follows:
DB::define('persistent', TRUE);
// DB::define('persistent', 'yes');You can use either the boolean TRUE or the string 'yes' to enable this feature.
If you are unsure whether your SQL query folders exist, you can call the following function to create them automatically. It returns FALSE if no directories were created, or an integer representing the number of directories created (e.g., 4).
DB::createQueryDirectories();Note: Ensure you place this function call after all DB::define() configurations.
You can conveniently create a new database if it doesn't already exist:
DB::createNotExist('database_name');Note: Ensure you place this function call after all DB::define() configurations.
Manual Installation:
To include the library manually, simply require the StashQueries.php file in your project:
require_once 'DB/StashQueries.php';Via Composer:
Alternatively, you can install the library via Composer by adding skyfirephp/db to the require section of your composer.json file:
{
"require": {
"skyfirephp/db": "dev-master"
}
}Stash Queries is open-sourced software licensed under the MIT License.
Copyright 2015-2017 Travis van der Font