Skip to content

Commit 2bb7274

Browse files
authored
[TASK] Update TeaController and Repository (#5411)
* [TASK] Update TeaController and Repository Resolves: #3864 Releases: main, 13.4 * [TASK] Update TeaController and Repository Resolves: #3864 Releases: main, 13.4
1 parent d561ccc commit 2bb7274

File tree

9 files changed

+78
-147
lines changed

9 files changed

+78
-147
lines changed

Documentation/CodeSnippets/Tutorials/Tea/Classes/Domain/Repository/InjectRepository.rst.txt

Lines changed: 0 additions & 17 deletions
This file was deleted.

Documentation/CodeSnippets/Tutorials/Tea/Classes/Domain/Repository/StoragePageAgnosticTrait.rst.txt

Lines changed: 0 additions & 27 deletions
This file was deleted.

Documentation/CodeSnippets/Tutorials/Tea/Classes/Domain/Repository/TeaRepository.rst.txt

Lines changed: 0 additions & 38 deletions
This file was deleted.

Documentation/CodeSnippets/Tutorials/Tea/Classes/Domain/Repository/UseRepository.rst.txt

Lines changed: 0 additions & 19 deletions
This file was deleted.

Documentation/ExtensionArchitecture/Tutorials/Tea/Controller.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ objects from the repository and hands them over to the view:
3636

3737
The controller has to access the :php:`TeaRepository` to find all available tea
3838
objects. We use :ref:`Dependency Injection <DependencyInjection>` to make the
39-
repository available to the controller: The method :php:`injectTeaRepository()`
39+
repository available to the controller: The constructor
4040
will be called automatically with an initialized :php:`TeaRepository` when
4141
the :php:`TeaController` is created.
4242

Documentation/ExtensionArchitecture/Tutorials/Tea/Repository.rst

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,72 +12,45 @@ A basic repository can be quite a short class. The shortest possible
1212
repository is an empty class inheriting from
1313
:php:`TYPO3\CMS\Extbase\Persistence\Repository`:
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

3018
The model the repository should deliver is derived from the namespace and
3119
name of the repository. A repository with the fully qualified name
3220
:php:`TTN\Tea\Domain\Repository\Product\TeaRepository` therefore delivers
3321
models with the fully qualified name :php:`TTN\Tea\Domain\Model\Product\Tea`
3422
without 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\Tea\Domain\Model\Product\Tea`.
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

4733
We override the protected parameter :php:`$defaultOrderings` here. This parameter
4834
is also defined in the parent class
4935
:php:`TYPO3\CMS\Extbase\Persistence\Repository` and used here when querying
5036
the 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

6443
Using the repository
6544
====================
6645

67-
The :php:`\TTN\Tea\Domain\Repository\Product\TeaRepository` can now be used in a
68-
controller or another class.
46+
The :php-short:`\TTN\Tea\Domain\Repository\Product\TeaRepository` 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

7855
The 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:`\TYPO3\CMS\Extbase\Persistence\Repository`.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TTN\Tea\Domain\Repository\Product;
6+
7+
use TYPO3\CMS\Extbase\Persistence\Repository;
8+
9+
/**
10+
* @extends Repository<Tea>
11+
*/
12+
class TeaRepository extends Repository {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TTN\Tea\Controller;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
use TTN\Tea\Domain\Repository\Product\TeaRepository;
9+
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
10+
11+
final class TeaController extends ActionController
12+
{
13+
public function __construct(private TeaRepository $teaRepository) {}
14+
15+
public function indexAction(): ResponseInterface
16+
{
17+
$this->view->assign('teas', $this->teaRepository->findAll());
18+
return $this->htmlResponse();
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TTN\Tea\Domain\Repository;
6+
7+
use TTN\Tea\Domain\Model\Tea;
8+
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
9+
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
10+
use TYPO3\CMS\Extbase\Persistence\Repository;
11+
12+
/**
13+
* @extends Repository<Tea>
14+
*/
15+
class TeaRepository extends Repository
16+
{
17+
protected $defaultOrderings = ['title' => QueryInterface::ORDER_ASCENDING];
18+
19+
public function findByOwnerUid(int $ownerUid): QueryResultInterface
20+
{
21+
$query = $this->createQuery();
22+
$query->setQuerySettings($query->getQuerySettings()->setRespectStoragePage(false));
23+
$query->matching($query->equals('ownerUid', $ownerUid));
24+
25+
return $query->execute();
26+
}
27+
}

0 commit comments

Comments
 (0)