Get support for yiisoft/active-record

If you're new to LTH, please see our FAQ for more information on what it is we do.

Support Options

Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:

View Open Issues

Take a look to see if anyone else has experienced the same issue as you and if they managed to solve it.

Open an Issue

Make sure to read any relevant guidelines for opening issues on this repo before posting a new issue.

Sponsor directly

Check out the page and see if there are any options to sponsor this project or it's developers directly.

yiisoft/active-record

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

Configure container with database connection

Add the following code to the configuration files, for example:

config/common/di/db.php:

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

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

config/common/params.php:

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

For more information about how to configure the connection, follow Yii Database.

config/common/bootstrap.php:

use Psr\Container\ContainerInterface;
use Yiisoft\ActiveRecord\ConnectionProvider;
use Yiisoft\Db\Connection\ConnectionInterface;

return [
    static function (ContainerInterface $container): void {
        ConnectionProvider::set($container->get(ConnectionInterface::class));
    }
];

See other ways to define the DB connection for Active Record.

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

Now you can use the Active Record:

use App\Entity\User;

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

Usage with ActiveQuery:

use App\Entity\User;
use Yiisoft\ActiveRecord\ActiveQuery;

$userQuery = new ActiveQuery(User::class);

$user = $userQuery->where(['id' => 1])->onePopulate();

$username = $user->getAttribute('username');
$email = $user->getAttribute('email');

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

Our Mission

We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.

Interesting Articles

Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated

p-e622a1a2