dev-master
9999999-devFancy database library for PHP
MIT
The Requires
- php >=5.3.4
The Development Requires
by Alvaro Cabrera
Wallogit.com
2017 © Pedro Peláez
Fancy database library for PHP
Provide a simple (and fancy) interface to work with databases, currently supports PostgreSQL (8.4+), MySQLi (5.1+) and SQLite3 (3.7+) using the same API., (*1)
Perform migrations quickly, although you must write a wrapper for this if you want to keep the track., (*2)
You can just load tables and work with them almost easily, or you could create tables directly from the code and skip the CLI or GUI to achieve that., (*4)
<?php
use Grocery\Base as DB;
# create a connection by DSN
$db = DB::connect('pgsql://postgres:test@localhost:5432/test#pdo');
# load existing table by accesing an array member
$foo = $db['my_table'];
# pick one row randomly!
$bar = $foo->select('*', [
'field' => 'value',
'OR' => [
'foo' => null,
'foo >=' => gmdate('Y-m-d H:i:s'),
], // WHERE "field" = 'value' AND ("foo" IS NULL OR "foo" >= '2023-08-22 23:45:12')
], [
'order_by' => [$db->rand()], // ORDER BY RAND|RANDOM()
]);
# create another table
$db['other_table'] = [
'id' => DB::pk(),
'title' => DB::str(['not_null' => true]),
'published_at' => DB::date(['default' => $db->now()]),
];
# inserting a new row
$db->other_table->insert([
'title' => 'Hello World!',
'published_at' => date('Y-m-d H:i:s'),
]);
There are generic types for creating new tables., (*5)
date('Y-m-d H:i:s')
Y-m-d
H:i:s
Grocery provides the hydrate() helper method for this purpose., (*6)
<?php # table fields $foo = [ 'id' => 'primary_key', 'bar' => 'string', 'candy' => 'timestamp', ]; # indexed fields $bar = ['bar', 'candy' => TRUE]; # create if not exists isset($db['tbl']) || $db['tbl'] = $foo; # performs the hydration Grocery\Helpers::hydrate($db['tbl'], $foo, $bar);
Internally it will load the current details from the specified table, then compare and update your table definitions against your provided changes., (*7)
Notice that some operations are restrictive depending on the driver limitations., (*8)
Table access is granted through the $db[...] syntax, most operations can be chained., (*9)
The last call MUST be a getter, e.g.
first(),pick(),get()orall()., (*10)
$tbl = $db['tbl'];
$tbl->where(['status' => 0])->count();
$tbl->where(['status' => -1])->delete();
$tbl->select('*')->where(['status' => 1])->all();
$tbl->order(['created_at' => 1])->where(['status' => 2])->select('*')->first();
$tbl->where(['id' => 42])->update(['value' => 'OSOM']);
Using the composer is the best way to get installed Grocery as dependency., (*11)
{
"require": {
"habanero/grocery": "dev-master"
}
}
Then include the vendor/autoload.php script at the top of your project et voila., (*12)
So far there are many ways to work with Grocery but the README is too short already., (*13)
If you want to test and write some documentation, examples, etc. you're welcome to do pull-requests., (*14)
Fancy database library for PHP
MIT