-
-
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.
Remove
$db
property from constructor (#368)
- Loading branch information
Showing
63 changed files
with
1,226 additions
and
1,566 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 |
---|---|---|
|
@@ -106,26 +106,59 @@ final class User extends ActiveRecord | |
|
||
For more information, follow [Create Active Record Model](docs/create-model.md). | ||
|
||
## Usage in controler with DI container autowiring | ||
## 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( | ||
User $user | ||
ConnectionInterface $db, | ||
): ResponseInterface { | ||
/** Connected AR by di autowired. */ | ||
ConnectionProvider::set($db); | ||
|
||
$user = new User(); | ||
$user->setAttribute('username', 'yiiliveext'); | ||
$user->setAttribute('email', '[email protected]'); | ||
$user->save(); | ||
} | ||
} | ||
``` | ||
|
||
## Usage in controler with Active Record factory | ||
## Usage with 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, if you use `yiisoft/config` and `yiisoft/middleware-dispatcher` packages, add the middleware to the configuration, | ||
for example in `config/common/params.php` file: | ||
|
||
```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)_ | ||
|
||
Now you can use the Active Record in the action: | ||
|
||
```php | ||
use App\Entity\User; | ||
|
@@ -134,12 +167,9 @@ use Yiisoft\ActiveRecord\ActiveRecordFactory; | |
|
||
final class Register | ||
{ | ||
public function register( | ||
ActiveRecordFactory $arFactory | ||
): ResponseInterface { | ||
/** Connected AR by factory di. */ | ||
$user = $arFactory->createAR(User::class); | ||
|
||
public function register(): ResponseInterface | ||
{ | ||
$user = new User(); | ||
$user->setAttribute('username', 'yiiliveext'); | ||
$user->setAttribute('email', '[email protected]'); | ||
$user->save(); | ||
|
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,8 @@ | ||
{ | ||
"symbol-whitelist": [ | ||
"Psr\\Http\\Message\\ResponseInterface", | ||
"Psr\\Http\\Message\\ServerRequestInterface", | ||
"Psr\\Http\\Server\\MiddlewareInterface", | ||
"Psr\\Http\\Server\\RequestHandlerInterface" | ||
] | ||
} |
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
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
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
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
Oops, something went wrong.