@@ -12,72 +12,45 @@ A basic repository can be quite a short class. The shortest possible
1212repository is an empty class inheriting from
1313:php: `TYPO3\C MS\E xtbase\P ersistence\R epository `:
1414
15- .. code-block :: php
16- :caption: EXT:tea/Classes/Domain/Repository/Product/TeaRepository.php
17-
18- <?php
19-
20- declare(strict_types=1);
21-
22- namespace TTN\Tea\Domain\Repository\Product;
23-
24- use TYPO3\CMS\Extbase\Persistence\Repository;
25-
26- class TeaRepository extends Repository
27- {
28- }
15+ .. literalinclude :: _Repository/_BasicRepository.php
16+ :caption: EXT:tea/Classes/Domain/Repository/Product/TeaRepository.php (simplified)
2917
3018The model the repository should deliver is derived from the namespace and
3119name of the repository. A repository with the fully qualified name
3220:php: `TTN\T ea\D omain\R epository\P roduct\T eaRepository ` therefore delivers
3321models with the fully qualified name :php: `TTN\T ea\D omain\M odel\P roduct\T ea `
3422without further configuration.
3523
36- In the EXT:tea extension some additional settings are required. These can be
37- done directly in the Repository or in a
38- `trait <https://www.php.net/manual/en/language.oop5.traits.php >`__. It is
39- important to know, that a trait overrides parameters and method from the
40- parent class but can be overridden from the current class.
24+ A special class comment (not mandatory), `@extends Repository<Tea> ` tells your IDE and
25+ static analytic tools like PHPStan that the find-by methods of this repository
26+ return objects of type :php-short: `TTN\T ea\D omain\M odel\P roduct\T ea `.
4127
42- The :php: `TeaRepository ` configures the :php: `$defaultOrderings ` directly in
43- the repository class and imports additional settings from the trait.
28+ The repository in the tea extension looks like this:
4429
45- .. include :: /CodeSnippets/Tutorials/Tea/Classes/Domain/Repository/TeaRepository.rst.txt
30+ .. literalinclude :: _Repository/_BasicRepository.php
31+ :caption: EXT:tea/Classes/Domain/Repository/Product/TeaRepository.php
4632
4733We override the protected parameter :php: `$defaultOrderings ` here. This parameter
4834is also defined in the parent class
4935:php: `TYPO3\C MS\E xtbase\P ersistence\R epository ` and used here when querying
5036the database.
5137
52- The trait itself is also defined in the extension:
53-
54- .. include :: /CodeSnippets/Tutorials/Tea/Classes/Domain/Repository/StoragePageAgnosticTrait.rst.txt
55-
56- Here we inject the :php: `$querySettings ` and allow to fetch tea objects from
57- all pages. We then set these as default query settings.
38+ Then we also add a custom find-by method. See also chapter
39+ :ref: `"Repository" in the Extbase reference <extbase-repository >`.
5840
59- The advantage of using a trait here instead of defining the parameters and
60- methods directly in the repository is, that the code can be reused without
61- requiring inheritance. Repositories of non-related models should not inherit
62- from each other.
41+ .. _extbase_tutorial_tea_repository-usage :
6342
6443Using the repository
6544====================
6645
67- The :php: `\T TN\T ea\D omain\R epository\P roduct\T eaRepository ` can now be used in a
68- controller or another class.
46+ The :php-short : `\T TN\T ea\D omain\R epository\P roduct\T eaRepository ` can now be
47+ used in a controller or another class, for example a service .
6948
70- Require it via :ref: `Dependency Injection <Dependency-Injection >`:
49+ Require it via :ref: `Dependency Injection <Dependency-Injection >` in the
50+ constructor:
7151
72- .. include :: /CodeSnippets/Tutorials/Tea/Classes/Domain/Repository/InjectRepository.rst.txt
73-
74- Then it can be used:
75-
76- .. include :: /CodeSnippets/Tutorials/Tea/Classes/Domain/Repository/UseRepository.rst.txt
52+ .. literalinclude :: _Repository/_InjectRepository.php
53+ :caption: EXT:tea/Classes/Controller/TeaController.php
7754
7855The method :php: `$this->teaRepository->findAll() ` that is called here is
79- defined in the parent class :php: `Repository `.
80-
81- You can also add additional methods here to query the database. See chapter
82- :ref: `"Repository" in the Extbase reference <extbase-repository >`. As this
83- example is very basic we do not need custom find methods.
56+ defined in the parent class :php: `\T YPO3\C MS\E xtbase\P ersistence\R epository `.
0 commit comments