-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
134 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -71,7 +70,7 @@ return [ | |
]; | ||
``` | ||
|
||
params.php | ||
`config/common/params.php`: | ||
|
||
```php | ||
return [ | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |