PHPackages                             clicalmani/database - 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. clicalmani/database

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

clicalmani/database
===================

Tonka database query builder

v3.3.0(2mo ago)01633MITPHPPHP &gt;=8.0

Since Jan 16Pushed 3w ago1 watchersCompare

[ Source](https://github.com/clicalmani/database)[ Packagist](https://packagist.org/packages/clicalmani/database)[ RSS](/packages/clicalmani-database/feed)WikiDiscussions main Synced today

READMEChangelog (10)Dependencies (3)Versions (22)Used By (3)

Tonka PHP Framework Database ORM
================================

[](#tonka-php-framework-database-orm)

[![Tonka Logo](https://camo.githubusercontent.com/29319c76f9ea07495423d94489793b94927d26113c31f89d1ffb4a1c0b183283/68747470733a2f2f636c6963616c6d616e692e6769746875622e696f2f746f6e6b612f6c6f676f2d6461726b2e706e67)](https://camo.githubusercontent.com/29319c76f9ea07495423d94489793b94927d26113c31f89d1ffb4a1c0b183283/68747470733a2f2f636c6963616c6d616e692e6769746875622e696f2f746f6e6b612f6c6f676f2d6461726b2e706e67)

Overview
--------

[](#overview)

The Tonka PHP Framework Database ORM (Object-Relational Mapping) provides a powerful and efficient way to interact with databases in PHP applications. It simplifies database operations by allowing you to work with PHP objects instead of writing raw SQL queries.

Features
--------

[](#features)

- **Active Record Implementation**: Easy to map database tables to PHP classes.
- **Query Builder**: Build complex queries using a fluent interface.
- **Relationships**: Support for one-to-one, one-to-many, and many-to-many relationships.
- **Migration Support**: Manage database schema changes with ease.
- **Transactions**: Handle database transactions effectively.

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

[](#installation)

You can install the Tonka ORM via Composer:

```
composer require clicalmani/database
```

Usage
-----

[](#usage)

Here’s a basic example of how to use the Elegant ORM:

### Defining a Model

[](#defining-a-model)

```
use Clicalmani\Database\Factory\Models\Elegant;

class User extends Elegant
{
    /**
     * Model database table
     *
     * @var string $table Table name
     */
    protected $table = "users";

    /**
     * Model entity
     *
     * @var string
     */
    protected string $entity = \Database\Entities\UserEntity::class;

    /**
     * Table primary key(s)
     * Use an array if the key is composed with more than one attributes.
     *
     * @var string|array $primary_keys Table primary key.
     */
    protected $primaryKey = "id";

    public function __construct($id = null)
    {
        parent::__construct($id);
    }
}
```

### Creating a Record

[](#creating-a-record)

```
$user = new User();
$user->name = 'John Doe';
$user->email = 'john@example.com';
$user->save();
```

### Querying Records

[](#querying-records)

```
$users = User::where('active = ?', [1])->fetch();
foreach ($users as $user) {
    echo $user->name;
    echo $user->isOnline();
}
```

### Relationships

[](#relationships)

#### One-to-Many Relationship

[](#one-to-many-relationship)

```
