PHPackages                             k-gun/oppa - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. k-gun/oppa

AbandonedArchivedLibrary[Database &amp; ORM](/categories/database)

k-gun/oppa
==========

Oppa: Database abstraction, query building and active record implementation with PHP.

3.36.0(6y ago)9971MITPHPPHP ^7.1

Since Apr 23Pushed 5y ago1 watchersCompare

[ Source](https://github.com/k-gun/oppa)[ Packagist](https://packagist.org/packages/k-gun/oppa)[ Docs](http://github.com/k-gun/oppa)[ RSS](/packages/k-gun-oppa/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (127)Used By (0)

NOTICE: This library is archived for the sake of [froq-database](https://github.com/froq/froq-database), use that library instead.

Oppa
----

[](#oppa)

Providing an easy interface, aims to simplify database CRUD operations/transactions that you tire. Oppa has also an Active Record implementation interface that sometimes make the things easier for you.

Secures user inputs sharply, carries results of your queries gently, handles errors smoothly, makes batch transactions/commits carefully and profiles your all query processes optionally for you. Oppa also provides a powerful logging mechanizm to report events that you may wonder about.

You will be enjoying while using it, promise.. :)

Before beginning;

- Set your autoloader properly
- Use PHP &gt;= 7.1 (older versions here: [v1](https://github.com/k-gun/oppa/tree/1.26.4), [v2 (7.0)](https://github.com/k-gun/oppa/tree/2.3.2))
- Use try/catch blocks
- You can use `test.sql` in test folder
- Wiki updated for v2, v3
- Supports [MySQLi](http://php.net/manual/en/book.mysqli.php) and [PgSQL](http://php.net/manual/en/book.pgsql.php)

You can see wiki pages for more doc:

### Autoloading / Using Libraries

[](#autoloading--using-libraries)

```
# composer
~$ composer require k-gun/oppa
```

```
// manual
$autoload = require('/src/Autoload.php');
$autoload->register();
```

### Config

[](#config)

```
// simply for single databases, see wiki for more
$cfg = [
   'agent'    => 'mysql',
   'database' => [
      'host'     => 'localhost',  'name'     => 'test',
      'username' => 'test',       'password' => '********',
      'charset'  => 'utf8',       'timezone' => '+00:00',
   ]
];
```

### Simple Usage

[](#simple-usage)

```
$db = new Oppa\Database($cfg);
$db->connect();

$agent = $db->getLink()->getAgent();
$agent->query('select * from `users` where `old` > ?', [25]);
dump $agent->rowsCount();
```

### Holy CRUD Stuffs

[](#holy-crud-stuffs)

```
// raw queries
$result = $agent->query('select * from `users`');
if ($result->hasData())
// if ($result->count() > 0)
   foreach ($result as $user)
      dump $user->name;

// or
if ($agent->rowsCount())
   foreach ($agent->getResult() as $user)
   // or foreach ($agent->getResult()->getData() as $user)
      dump $user->name;

// fetch one
$user = $agent->get('select * from `users` where `old` > ?', [50]);
dump $user->name;
// fetch all
$users = $agent->getAll('select * from `users` where `old` > ?', [50]);
foreach ($users as $user) {
   dump $user->name;
}

// or shorcut methods

// get one user
$result = $agent->select('users');
// get one users if old greater than 50
$result = $agent->select('users', '*', 'old > ?', [50]);
// get many users
$result = $agent->selectAll('users');
// get many users if old greater than 50
$result = $agent->selectAll('users', '*', 'old > ?', [50]);

// insert a user
$result = $agent->insert('user', ['name' => 'Ali', 'old' => 30]); // int: last insert id
// insert many users
$result = $agent->insertAll('user', [['name' => 'Ali', 'old' => 30], ...]); // int[]: last insert ids

// update a user
$result = $agent->update('user', ['old' => 30], 'id = ?', [123]); // int: affected rows
// update many users
$result = $agent->updateAll('user', ['old' => 30], 'id > ?', [123]); // int: affected rows

// delete a user
$result = $agent->delete('user', 'id = ?', [123]); // int: affected rows
// delete many users
$result = $agent->deleteAll('user', 'id > ?', [123]); // int: affected rows
```

### Query Builder

[](#query-builder)

```
// use and init with exists $db
use Oppa\Query\Builder as Query;

$query = new Query($db->getLink());
// set target table
$query->setTable('users u');

// build query
$query->select('u.*')
    ->aggregate('sum', 'us.score', 'sum_score')
    ->join('users_score us', 'us.user_id=u.id')
        ->selectMore('us.score')
    ->joinLeft('users_login ul', 'ul.user_id=u.id')
        ->selectMore('ul.login')
    ->whereIn('u.id', [1,2,3])
    ->whereBetween('u.old', [30,50])
    ->whereNotNull('ul.login')
    ->groupBy('u.id')
    ->orderBy('u.old')
    ->having('sum_score
