A secure, multiple database ORM for PHP
First, check if you meet all of the prerequisites, then install
- Install
MySQL
/MariaDB
and configure it - In the file
php.ini
(if it doesn't exists rename the filephp.ini-development
tophp.ini
) remove the;
before theextension=pdo_mysql
(it's probably in the line 939)
- Install
PostgreSQL
and configure it or crete aPostgreSQL
database on Heroku - In the file
php.ini
(if it doesn't exists rename the filephp.ini-development
tophp.ini
) remove the;
before theextension=pdo_pgsql
(it's probably in the line 942)
- Install
SQLite3
with the following command:sudo apt-get install sqlite3
To install you need to:
- execute the following command to install with Composer:
composer require connection/connection
- execute the following command to install with Git:
git clone https://github.com/TiagoCavalcanteTrindade/Connection
sample.php
:
<?php
require_once 'env.php';
require_once 'vendor/autoload.php';
# initialize the database
$conn = new Connection\Connection();
# create the table `posts` with the fields `title` and `text`
$conn->table('posts')
->create()
->columns([
'title' => 'TEXT',
'text' => 'TEXT'
])
->run();
for ($i = 0; $i <= 9; $i++) {
# insert into the table `posts`
$conn->table('posts')
->insert()
->what('title', 'text')
->values('Title', 'Text')
->run();
}
# go through the array of results
foreach ($conn->table('posts')->select()->what('title', 'text')->run() as $result) {
# echo the `title` and the `text` of a post
echo "Title: {$result['title']}\nText: {$result['text']}\n";
}
# colse the connection (necessary for security)
$conn->close();
?>
env.php
:
<?php
putenv('name=SQLite');
putenv('database=database.sqlite3');
?>
And to execute:
php sample.php
Before init you need to:
- execute the following command:
composer install
- add a
env file
:- all
envs
need to be defined withputenv
, e.g.:putenv('name=SQLite')
- all
envs
are case sensitive - all
env file
need to have theenv
name, its possible values are:MySQL
(for MySQL and MariaDB),PgSQL
andSQLite
- all
- all
env file
need to have theenv
database - the specific
envs
for each database are:MySQL
/MariaDB
: host, port, user and passwordPgSQL
: host, port, user and password
- grant perssion to execute
phpunit
with the following command:chmod 777 vendor/bin/phpunit
- grant permission to execute the testing script with the following command:
chmod 777 tests.sh
- Execute all tests:
- you don't need to edit
env.php
for every database, it's autmated by a script, you just need to create a file nameddatabase.env.php
for eachdatabase
- execute the script:
./test.sh
- you don't need to edit
- Execute
autoload.php
tests:- execute the following command:
./vendor/bin/phpunit tests/AutoloadFileTest.php
- execute the following command:
- Execute
Connection
tests for specific database:- add
env.php
file - execute the following command:
./vendor/bin/phpunit tests/ConnectionClassTest.php
- add
Access the documentation