To use the Active Record, you need to define the DB connection in one of the following ways:
Add the following code to the configuration file, for example, in 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));
}
];
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, using with yiisoft/middleware-dispatcher
packages, add the middleware to the configuration file,
for example in config/common/params.php
:
use Yiisoft\ActiveRecord\ConnectionProviderMiddleware;
return [
'middlewares' => [
ConnectionProviderMiddleware::class,
],
];
For more information about how to configure middleware, follow Middleware Documentation
You can set the DB connection for Active Record using the DI container autowiring.
use Psr\Http\Message\ResponseInterface;
use Yiisoft\ActiveRecord\ConnectionProvider;
use Yiisoft\Db\Connection\ConnectionInterface;
final class SomeController
{
public function someAction(
ConnectionInterface $db,
): ResponseInterface {
ConnectionProvider::set($db);
// ...
}
}
Another way to define the DB connection for Active Record is to use dependency injection.
use Yiisoft\Db\Connection\ConnectionInterface;
class User extends ActiveRecord
{
public function __construct(private readonly ConnectionInterface $db)
{
}
public function db(): ConnectionInterface
{
return $this->db;
}
}
/** @var \Yiisoft\Factory\Factory $factory */
$user = $factory->create(User::class);
Back to README