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

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

effectra/db
===========

The Effectra Database package.

v3.0.4(2y ago)1441MITPHP

Since Jun 20Pushed 2y agoCompare

[ Source](https://github.com/effectra/db)[ Packagist](https://packagist.org/packages/effectra/db)[ RSS](/packages/effectra-db/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (12)Used By (1)

Effectra\\Database
==================

[](#effectradatabase)

Effectra\\Database is a PHP package that provides database connection and query execution functionality. It offers a convenient interface for interacting with different database drivers and executing common database operations.

Installation
------------

[](#installation)

You can install the Effectra\\Database package via Composer. Simply run the following command:

```
composer require effectra/db
```

Usage
-----

[](#usage)

### Connection

[](#connection)

To establish a database connection, you need to create an instance of the `Connection` class and call the `connect` method. The `connect` method retrieves the database configuration from the provided configuration file and returns a PDO object representing the database connection.

```
use Effectra\Database\Connection;
use Effectra\Database\Diver;
use Effectra\Config\ConfigDB;

// Create a new instance of the Connection class

$mysqlConfig = [
    'driver' => 'mysql',
    'host' => 'localhost',
    'database' => 'your_database_name',
    'username' => 'your_mysql_username',
    'password' => 'your_mysql_password',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    // Add any additional options if needed
];

$connection = new Connection($mysqlConfig);
```

By default, the `Connection` class supports MySQL and SQLite database drivers. You can easily add support for additional database drivers by implementing the `DriverInterface` and configuring the `Connection` class accordingly.

### Query Execution

[](#query-execution)

Once you have established a database connection, you can execute queries using the `DB` class. The `DB` class provides methods for executing common database operations such as `select`, `insert`, `update`, and `delete`.

```
use Effectra\Database\DB;

// Establish the database connection
DB::createConnection($con);
// Establish the database event dispatcher
DB::setEventDispatcher(new EventDispatcher());

// Create a new instance of the DB class
$db = new DB();

// Execute a select query
$data = $db->withQuery('SELECT * FROM users')->get();

// Execute an insert query
$db->table('users')->data(['name' => 'Jane Doe','email'=> 'janeDoe@mail.com'])->insert();

// Execute an update query
$db->table('users')->data(['name' => 'Jane Doe'])->update((new Condition())->where(['id' => 2]));
```

The `DB` class provides a fluent interface for building and executing queries. You can chain methods to construct complex queries easily.

### Error Handling

[](#error-handling)

If an error occurs during query execution, the `DB` class will throw a `DatabaseException`. You can catch and handle this exception to gracefully handle database errors.

```
use Effectra\Database\Exception\DatabaseException;

try {
    $db->table('users')->insert( ['name' => 'John Doe']); // Missing 'email' field
} catch (DatabaseException $e) {
    // Handle the exception
    echo "Database Error: " . $e->getMessage();
}
```

Model
=====

[](#model)

1. **Namespace:** The model is part of the `Effectra\Database` namespace.
2. **Traits:** The model uses the `ModelEventTrait` trait.
3. **Properties:**

    - `$connection`: An instance of the `DBInterface` representing the database connection.
    - `$schema`: An array containing the schema of the model.
    - `$entries`: An array containing the entries of the model.
    - `$table`: The name of the table associated with the model.
    - `$primaryKey`: The primary key for the model (default is 'id').
    - `$keyType`: The data type of the primary key (default is 'int').
    - `$incrementing`: Indicates if the model's ID is auto-incrementing (default is `true`).
    - `const CREATED_AT` and `const UPDATED_AT`: Constants representing the names of "created at" and "updated at" columns.
    - `$options`: Additional options for the model.
    - `private static $query`: A static property to store the last executed database query.
4. **Constructor:**

    - The constructor sets the table name if not provided.
5. **Methods:**

    - `isEntriesCreated()`: Checks if entries have been created for the model.
    - `createModelStructure()`: Creates the structure of the model, including schema and entries.
    - `getDatabaseConnection()`: Gets a new database connection instance.
    - `createSchema()`: Creates the schema for the model by fetching metadata from the database.
    - `isSchemaCreated()`: Checks if the schema has been created for the model.
    - `createEntriesFromSchema()`: Creates entries for the model based on the schema.
    - Various methods for setting, getting, and manipulating options.
    - `getSchema($property)`: Gets the schema entry for a specific property.
    - `getEntries()`: Gets the entries for the model.
    - `hasEntry($property)`: Checks if a specific entry exists in the model.
    - `getEntry($property)`: Gets the value of a specific entry in the model.
    - `setEntries($entries)`: Sets the entries for the model.
    - `setEntry($property, $value)`: Sets a specific entry for the model.
    - `removeEntry($property)`: Removes a specific entry from the model.
    - Various magic methods (`__invoke`, `__set`, `__get`, `__toString`, `__isset`, `__unset`, `__callStatic`, `__call`) for dynamic property access and method calls.
    - `toArray()`: Converts the model to an array representation.
    - `toJson($flags = 0, $depth = 512)`: Converts the model to its JSON representation.
    - `save()`: Saves the model to the database.
    - `update()`: Updates the model in the database.
    - `transaction($callback, ...$args)`: Performs a model operation in a transaction.
    - `saveInTransaction($data = [])`: Saves the model in a transaction.
    - `updateInTransaction()`: Updates the model in a transaction.
    - Methods for retrieving models from the database (`get`, `all`, `limit`, `find`, `findBy`, `search`, `where`, `between`).
    - Methods for deleting models from the database (`delete`, `deleteById`, `deleteByIds`, `deleteByIdsInTransaction`).
    - `truncate()`: Truncates the model's table.
    - `lastInsertId()`: Gets the last inserted ID for the model.
    - `validateId($id)`: Validates a model ID.
    - `setQuery($query)`: Sets the query instance for the model.
    - `getQueryUsed()`: Gets the query instance used by the model.
    - `getQueryUsedAsString()`: Gets the query instance used by the model as a string.
    - `dd()`: Dumps the model class using Symfony's VarDumper.

Overall, this model provides a flexible and extensible foundation for database interactions in a PHP application. It includes features for CRUD operations, query building, and transaction management. Additionally, it leverages traits for handling model events and uses Symfony's VarDumper for debugging purposes.

Basic Usage
-----------

[](#basic-usage)

### create Model

[](#create-model)

```
class User extends Model {

}
```

### Retrieve a Record

[](#retrieve-a-record)

```
$user = User::find(1);
print_r($user);

echo $user->id;
// or use method
echo $user->getId();
```

### Update a Record

[](#update-a-record)

```
$user->name = 'Foo Bar';
// or use method
$user->setName('Foo Bar');

$user->update();
```

### Save a Record

[](#save-a-record)

```
$user = new User();

$user->name = 'Foo Bar';
$user->email = 'FooBar@email.com';

// or use method
$user
    ->setName('Foo Bar');
    ->setEmail('FooBar@email.com');

$user->save();
```

Contributing
------------

[](#contributing)

Contributions to the Effectra\\Database package are welcome. If you find any issues or have suggestions for improvement, please open an issue or submit a pull request on the GitHub repository.

License
-------

[](#license)

The Effectra\\Database package is open-source software licensed under the [MIT license](https://opensource.org/licenses/MIT). See the `LICENSE` file for more information.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity50

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 ~17 days

Recently: every ~6 days

Total

11

Last Release

887d ago

Major Versions

v1.0.0 → v2.0.02023-07-28

v2.1.3 → v3.0.02023-11-15

### Community

Maintainers

![](https://www.gravatar.com/avatar/7e6219ae87e98df8783b2f595b013035dd183c0712afd24045a8acf0a40c3bdf?d=identicon)[effectra](/maintainers/effectra)

---

Top Contributors

[![BMTmohammedtaha](https://avatars.githubusercontent.com/u/95439605?v=4)](https://github.com/BMTmohammedtaha "BMTmohammedtaha (43 commits)")

---

Tags

connectiondbdb-managermysqlooppdophpsqlite

### Embed Badge

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

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

###  Alternatives

[doctrine/mongodb-odm

PHP Doctrine MongoDB Object Document Mapper (ODM) provides transparent persistence for PHP objects to MongoDB.

1.1k23.3M302](/packages/doctrine-mongodb-odm)[getgrav/grav

Modern, Crazy Fast, Ridiculously Easy and Amazingly Powerful Flat-File CMS

15.4k84.1k1](/packages/getgrav-grav)[basemkhirat/elasticsearch

Laravel, Lumen and Native php elasticseach query builder to build complex queries using an elegant syntax

402312.0k](/packages/basemkhirat-elasticsearch)[bolt/core

🧿 Bolt Core

585142.5k54](/packages/bolt-core)[doctrine/doctrine-mongo-odm-module

Laminas Module which provides Doctrine MongoDB ODM functionality

86676.6k35](/packages/doctrine-doctrine-mongo-odm-module)[worksome/foggy

Foggy is a tool for making database dumps with some data removed/changed.

26571.7k1](/packages/worksome-foggy)

PHPackages © 2026

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