PHPackages                             attwframework/db - 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. attwframework/db

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

attwframework/db
================

Database component of AttwFramework

09PHP

Since Oct 6Pushed 11y ago1 watchersCompare

[ Source](https://github.com/AttwFramework/db)[ Packagist](https://packagist.org/packages/attwframework/db)[ RSS](/packages/attwframework-db/feed)WikiDiscussions master Synced 6d ago

READMEChangelogDependenciesVersions (1)Used By (0)

DB
==

[](#db)

[![Total Downloads](https://camo.githubusercontent.com/da743388e69229dc653032ba003d20dbab69e6c22bc93e4bb1f5d8d41a16ad24/68747470733a2f2f706f7365722e707567782e6f72672f617474776672616d65776f726b2f64622f646f776e6c6f6164732e706e67)](https://packagist.org/packages/attwframework/db) [![Latest Unstable Version](https://camo.githubusercontent.com/f35d9fa6eea2f492b9bc8a0f4245d9e5bc7280f0a226882782c334681013d588/68747470733a2f2f706f7365722e707567782e6f72672f617474776672616d65776f726b2f64622f762f756e737461626c652e706e67)](https://packagist.org/packages/attwframework/db) [![License](https://camo.githubusercontent.com/38fa1e971acc9250966c155628268d51480b96dce14c6286bb17b9cc1299bad6/68747470733a2f2f706f7365722e707567782e6f72672f617474776672616d65776f726b2f64622f6c6963656e73652e706e67)](https://packagist.org/packages/attwframework/db)

Database component of [AttwFramework](https://github.com/attwframework/framework).

\##Composer ###Download

```
{
    "require": {
        "attwframework/db": "dev-master"
    }
}
```

\##Support ###Database

- [MySQL](http://www.mysql.com/)

\###Driver

- [PDO](http://www.php.net/manual/en/book.pdo.php)
- [MySQLi](https://php.net/manual/pt_BR/book.mysqli.php)

\##How to use ###Connecting with a relational database Create an object to be the connector, passing the instance of connector configurations in the constructor

```
use Attw\Db\Connection\PDOConnector;

$connector = new PDOConnector('mysql:host=localhost;dbname=test', 'root', 'pass');
```

Connections collection

```
use Attw\Db\Connection\Connector\Config\MySQLConnectionConfig;
use Attw\Db\Connection\PDOConnector;
use Attw\Db\Collection as DBCollection;

$connector = new PDOConnector('mysql:host=localhost;dbname=test', 'root', 'pass');

$connections = DBCollection::getInstance();
$connections->add('ConnectionName', $connector);
```

The default methods of a connector are:

- `Attw\Db\Connection\ConnectorInterface::getConnection()` Returns the connection
- `Attw\Db\Connection\ConnectorInterface::getDriver()` Returns the connected driver
- `Attw\Db\Connection\ConnectorInterface::query($sql)` Execute a SQL query
- `Attw\Db\Connection\ConnectorInterface::prepare($sql)` Prepares a statement for execution and returns a statement object
- `Attw\Db\Connection\ConnectorInterface::exec($sql)` Execute an SQL statement and return the number of affected rows
- `Attw\Db\Connection\ConnectorInterface::lastInsertId([ string $name ])` Returns the ID of the last inserted row or sequence value
- `Attw\Db\Connection\ConnectorInterface::getStatement( $sql )` Returns statement class

\###Storage methods Methods useds for interaction with database

Crud: ####Inserting something

```
use Attw\Db\Connection\PDOConnector;
use Attw\Db\Storage\Storage;
use Attw\Db\Sql\MySQL;

$connector = new PDOConnector('mysql:host=localhost;dbname=test', 'root', 'pass');
$storage = new Storage($connector, new MySQL());
$storage->create('users', array(
    'name' => 'Gabriel Jacinto',
    'email' => 'gamjj74@hotmail.com',
    'age' => 15,
    'gender' => 'male'
))->execute();
```

\####Updating something

```
$storage->update('users', array('name' => 'Gabriel Jacinto'), array('id' => 17))
    ->execute();
```

\####Deleting something

```
$storage->remove('users', array('id' => 17))
    ->execute();
```

\####Selecting somethig

```
$stmt = $storage->select('users')->where(array('id' => 17));
$stmt->execute();
print_r($stmt->fetch());
```

\####Counting results

```
$stmt = $storage->select('users')->where(array('id' => 17));
$stmt->execute();
$total = $stmt->rowCount();
```

\####Executing a SQL query

```
$connector->query("DELETE FROM `users` WHERE `id` = '20'");
```

\####Prepared statements

```
use Attw\Db\Connection\PDOConnector;
use Attw\Db\Storage\Storage;
use Attw\Db\Sql\MySQL;

$connector = new PDOConnector('mysql:host=localhost;dbname=test', 'root', 'pass');

$stmt = $connector->getStatement("SELECT * FROM `users` WHERE `id` = ?");
$stmt->execute(array(20));
$userData = $stmt->fetch();
```

\###Entities Entities are classes that represent tables. To create an entity, create a class that extends `Attw\Db\Storage\Entity\AbstractEntity`. The configurations of an entity must be on a property called `$_configs` as an array. If a column of the table represents a registry of other table, create an index `entities` like the example. And if a column represents a datetime column, create an index `datetime`:

```
namespace Your\Namespace\Entity;

use Attw\Db\Storage\Entity\AbstractEntity;

class User extends AbstractEntity
{
    protected $_configs = array(
        'table' => 'users',
        'primary_key' => 'id',
        'entities' => array(
            'category' => 'Your\Namespace\Entity\Category'
        ),
        'datetime' => array(
            'created_at',
            'updated_at'
        )
    );

    protected $id;
    protected $username;
    protected $email;
    protected $category;
    protected $created_at;
    protected $updated_at;
}
```

\####Inserting and updataing If you indicate the primary key and it exists, an update will be make.

**Insert**

```
use Attw\Db\Connection\PDOConnector;
use Attw\Db\Storage\Storage;
use Attw\Db\Sql\MySQL;
use Attw\Db\Entity\EntityStorage;

$connector = new PDOConnector('mysql:host=localhost;dbname=test', 'root', 'pass');
$storage = new Storage($connector, new MySQL());
$entityStorage = new EntityStorage($storage);

$user = new User();
$user->username = 'Gabriel';
$user->email = 'gamjj74@hotmail.com';

$entityStorage->persist($user);
```

**Update**

```
$user = new User(17);//Id on contructor
$user->email = 'other@email.com';

$entityStorage->persist($user);
```

\####Removing

```
$user = new User(17);

$entityStorage->remove($user);
```

\####Fetching **Fetch one**

```
$user = new User(21);

$data = $entityStorage->fetch($user);
```

**Fetch all**

```
$user = new User();

$data = $entityStorage->fetchAll($user);
```

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/4fcbdf815749595d3dcfd4fb781e2cac05aa6d132123aa9da889d2c5ca6c8e11?d=identicon)[GabrielJMJ](/maintainers/GabrielJMJ)

---

Top Contributors

[![gabrieljmj](https://avatars.githubusercontent.com/u/2223216?v=4)](https://github.com/gabrieljmj "gabrieljmj (17 commits)")

### Embed Badge

![Health badge](/badges/attwframework-db/health.svg)

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

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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