terabytesoft / active-record
Yii ActiveRecord Library
This package's canonical repository appears to be gone and the package has been frozen as a result.
Fund package maintenance!
Open Collective
yiisoft
Requires
- php: ^8.0
- ext-json: *
- terabytesoft/db: @dev
- yiisoft/arrays: ^2.0
- yiisoft/factory: ^1.0
- yiisoft/strings: ^2.0
Requires (Dev)
- phpunit/phpunit: ^9.4
- roave/infection-static-analysis-plugin: ^1.14
- terabytesoft/db-mssql: @dev
- terabytesoft/db-mysql: @dev
- terabytesoft/db-oracle: @dev
- terabytesoft/db-pgsql: @dev
- terabytesoft/db-sqlite: @dev
- vimeo/psalm: ^4.2
- yiisoft/aliases: ^2.0
- yiisoft/di: ^1.0
- yiisoft/event-dispatcher: ^1.0
- yiisoft/json: ^1.0
- yiisoft/log: ^1.0
This package is auto-updated.
Last update: 2023-05-10 00:11:46 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 - 2019 | |
[db-mysql] | 7.4 - 8.0 | 5.7 - 8.0 | |
[db-oracle] | 7.4 - 8.0 | 11c - 12c | |
[db-pgsql] | 7.4 - 8.0 | 9.0 - 13.0 | |
[db-sqlite] | 7.4 - 8.0 | 3:latest | |
[db-redis] | 7.4 - 8.0 | 4.0 - 6.0 |
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-mysql
Configuration container di autowired
web.php:
<?php declare(strict_types=1); use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Sqlite\Connection as SqliteConnection; /** * config ConnectionInterface::class */ return [ ConnectionInterface::class => [ 'class' => SqliteConnection::class, '__construct()' => [ 'dsn' => $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, example User.php:
<?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 tableName(): string { return '{{%user}}'; } }
in controler or action:
<?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(); } }
Configuration factory di
web.php:
<?php declare(strict_types=1); use Yiisoft\ActiveRecord\ActiveRecordFactory; use Yiisoft\Db\Connection\ConnectionInterface; use Yiisoft\Db\Sqlite\Connection as SqliteConnection; use Yiisoft\Definitions\Reference; /** * config SqliteConnection::class */ return [ SqliteConnection::class => [ 'class' => SqliteConnection::class, '__construct()' => [ 'dsn' => $params['yiisoft/db-sqlite']['dsn'], ] ], ActiveRecordFactory::class => [ 'class' => ActiveRecordFactory::class, '__construct()' => [ null, [ConnectionInterface::class => Reference::to(SqliteConnection::class)], ] ] ];
params.php
<?php declare(strict_types=1); return [ 'yiisoft/db-sqlite' => [ 'dsn' => 'sqlite:' . dirname(__DIR__) . '/runtime/yiitest.sq3', ] ]
defined your active record, example User.php:
<?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 tableName(): string { return '{{%user}}'; } }
in controler or action:
<?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(); } }
Unit testing
The package is tested with PHPUnit. To run tests:
./vendor/bin/phpunit
Mutation testing
The package tests are checked with Infection mutation framework. To run it:
./vendor/bin/infection
Static analysis
The code is statically analyzed with Psalm. To run static analysis:
./vendor/bin/psalm
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.