Now we have the table example_crud
created in the previous tutorial,
let's modify it to add a new field status
.
We need to add the proper field in the up
script and remove it in the down
script.
db/migrations/up/00003.sql
:
alter table example_crud
add status varchar(10) null;
db/migrations/down/00002.sql
:
alter table example_crud
drop column status;
APP_ENV=dev composer run migrate -- update
Open the file: src/Model/ExampleCrud.php
and add the field status
:
...
/**
* @var string|null
*/
#[OA\Property(type: "string", format: "string", nullable: true)]
protected ?string $status = null;
*/
public function getStatus(): ?string
{
return $this->status;
}
/**
* @param string|null $status
* @return ExampleCrud
*/
public function setStatus(?string $status): ExampleCrud
{
$this->status = $status;
return $this;
}
...
As we are just adding a new field, and we already updated the Model to support this new field
we don't need to change the Repository
class.
We just need to allow the rest receive the new field. If we don't do it the API will throw an error.
Open the file: src/Rest/ExampleCrudRest.php
and add the attribute status
to method postExampleCrud()
:
#[OA\RequestBody(
description: "The object DummyHex to be created",
required: true,
content: new OA\JsonContent(
required: [ "name" ],
properties: [
new OA\Property(property: "name", type: "string", format: "string"),
new OA\Property(property: "birthdate", type: "string", format: "date-time", nullable: true),
new OA\Property(property: "code", type: "integer", format: "int32", nullable: true),
new OA\Property(property: "status", type: "string", format: "string", nullable: true) # <-- Add this line
]
)
)]
public function postExampleCrud(HttpResponse $response, HttpRequest $request)
We only need to change our method getSample()
to return the status.
Open the file: tests/Functional/Rest/ExampleCrudTest.php
protected function getSampleData($array = false)
{
$sample = [
'name' => 'name',
'birthdate' => '2023-01-01 00:00:00',
'code' => 1,
'status' => 'status', # <-- Add this line
];
...
composer run openapi
If everything is ok, the tests should pass:
composer run test