PHP code example of habanero / grocery

1. Go to this page and download the library: Download habanero/grocery library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

habanero / grocery example snippets




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'),
]);



# 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);

$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']);