Skip to content

Commit 551ad95

Browse files
authored
Fix #139: Clarify database data access guidance (#465)
1 parent 087090b commit 551ad95

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

src/guide/start/databases.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@ For non-relational ones, there are usually official libraries available:
1515
- [Redis](https://redis.io/docs/latest/develop/clients/php/)
1616
- ...
1717

18+
## Choosing a data access approach
19+
20+
Yii3 keeps database access in optional packages. A new application doesn't get a global `Yii::$app->db` component
21+
or a built-in Active Record layer automatically, so choose the package and boundary that fit your application.
22+
23+
If you used Yii 2.0 database access layer, `yiisoft/db` is the closest Yii3 equivalent. It provides database
24+
connections, commands, schema support, and a query builder. Instead of calling `Yii::$app->db`, configure a
25+
connection as a service and inject `ConnectionInterface` where the database is needed.
26+
27+
If you used Yii 2.0 Active Record, you can install `yiisoft/active-record`. It is useful when your model classes
28+
mostly describe tables and relations. For applications where Yii 2.0 Active Record classes also contain form
29+
validation, request scenarios, authorization checks, or business workflows, move those responsibilities to form
30+
models, services, and domain objects instead of putting them back into the record class.
31+
32+
For most application code, put a repository or query service between the action/service layer and storage:
33+
34+
- actions and application services depend on repository methods such as `findOneBySlug()`, `findAll()`, `save()`,
35+
and `deleteBySlug()`;
36+
- the repository hides whether persistence is implemented with `yiisoft/db`, Active Record, Cycle, Doctrine, or
37+
another storage;
38+
- the repository is the place to map raw database rows or records to typed objects used by the rest of the code.
39+
40+
Small CRUD applications can start with a concrete repository class. Larger applications usually benefit from a
41+
repository interface in the domain or module and an implementation in the infrastructure layer.
42+
1843
In this guide, we will focus on working with relational databases using Yii DB. We'll use PostgreSQL to implement a
1944
simple CRUD (create read update delete).
2045

@@ -366,8 +391,9 @@ final readonly class Page
366391

367392
## Repository
368393

369-
Now that we have entity, we need a place for methods to save an entity, delete it and select either
370-
a single page or multiple pages.
394+
Now that we have an entity, we need a place for methods to save it, delete it, and select either a single page or
395+
multiple pages. This is the repository boundary described earlier: the rest of the application works with `Page`
396+
objects and doesn't need to know how they are stored.
371397

372398
Create `src/Web/Page/PageRepository.php`:
373399

0 commit comments

Comments
 (0)