simsoft/adodb-orm

ORM wrapper for ADOdb PHP

1.1.7 2024-11-05 20:04 UTC

README

This is an ORM wrapper for ADOdb (https://adodb.org/)

Requirements

  • PHP >= 8

Installation

  1. Install package using composer
$ composer require simsoft/adodb-orm

Getting Started

require_once "vendor/autoload.php";
require_once "vendor/adodb/adodb-php/adodb-active-recordx.inc.php";

use Simsoft\ADOdb\DB;
use Simsoft\ADOdb\Query;

$config = [
    'mysql' => [
        'driver' => 'mysqli',
        'host' => '127.0.0.1',
        'user' => 'username',
        'pass' => 'password',
        'schema' => 'db_example',
    ],
    /* Sample config
    'connection_name' => [
        'driver' => 'mysqli',
        'host' => '127.0.0.1',
        'user' => 'username',
        'pass' => 'password',
        'schema' => 'database_name',
        'debug' => true,
        'execute' => [ // run once when the connection is successful.
            "SET @@session.time_zone='+00:00'",
        ],
    ],*/
];

// Initiate DB config
DB::init($config);

$result = DB::use('mysql')->execute('SELECT * FROM table1 WHERE attr1 = ? AND attr2 = ?', ['value1', 'value2']);

while ($row = $result->fetchRow()) {
    print_r($row);
}

// Insert new record
$success = DB::use('mysql')->insert('table', ['attr1' => 'value1', 'attr2' => 'value2']);

// Update an record.
$success = DB::use('mysql')->update('table', ['attr1' => 'value1', 'attr2' => 'value2'], 'id=2');
$success = DB::use('mysql')->update('table', ['attr1' => 'value1', 'attr2' => 'value2'], Query::where('id', 2));

// Return result in array
$result = DB::use('mysql')->getArray('SELECT * FROM user WHERE name LIKE ?', ['%john%']);

// Enable debug mode
$result = DB::use('mysql')->debug()->getArray('SELECT * FROM user WHERE name LIKE ?', ['%john%']);

Basic Usage

Query Builder

require_once "vendor/autoload.php";
require_once "vendor/adodb/adodb-php/adodb-active-recordx.inc.php";

use Simsoft\ADOdb\DB;
use Simsoft\ADOdb\Query;

$config = [
    'mysql' => [
        'driver' => 'mysqli',
        'host' => '127.0.0.1',
        'user' => 'username',
        'pass' => 'password',
        'schema' => 'db_example',
    ],
];

DB::init($config);

$result = Query::db('mysql')->from('table')->where('attr1', 'value1')->where('attr2', 'value2')->execute();

while ($row = $result->fetchRow()) {
    print_r($row);
}

Active Record

namespace Model;

use Simsoft\ADOdb\ActiveRecord;

/**
 * Class User
 */
class User extends ActiveRecord
{
    /** @var string Connection name */
    public $_dbat = 'mysql';

    /** @var string table name */
    public $_table = 'user';
}
require_once "vendor/autoload.php";
require_once "vendor/adodb/adodb-php/adodb-active-recordx.inc.php";

use Simsoft\ADOdb\DB;
use Model\User;

$config = [
    'mysql' => [
        'driver' => 'mysqli',
        'host' => '127.0.0.1',
        'user' => 'username',
        'pass' => 'password',
        'schema' => 'db_example',
    ],
];

DB::init($config);

$users = User::query()->where('age', '>', 20)->where('gender', 'male')->findAll();

foreach ($users as $user) {
    echo $user->first_name;
    echo $user->last_name;
}

Documentations

  1. DB class
  2. Query Builder
  3. Active Record

License

The WeCanTrack PHP API is licensed under the MIT License. See the LICENSE file for details