Skip to content

yiisoft/active-record

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

e5ff769 · Jul 2, 2024
May 2, 2024
Jun 6, 2024
Jul 2, 2024
Jul 2, 2024
Aug 25, 2021
Jun 28, 2024
Dec 19, 2020
Dec 19, 2020
Mar 31, 2023
May 3, 2024
May 3, 2024
Jul 1, 2024
Jul 2, 2024
Jan 11, 2022
Apr 23, 2024
May 2, 2024
May 2, 2024
May 22, 2024

Repository files navigation

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 Versions CI-Actions
[db-mssql] 2017 - 2022 Build status Mutation testing badge codecov
[db-mysql] 5.7 - 8.0 Build status Mutation testing badge codecov
[db-oracle] 11 - 21 Build status Mutation testing badge codecov
[db-pgsql] 9.0 - 15.0 Build status Mutation testing badge codecov
[db-sqlite] 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
 **/
#[\AllowDynamicProperties]
final class User extends ActiveRecord
{
    public function getTableName(): string
    {
        return '{{%user}}';
    }
}

For more information, follow Create Active Record Model.

Usage in controller with DI container autowiring

use App\Entity\User;
use Psr\Http\Message\ResponseInterface;
use Yiisoft\ActiveRecord\ConnectionProvider;
use Yiisoft\Db\Connection\ConnectionInterface;

final class Register
{
    public function register(
        ConnectionInterface $db,
    ): ResponseInterface {
        ConnectionProvider::set($db);
    
        $user = new User();
        $user->setAttribute('username', 'yiiliveext');
        $user->setAttribute('email', '[email protected]');
        $user->save();
    }
}

Usage with middleware

Add the middleware to the action, for example:

use Yiisoft\ActiveRecord\ConnectionProviderMiddleware;
use Yiisoft\Router\Route;

Route::methods([Method::GET, Method::POST], '/user/register')
    ->middleware(ConnectionProviderMiddleware::class)
    ->action([Register::class, 'register'])
    ->name('user/register');

Or, if you use yiisoft/config and yiisoft/middleware-dispatcher packages, add the middleware to the configuration, for example in config/common/params.php file:

use Yiisoft\ActiveRecord\ConnectionProviderMiddleware;

return [
    'middlewares' => [
        ConnectionProviderMiddleware::class,
    ],
];

For more information about how to configure middleware, follow Middleware Documentation

Now you can use the Active Record in the action:

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

final class Register
{
    public function register(): ResponseInterface
    {
        $user = new User();
        $user->setAttribute('username', 'yiiliveext');
        $user->setAttribute('email', '[email protected]');
        $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