forked from TYPO3-Documentation/TYPO3CMS-Reference-CoreApi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Create stub for DTO explanation (TYPO3-Documentation#4823)
* [TASK] Create stub for DTO explanation This is a basic first step definition of a DTO Addresses TYPO3-Documentation#4822 * [TASK] CGL
- Loading branch information
1 parent
12c3ac1
commit bcf3b75
Showing
2 changed files
with
125 additions
and
0 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
88 changes: 88 additions & 0 deletions
88
Documentation/ExtensionArchitecture/BestPractises/_dto.php
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,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
// The actual "plain" DTO, just setters and getters | ||
class PersonDTO | ||
{ | ||
protected string $first_name; | ||
protected string $last_name; | ||
|
||
public function getFirstName(): string | ||
{ | ||
return $this->first_name; | ||
} | ||
|
||
public function setFirstName(string $first_name): void | ||
{ | ||
$this->first_name = $first_name; | ||
} | ||
|
||
public function getLastName(): string | ||
{ | ||
return $this->last_name; | ||
} | ||
|
||
public function setLastName(string $last_name): void | ||
{ | ||
$this->last_name = $last_name; | ||
} | ||
} | ||
|
||
// The Extbase domain model entity. | ||
// Note that the getters and setters can easily be mapped | ||
// to the DTO due to their same names! | ||
class Person extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity | ||
{ | ||
protected string $first_name; | ||
protected string $last_name; | ||
|
||
public function getFirstName(): string | ||
{ | ||
return $this->first_name; | ||
} | ||
|
||
public function setFirstName(string $first_name): void | ||
{ | ||
$this->first_name = $first_name; | ||
} | ||
|
||
public function getLastName(): string | ||
{ | ||
return $this->last_name; | ||
} | ||
|
||
public function setLastName(string $last_name): void | ||
{ | ||
$this->last_name = $last_name; | ||
} | ||
} | ||
|
||
// An Extbase controller utilizing DTO-to-entity transfer | ||
class DtoController extends TYPO3\CMS\Extbase\Mvc\Controller\ActionController | ||
{ | ||
public function __construct(protected MyVendor\MyExtension\Domain\Repository\PersonRepository $personRepository) {} | ||
|
||
public function createAction(): Psr\Http\Message\ResponseInterface | ||
{ | ||
// Set up a DTO to be filled with input data. | ||
// The Fluid template would use <f:form> and its helpers. | ||
$this->view->assign('personDTO', new PersonDTO()); | ||
} | ||
|
||
public function saveAction(PersonDTO $personDTO): Psr\Http\Message\ResponseInterface | ||
{ | ||
// Transfer all data to a proper Extbase entity. | ||
// Create an empty entity first: | ||
$person = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(Person::class); | ||
|
||
// Use setters/getters for propagation | ||
$person->setFirstName($personDTO->getFirstName()); | ||
$person->setLastName($personDTO->getLastName()); | ||
|
||
// Persist the extbase entity | ||
$this->personRepository->add($person); | ||
|
||
// The "old" DTO needs to further processing. | ||
} | ||
} |