yiisoft / active-record
Yii ActiveRecord Library
Fund package maintenance!
Opencollective
yiisoft
Installs: 56 425
Dependents: 15
Suggesters: 0
Security: 0
Stars: 69
Watchers: 26
Forks: 29
Open Issues: 62
Requires
- php: ^8.1
- ext-json: *
- yiisoft/db: dev-master
Requires (Dev)
- maglnet/composer-require-checker: ^4.2
- phpunit/phpunit: ^10.5
- rector/rector: ^1.0
- roave/infection-static-analysis-plugin: ^1.34
- spatie/phpunit-watcher: ^1.23
- vimeo/psalm: ^5.25
- yiisoft/aliases: ^2.0
- yiisoft/arrays: ^3.1
- yiisoft/cache: ^3.0
- yiisoft/db-sqlite: dev-master
- yiisoft/di: ^1.0
- yiisoft/factory: ^1.2
- yiisoft/json: ^1.0
- yiisoft/middleware-dispatcher: ^5.2
Suggests
- yiisoft/arrays: For \Yiisoft\Arrays\ArrayableInterface support
- yiisoft/db-mssql: For MSSQL database support
- yiisoft/db-mysql: For MySQL database support
- yiisoft/db-oracle: For Oracle database support
- yiisoft/db-pgsql: For PostgreSQL database support
- yiisoft/db-sqlite: For SQLite database support
- yiisoft/factory: For factory support
- yiisoft/middleware-dispatcher: For middleware support
This package is auto-updated.
Last update: 2024-10-13 19:32:06 UTC
README
Yii ActiveRecord
This package provides ActiveRecord library. It is used in Yii Framework but is supposed to be usable separately.
Support databases
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->set('username', 'yiiliveext'); $user->set('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])->one(); $username = $user->get('username'); $email = $user->get('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.