-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathguide_start_databases.md.po
More file actions
527 lines (475 loc) · 14.9 KB
/
guide_start_databases.md.po
File metadata and controls
527 lines (475 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# Indonesian translations for PACKAGE package
# Copyright (C) 2025 Free Software Foundation, Inc.
# This file is distributed under the same license as the PACKAGE package.
# Automatically generated, 2025.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2025-12-26 09:52+0000\n"
"PO-Revision-Date: 2025-09-04 11:19+0500\n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
"Language: id\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#. type: Title #
#: ../src/guide/index.md ../src/guide/start/databases.md
#, no-wrap
msgid "Working with databases"
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "Yii doesn't dictate using a particular database or storage for your application. There are many ways you can work with relational databases:"
msgstr ""
#. type: Bullet: '- '
#: ../src/guide/start/databases.md
msgid "[Yii DB](https://github.com/yiisoft/db)"
msgstr ""
#. type: Bullet: '- '
#: ../src/guide/start/databases.md
msgid "[Yii Active Record](https://github.com/yiisoft/active-record)"
msgstr ""
#. type: Bullet: '- '
#: ../src/guide/start/databases.md
msgid "[Cycle](https://github.com/cycle) via [Yii Cycle package](https://github.com/yiisoft/yii-cycle)"
msgstr ""
#. type: Bullet: '- '
#: ../src/guide/start/databases.md
msgid "[Doctrine](https://www.doctrine-project.org/) via [Yii Doctrine package](https://github.com/stargazer-team/yii-doctrine)"
msgstr ""
#. type: Bullet: '- '
#: ../src/guide/start/databases.md
msgid "[PDO](https://www.php.net/manual/en/book.pdo.php)"
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "For non-relational ones, there are usually official libraries available:"
msgstr ""
#. type: Bullet: '- '
#: ../src/guide/start/databases.md
msgid "[ElasticSearch](https://github.com/elastic/elasticsearch-php)"
msgstr ""
#. type: Bullet: '- '
#: ../src/guide/start/databases.md
msgid "[Redis](https://redis.io/docs/clients/#php)"
msgstr ""
#. type: Bullet: ' - '
#: ../src/guide/start/databases.md ../src/internals/000-packages.md
msgid "..."
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "In this guide, we will focus on working with relational databases using Yii DB. We'll use PostgreSQL to implement a simple CRUD (create read update delete)."
msgstr ""
#. type: Title ##
#: ../src/guide/start/databases.md
#, no-wrap
msgid "Installing PostgreSQL"
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "You need to install PostgreSQL. If you prefer not to use Docker, [get the installer from official website](https://www.postgresql.org/download/), install it and create a database."
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "If you use Docker, it is a bit simpler. Modify `docker/dev/compose.yml`:"
msgstr ""
#. type: Fenced code block (yaml)
#: ../src/guide/start/databases.md
#, no-wrap
msgid ""
"services:\n"
" app:\n"
" build:\n"
" dockerfile: docker/Dockerfile\n"
" context: ..\n"
" target: dev\n"
" args:\n"
" USER_ID: ${UID}\n"
" GROUP_ID: ${GID}\n"
" env_file:\n"
" - path: ./dev/.env\n"
" - path: ./dev/override.env\n"
" required: false\n"
" ports:\n"
" - \"${DEV_PORT:-80}:80\"\n"
" volumes:\n"
" - ../:/app\n"
" - ../runtime:/app/runtime\n"
" - caddy_data:/data\n"
" - caddy_config:/config\n"
" tty: true\n"
" depends_on:\n"
" db:\n"
" condition: service_healthy\n"
"\n"
" db:\n"
" image: postgres:${POSTGRES_VERSION:-15}-alpine\n"
" environment:\n"
" POSTGRES_DB: app\n"
" POSTGRES_PASSWORD: password\n"
" POSTGRES_USER: user\n"
" volumes:\n"
" - ../runtime/db:/var/lib/postgresql/data:rw\n"
" ports:\n"
" - \"${DEV_DB_PORT:-5432}:5432\"\n"
" healthcheck:\n"
" test: [ \"CMD-SHELL\", \"pg_isready -U user -d app\" ]\n"
" interval: 5s\n"
" timeout: 5s\n"
" retries: 5\n"
"\n"
"volumes:\n"
" db:\n"
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "Note that we add `depends_on` so application waits for database to be up."
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "Also, we'll need a `pdo_pgsql` extension to communicate with PostgreSQL. You can enable it locally in `php.ini`. If you use Docker, check `docker/Dockerfile` and add `pdo_pgsql` in `install-php-extensions` list. Then rebuild PHP image with `make build && make down && make up`."
msgstr ""
#. type: Title ##
#: ../src/guide/start/databases.md
#, no-wrap
msgid "Configuring connection"
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "Now that we have the database, it's time to define the connection."
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "First we need a package to be installed:"
msgstr ""
#. type: Fenced code block (sh)
#: ../src/guide/start/databases.md
#, fuzzy, no-wrap
#| msgid "composer require yiisoft/cache\n"
msgid "make composer require yiisoft/db-pgsql\n"
msgstr "composer require yiisoft/cache\n"
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "Now create `config/common/di/db-pgsql.php`:"
msgstr ""
#. type: Fenced code block (php)
#: ../src/guide/start/databases.md
#, no-wrap
msgid ""
"<?php\n"
"\n"
"use Yiisoft\\Db\\Connection\\ConnectionInterface;\n"
"use Yiisoft\\Db\\Pgsql\\Connection;\n"
"use Yiisoft\\Db\\Pgsql\\Driver;\n"
"\n"
"/** @var array $params */\n"
"\n"
"return [\n"
" ConnectionInterface::class => [\n"
" 'class' => Connection::class,\n"
" '__construct()' => [\n"
" 'driver' => new Driver(\n"
" $params['yiisoft/db-pgsql']['dsn'],\n"
" $params['yiisoft/db-pgsql']['username'],\n"
" $params['yiisoft/db-pgsql']['password'],\n"
" ),\n"
" ],\n"
" ],\n"
"];\n"
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "And define parameters in `config/common/params.php`. For Docker that would be:"
msgstr ""
#. type: Fenced code block (php)
#: ../src/guide/start/databases.md
#, no-wrap
msgid ""
"use Yiisoft\\Db\\Pgsql\\Dsn;\n"
"\n"
"return [\n"
" // ...\n"
" 'yiisoft/db-pgsql' => [\n"
" 'dsn' => new Dsn('pgsql', 'db', 'app', '5432'),\n"
" 'username' => 'user',\n"
" 'password' => 'password',\n"
" ],\n"
"];\n"
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "`db` host is resolved automatically within the Docker network."
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "For local installation without Docker the host in Dsn would be `localhost`. You'll have to adjust the rest to match how you configured the database."
msgstr ""
#. type: Title ##
#: ../src/guide/start/databases.md
#, no-wrap
msgid "Creating and applying migrations"
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "For the initial state of the application and for further database changes, it is a good idea to use migrations. These are files that create database changes. Applied migrations are tracked in the database, allowing us to know the current state and which migrations remain to be applied."
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "To use migrations we need another package installed:"
msgstr ""
#. type: Fenced code block (sh)
#: ../src/guide/start/databases.md
#, fuzzy, no-wrap
#| msgid "composer require yiisoft/cache\n"
msgid "make composer require yiisoft/db-migration\n"
msgstr "composer require yiisoft/cache\n"
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "Create a directory to store migrations `src/Migration` right in the project root. Add the following configuration to `config/common/params.php`:"
msgstr ""
#. type: Fenced code block (php)
#: ../src/guide/start/databases.md
#, no-wrap
msgid ""
"'yiisoft/db-migration' => [\n"
" 'newMigrationNamespace' => 'App\\\\Migration',\n"
" 'sourceNamespaces' => ['App\\\\Migration'],\n"
"],\n"
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "Now you can use `make yii migrate:create page` to create a new migration. For our example we need a `page` table with some columns:"
msgstr ""
#. type: Fenced code block (php)
#: ../src/guide/start/databases.md
#, no-wrap
msgid ""
"<?php\n"
"\n"
"declare(strict_types=1);\n"
"\n"
"namespace App\\Migration;\n"
"\n"
"use Yiisoft\\Db\\Migration\\MigrationBuilder;\n"
"use Yiisoft\\Db\\Migration\\RevertibleMigrationInterface;\n"
"\n"
"final class M251102141707Page implements RevertibleMigrationInterface\n"
"{\n"
" public function up(MigrationBuilder $b): void\n"
" {\n"
" $cb = $b->columnBuilder();\n"
"\n"
" $b->createTable('page', [\n"
" 'id' => $cb::uuidPrimaryKey(),\n"
" 'title' => $cb::string()->notNull(),\n"
" 'slug' => $cb::string()->notNull(),\n"
" 'text' => $cb::text()->notNull(),\n"
" 'created_at' => $cb::dateTime(),\n"
" 'updated_at' => $cb::dateTime(),\n"
" 'deleted_at' => $cb::dateTime(),\n"
" ]);\n"
" }\n"
"\n"
" public function down(MigrationBuilder $b): void\n"
" {\n"
" $b->dropTable('page');\n"
" }\n"
"}\n"
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "Note that we use UUID as the primary key. While the storage space is a bit bigger than using int, the workflow with such IDs is beneficial. You generate the ID yourself so you can define a set of related data and save it in a single transaction. The entities that define this set of data in the code are often called an \"aggregate\"."
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "Apply it with `make yii migrate:up`."
msgstr ""
#. type: Title ##
#: ../src/guide/start/databases.md
#, no-wrap
msgid "An entity"
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "Now that you have a table it is time to define an entity in the code. Create `src/Web/Page/Page.php`:"
msgstr ""
#. type: Fenced code block (php)
#: ../src/guide/start/databases.md
#, no-wrap
msgid ""
"<?php\n"
"\n"
"declare(strict_types=1);\n"
"\n"
"namespace App\\Web\\Page;\n"
"\n"
"use DateTimeImmutable;\n"
"use Yiisoft\\Strings\\Inflector;\n"
"\n"
"final readonly class Page\n"
"{\n"
" private function __construct(\n"
" public string $id,\n"
" public string $title,\n"
" public string $text,\n"
" public DateTimeImmutable $createdAt,\n"
" public DateTimeImmutable $updatedAt,\n"
" public ?DateTimeImmutable $deletedAt = null,\n"
" ) {}\n"
"\n"
" public static function create(\n"
" string $id,\n"
" string $title,\n"
" string $text,\n"
" ?DateTimeImmutable $createdAt = null,\n"
" ?DateTimeImmutable $updatedAt = null,\n"
" ?DateTimeImmutable $deletedAt = null,\n"
" ): self {\n"
" return new self(\n"
" id: $id,\n"
" title: $title,\n"
" text: $text,\n"
" createdAt: $createdAt ?? new DateTimeImmutable(),\n"
" updatedAt: $updatedAt ?? new DateTimeImmutable(),\n"
" deletedAt: $deletedAt,\n"
" );\n"
" }\n"
"\n"
" public function getSlug(): string\n"
" {\n"
" return (new Inflector())->toSlug($this->title);\n"
" }\n"
"\n"
" public function isDeleted(): bool\n"
" {\n"
" return $this->deletedAt !== null;\n"
" }\n"
"}\n"
msgstr ""
#. type: Bullet: '- '
#: ../src/guide/start/databases.md ../src/guide/structure/domain.md
#: ../src/guide/structure/service.md
#, no-wrap
msgid "Repository"
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "Now that we have entity, we need a place for methods to save an entity, delete it and select either a single page or multiple pages."
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "Create `src/Web/Page/PageRepository.php`:"
msgstr ""
#. type: Fenced code block (php)
#: ../src/guide/start/databases.md
#, no-wrap
msgid ""
"<?php\n"
"\n"
"declare(strict_types=1);\n"
"\n"
"namespace App\\Web\\Page;\n"
"\n"
"use DateTimeImmutable;\n"
"use Yiisoft\\Db\\Connection\\ConnectionInterface;\n"
"use Yiisoft\\Db\\Query\\Query;\n"
"\n"
"final readonly class PageRepository\n"
"{\n"
" public function __construct(\n"
" private ConnectionInterface $connection,\n"
" ) {}\n"
"\n"
" public function save(Page $page): void\n"
" {\n"
" $this->connection->createCommand()->upsert('{{%page}}', [\n"
" 'id' => $page->id,\n"
" 'title' => $page->title,\n"
" 'slug' => $page->getSlug(),\n"
" 'text' => $page->text,\n"
" 'created_at' => $page->createdAt,\n"
" 'updated_at' => $page->updatedAt,\n"
" 'deleted_at' => $page->deletedAt,\n"
" ])->execute();\n"
" }\n"
"\n"
" public function findOneBySlug(string $slug): ?Page\n"
" {\n"
" $data = (new Query($this->connection))\n"
" ->select('*')\n"
" ->from('{{%page}}')\n"
" ->where('slug = :slug', ['slug' => $slug])\n"
" ->one();\n"
"\n"
" if ($data === null) {\n"
" return null;\n"
" }\n"
"\n"
" return $this->createPage($data);\n"
" }\n"
"\n"
" /**\n"
" * @return iterable<Page>\n"
" */\n"
" public function findAll(): iterable\n"
" {\n"
" $data = (new Query($this->connection))\n"
" ->select('*')\n"
" ->from('{{%page}}')\n"
" ->all();\n"
"\n"
" foreach ($data as $page) {\n"
" yield $this->createPage($page);\n"
" }\n"
" }\n"
"\n"
" private function createPage(array $data): Page\n"
" {\n"
" return Page::create(\n"
" id: $data['id'],\n"
" title: $data['title'],\n"
" text: $data['text'],\n"
" createdAt: new DateTimeImmutable($data['created_at']),\n"
" updatedAt: new DateTimeImmutable($data['updated_at']),\n"
" deletedAt: $data['deleted_at'] ? new DateTimeImmutable($data['deleted_at']) : null,\n"
" );\n"
" }\n"
"\n"
" public function delete(string $id): void\n"
" {\n"
" $this->connection->createCommand()->delete('{{%page}}', ['id' => $id])->execute();\n"
" }\n"
"}\n"
msgstr ""
#. type: Title ##
#: ../src/guide/start/databases.md
#, no-wrap
msgid "Actions and routes"
msgstr ""
#. type: Plain text
#: ../src/guide/start/databases.md
msgid "You need actions to:"
msgstr ""
#. type: Bullet: '1. '
#: ../src/guide/start/databases.md
msgid "List all pages."
msgstr ""
#. type: Bullet: '2. '
#: ../src/guide/start/databases.md
msgid "View a page."
msgstr ""
#. type: Bullet: '3. '
#: ../src/guide/start/databases.md
msgid "Delete a page."
msgstr ""
#. type: Bullet: '4. '
#: ../src/guide/start/databases.md
msgid "Create a page."
msgstr ""
#. type: Bullet: '5. '
#: ../src/guide/start/databases.md
msgid "Edit a page."
msgstr ""