PHPackages                             wscore/scoredb - 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. wscore/scoredb

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

wscore/scoredb
==============

Database Access Management with Simple DAO.

0.5.0(4y ago)040PHP

Since Aug 24Pushed 3y ago1 watchersCompare

[ Source](https://github.com/asaokamei/ScoreDB)[ Packagist](https://packagist.org/packages/wscore/scoredb)[ RSS](/packages/wscore-scoredb/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (9)Used By (0)

ScoreDB
=======

[](#scoredb)

A simple Database Access Manager and Data Access Object class.

This package provides a quick access for

- ExtendedPdo and ConnectionPools from AuraPHP/Sql package,
- slick query composition using ScoreSql, and
- a simple Dao (Database Access Object).

### license

[](#license)

MIT License

Sample Usage
------------

[](#sample-usage)

### configuration

[](#configuration)

Uses static `DB` class as a gateway (aka facade) to database manager (`DbAccess` class).

Use `DB::config()` method to configure the db connection.

```
DB::config( [
    'dsn'  => 'mysql:host=localhost;dbname=name;charset=utf',
    'user' => 'username',
    'pass' => 'password',
    'option' => [],
    'attribute' => []
] );
```

Please refer to Aura/Sql components for details of the each configuration.

Specify `for` in the configuration to connect to different databases for reading and writing.

```
DB::config( [
    'dsn'  => '...',
] );
DB::config( [
    'dsn'  => '...',
    'for'  => 'write',
] );
```

### get connection (i.e. Pdo)

[](#get-connection-ie-pdo)

getting the Pdo object for db connection. The `ExtendedPdo`in Aura/Sql is returned.

```
$pdo = DB::connect();
$pdo2 = DB::connectWrite();
```

returns the connection for reading if write connection is not set.

### get named connection

[](#get-named-connection)

Configure different database connection using names.

```
DB::config( 'log', [
    'dsn' => 'mysql',...
] );
// then get PDO as:
$pdo = DB::connect( 'log' );
```

Querying Database
-----------------

[](#querying-database)

Use `DB::query()` to get a query object to access database. Please refer to ScoreSql to find out how to manipulate the query.

```
$result = DB::query( 'myTable' )
    ->connect( 'conn' )
    ->where( DB::given( 'status' )->is( 5 ) )
    ->select();
```

The `connect()` maybe omitted if the default connection is used.

### EntityObject

[](#entityobject)

As default, the Dao object returns the found database record as `EntityObject` class. This class has a reference to the Dao object to provide many useful functions while keeping the code base to a minimum size.

some functions are:

- mutating values to an object (or back).
- accessing data as a property, or as array.
- finding the modified data.
- get the primary key.
- get the relation objects.

### ActiveRecord

[](#activerecord)

Dao can return object as ActiveRecord class, as

```
class YourDao extends Dao
{
    protected $fetch_class = 'WScore\ScoreDB\Entity\ActiveRecord';
}
```

The ActiveRecord class will provide more functions added to the EntityObject described above, such as,

- save to database.
- delete itself from database.
- immunize the access to database.

Data Access Object
------------------

[](#data-access-object)

### sample dao class

[](#sample-dao-class)

Extend `Dao` class.

```
/**
 * @method User status( $status=1 )
 */
class User extends Dao
{
    protected $table = 'dao_user';
    protected $keyName = 'user_id';
    protected $timeStamps = [
        'created_at' => [
            'created_at',
            'open_date' => 'Y-m-d'
        ],
        'updated_at' => [
            'updated_at'
        ],
    ];

    /**
     * @param int $status
     */
    public function scopeActive() {
        $this->where( $this->status->is( 1 ) );
    }
}
```

##### table name and primary key

[](#table-name-and-primary-key)

specify table name as `class::$table`, and primary key name as `class::$keyName`.

If these are not set, class name is used as table name, and `tableName_id` is used as key name.

##### timestamps

[](#timestamps)

Use `class::$timeStamps` to indicate stamps: `created_at` for at the creation of data, and `updated_at` for updating and creation time.

Specify the date format if different

##### using DaoTrait

[](#using-daotrait)

alternatively, use DaoTrait to create dao object using other query class. as such,

```
class Other extends OtherQuery
{
    user DaoTrait;
}
```

### accessing database

[](#accessing-database)

Use $dao object just like a Query object in WScore.SqlBuilder.

##### selecting:

[](#selecting)

```
// list all users with status=1.
$found = $user->where(
    $user->status->is(1)
)->select();
```

or use $dao object as an iterator.

```
$users = User::forge();
foreach( $users as $user ) {
    echo $user->name;
}
```

##### updating:

[](#updating)

```
// update active people to status=2.
$user->active()->update('status'=>2);
```

or,

```
$user->status = 2;
$user->active()->update();
```

##### inserting:

[](#inserting)

```
$user->insert( [ 'name' => 'bob', 'status'=>0 ] );
```

or,

```
$user->name = 'bob';
$user->status = 0;
$user->insert();
```

Scopes and Events in Dao
------------------------

[](#scopes-and-events-in-dao)

### scopes

[](#scopes)

Scopes are functions starting with `scope`. In the scope function, influence the query to get what is desired.

```
$user = User::forge();
// use $dao as iterator.
foreach( $user->active() as $applied ) {
    echo $applied->name;
}
```

### hooks

[](#hooks)

Hook methods starts with `on`, event name, and end with `Hook`.

```
class User extends
    public function on{EventName}Hook( $data ) {
    }
}
```

### filters

[](#filters)

Filter methods starts with `on`, event name, and end with `Filter`.

Filters are for modifying the input or output; make very sure that filters return what is given (or modified value) or nothing will happen.

```
class User extends
    public function on{EventName}Filter( $data ) {
        return $data;
    }
}
```

### hook objects

[](#hook-objects)

The Dao class may become too large with lots of scopes and event hooks. To simplify the dao class, these methods can be transferred to another object (hook object) using `setHook()` method, such as:

```
// $hookObject has the events and scopes.
$user->setHook( $hookObject );
```

### available events

[](#available-events)

whenever accessing database, start with `~ing`, and followed by `~ed`.

- selecting, selected,
- loading, loaded,
- counting, counted,
- inserting, inserted,
- updating, updated,
- deleting, deleted,

hidden (or already used) events:

- createStamp, updateStamp:

for adding timestamps to data when inserting or updating data.

Relation
--------

[](#relation)

ScoreDB provides simple relationship classes as,

- HasOne,
- HasMany, and
- HasJoin.

### Using Relation

[](#using-relation)

In your Dao class, create methods like'

```
class YourDao extends Dao
{
    public function getTargetsRelation() {
        return Relation::HasOne( $this, 'TargetDaoName' );
    }
}
$dao     = new YourDao();
$entity  = $dao->find($id);

// get target record using HasOne relation.
$targets = $dao->getTargetsRelation()->entity( $entity )->get();

// or use EntityObject's magic method.
$targets = $entity->targets->get();
```

to set new relation,

```
$entity->targets->link( $targetEntity );
```

WISHES WISHES WISHES
====================

[](#wishes-wishes-wishes)

### transaction

[](#transaction)

for transaction, use the Pdo (ExtendedPdo)'s transaction method.

```
DB::db()->transaction( function() {
    // do database access.
} );
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~700 days

Recently: every ~875 days

Total

6

Last Release

1141d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8ed783829e6fa0bd4b0def8c04ccfdfb2fc99f9e61e4a9470acad9e5abc5fcac?d=identicon)[asaokamei](/maintainers/asaokamei)

---

Top Contributors

[![asaokamei](https://avatars.githubusercontent.com/u/747030?v=4)](https://github.com/asaokamei "asaokamei (310 commits)")

### Embed Badge

![Health badge](/badges/wscore-scoredb/health.svg)

```
[![Health](https://phpackages.com/badges/wscore-scoredb/health.svg)](https://phpackages.com/packages/wscore-scoredb)
```

###  Alternatives

[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k578.4M5.6k](/packages/doctrine-dbal)[cycle/database

DBAL, schema introspection, migration and pagination

64690.9k31](/packages/cycle-database)[guikingone/scheduler-bundle

A Symfony bundle that allows to schedule and create repetitive tasks

114217.4k](/packages/guikingone-scheduler-bundle)[symfony/ai-store

Low-level abstraction for storing and retrieving documents in a vector store.

19292.4k53](/packages/symfony-ai-store)[tommyknocker/pdo-database-class

Framework-agnostic PHP database library with unified API for MySQL, MariaDB, PostgreSQL, SQLite, MSSQL, and Oracle. Query Builder, caching, sharding, window functions, CTEs, JSON, migrations, ActiveRecord, CLI tools, AI-powered analysis. Zero external dependencies.

845.7k](/packages/tommyknocker-pdo-database-class)[bartlett/php-compatinfo-db

Reference Database of all functions, constants, classes, interfaces on PHP standard distribution and about 110 extensions

1183.0k1](/packages/bartlett-php-compatinfo-db)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
