aivchen / active-record

Active Record database abstraction layer

Home Page:https://www.yiiframework.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Yii

Yii ActiveRecord Library


Latest Stable Version Total Downloads codecov Mutation testing badge static analysis type-coverage

This package provides ActiveRecord library. It is used in Yii Framework but is supposed to be usable separately.

Support databases

Packages PHP Versions CI-Actions
[db-mssql] 7.4 - 8.0 2017 - 2022 Build status Mutation testing badge codecov
[db-mysql] 7.4 - 8.0 5.7 - 8.0 Build status Mutation testing badge codecov
[db-oracle] 7.4 - 8.0 11 - 21 Build status Mutation testing badge codecov
[db-pgsql] 7.4 - 8.0 9.0 - 15.0 Build status Mutation testing badge codecov
[db-sqlite] 7.4 - 8.0 3:latest Build status Mutation testing badge codecov

Requirements

  • PHP 8.1 or higher.

Installation

The package could be installed with Composer:

composer require yiisoft/active-record

Note: You must install the repository of the implementation to use.

Example:

composer require yiisoft/db-sqlite

Config container interface class

web.php:

use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Sqlite\Connection;
use Yiisoft\Db\Sqlite\Driver;

/**
 * config ConnectionInterface::class
 */
return [
    ConnectionInterface::class => [
        'class' => Connection::class,
        '__construct()' => [
            'driver' => new Driver($params['yiisoft/db-sqlite']['dsn']),
        ],
    ]
];

params.php

return [
    'yiisoft/db-sqlite' => [
        'dsn' => 'sqlite:' . dirname(__DIR__) . '/runtime/yiitest.sq3',
    ]
]

Defined your active record class

use Yiisoft\ActiveRecord\ActiveRecord;

/**
 * Entity User.
 *
 * Database fields:
 * @property int $id
 * @property string $username
 * @property string $email
 **/
final class User extends ActiveRecord
{
    public function getTableName(): string
    {
        return '{{%user}}';
    }
}

Usage in controler with DI container autowiring

use App\Entity\User;
use Psr\Http\Message\ResponseInterface;

final class Register
{
    public function register(
        User $user
    ): ResponseInterface {
        /** Connected AR by di autowired. */
        $user->setAttribute('username', 'yiiliveext');
        $user->setAttribute('email', 'yiiliveext@mail.ru');
        $user->save();
    }
}

Usage in controler with Active Record factory

use App\Entity\User;
use Psr\Http\Message\ResponseInterface;
use Yiisoft\ActiveRecord\ActiveRecordFactory;

final class Register
{
    public function register(
        ActiveRecordFactory $arFactory
    ): ResponseInterface {
        /** Connected AR by factory di. */
        $user = $arFactory->createAR(User::class);

        $user->setAttribute('username', 'yiiliveext');
        $user->setAttribute('email', 'yiiliveext@mail.ru');
        $user->save();
    }
}

Documentation

If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.

License

The Yii Active Record Library is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

About

Active Record database abstraction layer

https://www.yiiframework.com/

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:PHP 92.7%Language:PLSQL 4.2%Language:TSQL 3.1%