A secure, multiple database ORM for PHP
First, check if you meet all of the prerequisites, then install
MySQL
/MariaDB
and configure itphp.ini
(if it doesn't exists rename the file php.ini-development
to php.ini
) remove the ;
before the extension=pdo_mysql
(it's probably in the line 939)PostgreSQL
and configure it or crete a PostgreSQL
database on Heroku
php.ini
(if it doesn't exists rename the file php.ini-development
to php.ini
) remove the ;
before the extension=pdo_pgsql
(it's probably in the line 942)SQLite3
with the following command: sudo apt-get install sqlite3
To install you need to:
composer require connection/connection
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:
composer install
env file
:
envs
need to be defined with putenv
, e.g.: putenv('name=SQLite')
envs
are case sensitiveenv file
need to have the env
name, its possible values are: MySQL
(for MySQL and MariaDB), PgSQL
and SQLite
env file
need to have the env
database
envs
for each database are:
MySQL
/MariaDB
: host, port, user and password
PgSQL
: host, port, user and password
phpunit
with the following command: chmod 777 vendor/bin/phpunit
chmod 777 tests.sh
env.php
for every database, it's autmated by a script, you just need to create a file named database.env.php
for each database
./test.sh
autoload.php
tests:
./vendor/bin/phpunit tests/AutoloadFileTest.php
Connection
tests for specific database:
env.php
file./vendor/bin/phpunit tests/ConnectionClassTest.php
Access the documentation