yiisoft / active-record
Yii ActiveRecord Library
Fund package maintenance!
Open Collective
yiisoft
Installs: 50 491
Dependents: 14
Suggesters: 0
Security: 0
Stars: 64
Watchers: 24
Forks: 26
Open Issues: 70
Requires
- php: ^8.1
- ext-json: *
- yiisoft/arrays: ^3.0
- yiisoft/db: ^1.1
- yiisoft/factory: ^1.0
Requires (Dev)
- maglnet/composer-require-checker: ^4.2
- phpunit/phpunit: ^10.5
- rector/rector: ^0.19
- roave/infection-static-analysis-plugin: ^1.34
- spatie/phpunit-watcher: ^1.23
- vimeo/psalm: ^5.20
- yiisoft/aliases: ^2.0
- yiisoft/cache: ^3.0
- yiisoft/db-sqlite: ^1.1
- yiisoft/di: ^1.0
- yiisoft/json: ^1.0
This package is auto-updated.
Last update: 2024-04-07 11:33:38 UTC
README
Yii ActiveRecord Library
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 | |
[db-mysql] | 7.4 - 8.0 | 5.7 - 8.0 | |
[db-oracle] | 7.4 - 8.0 | 11 - 21 | |
[db-pgsql] | 7.4 - 8.0 | 9.0 - 15.0 | |
[db-sqlite] | 7.4 - 8.0 | 3:latest |
Installation
The package could be installed via 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:
<?php declare(strict_types=1); 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
<?php declare(strict_types=1); return [ 'yiisoft/db-sqlite' => [ 'dsn' => 'sqlite:' . dirname(__DIR__) . '/runtime/yiitest.sq3', ] ]
Defined your active record class
<?php declare(strict_types=1); namespace App\Entity; 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
<?php declare(strict_types=1); namespace App\Action; 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
<?php declare(strict_types=1); namespace App\Action; 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(); } }
Support
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.
Testing
Check the testing instructions to learn about testing.
Support the project
Follow updates
License
The Yii ActiveRecord Library is free software. It is released under the terms of the BSD License.
Please see LICENSE
for more information.
Maintained by Yii Software.