Skip to content

Commit

Permalink
Update doc (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Jul 4, 2024
1 parent b20482d commit b5a39d0
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 65 deletions.
102 changes: 37 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,17 @@ Example:
composer require yiisoft/db-sqlite
```

## Config container interface class
## Configure container with database connection

web.php:
Add the following code to the configuration files, for example:

`config/common/di/db.php`:

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

/**
* config ConnectionInterface::class
*/
return [
ConnectionInterface::class => [
'class' => Connection::class,
Expand All @@ -71,7 +70,7 @@ return [
];
```

params.php
`config/common/params.php`:

```php
return [
Expand All @@ -81,6 +80,24 @@ return [
]
```

For more information about how to configure the connection, follow [Yii Database](https://github.com/yiisoft/db/blob/master/docs/guide/en/README.md).

`config/common/bootstrap.php`:

```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](docs/define-connection.md) for Active Record.

## Defined your active record class

```php
Expand All @@ -106,76 +123,31 @@ final class User extends ActiveRecord

For more information, follow [Create Active Record Model](docs/create-model.md).

## Usage in controller with DI container autowiring

```php
use App\Entity\User;
use Psr\Http\Message\ResponseInterface;
use Yiisoft\ActiveRecord\ConnectionProvider;
use Yiisoft\Db\Connection\ConnectionInterface;

final class Register
{
public function register(
ConnectionInterface $db,
): ResponseInterface {
ConnectionProvider::set($db);

$user = new User();
$user->setAttribute('username', 'yiiliveext');
$user->setAttribute('email', '[email protected]');
$user->save();
}
}
```

## Usage with middleware
## Usage

Add the middleware to the action, for example:
Now you can use the Active Record:

```php
use Yiisoft\ActiveRecord\ConnectionProviderMiddleware;
use Yiisoft\Router\Route;
use App\Entity\User;

Route::methods([Method::GET, Method::POST], '/user/register')
->middleware(ConnectionProviderMiddleware::class)
->action([Register::class, 'register'])
->name('user/register');
$user = new User();
$user->setAttribute('username', 'yiiliveext');
$user->setAttribute('email', '[email protected]');
$user->save();
```

Or, if you use `yiisoft/config` and `yiisoft/middleware-dispatcher` packages, add the middleware to the configuration,
for example in `config/common/params.php` file:
Usage with ActiveQuery:

```php
use Yiisoft\ActiveRecord\ConnectionProviderMiddleware;

return [
'middlewares' => [
ConnectionProviderMiddleware::class,
],
];
```

_For more information about how to configure middleware, follow
[Middleware Documentation](https://github.com/yiisoft/docs/blob/master/guide/en/structure/middleware.md)_
use App\Entity\User;
use Yiisoft\ActiveRecord\ActiveQuery;

Now you can use the Active Record in the action:
$userQuery = new ActiveQuery(User::class);

```php
use App\Entity\User;
use Psr\Http\Message\ResponseInterface;
use Yiisoft\ActiveRecord\ActiveRecordFactory;
$user = $userQuery->where(['id' => 1])->onePopulate();

final class Register
{
public function register(): ResponseInterface
{
$user = new User();
$user->setAttribute('username', 'yiiliveext');
$user->setAttribute('email', '[email protected]');
$user->save();
}
}
$username = $user->setAttribute('username');
$email = $user->getAttribute('email');
```

## Documentation
Expand Down
97 changes: 97 additions & 0 deletions docs/define-connection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Define the DB connection for Active Record

To use the Active Record, you need to define the DB connection in one of the following ways:

## Using the bootstrap configuration

Add the following code to the configuration file, for example in `config/common/bootstrap.php`:

```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));
}
];
```

## Using middleware

Add the middleware to the action, for example:

```php
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`:

```php
use Yiisoft\ActiveRecord\ConnectionProviderMiddleware;

return [
'middlewares' => [
ConnectionProviderMiddleware::class,
],
];
```

_For more information about how to configure middleware, follow
[Middleware Documentation](https://github.com/yiisoft/docs/blob/master/guide/en/structure/middleware.md)_

## Using DI container autowiring

You can set the DB connection for Active Record using the DI container autowiring.

```php
use Psr\Http\Message\ResponseInterface;
use Yiisoft\ActiveRecord\ConnectionProvider;
use Yiisoft\Db\Connection\ConnectionInterface;

final class Register
{
public function register(
ConnectionInterface $db,
): ResponseInterface {
ConnectionProvider::set($db);

// ...
}
}
```

## Using dependency injection

Another way to define the DB connection for Active Record is to use dependency injection.

```php
use Psr\Container\ContainerInterface;

class User extends ActiveRecord
{
public function __construct(private ContainerInterface $db)
{
}

public function db(): ContainerInterface
{
return $this->db;
}
}
```

```php
/** @var \Yiisoft\Factory\Factory $factory */
$user = $factory->create(User::class);
```

Back to [README](../README.md)

0 comments on commit b5a39d0

Please sign in to comment.