Data is created according to schemas and after the data values pass through input filters, safe inputs are obtained through swagger schemas and filtering. If data of array type comes from the http header as follows;
src/App/Schema/Employees/EmployeeSave.php
/**
* @var array
* @OA\Property(
* type="array",
* @OA\Items(
* @OA\Property(
* property="childId",
* type="string",
* ),
* @OA\Property(
* property="childName",
* type="string",
* ),
* @OA\Property(
* property="childBirthdate",
* type="string",
* ),
* ),
* );
*/
public $employeeChildren;
{
"employeeId": "string",
"name": "string",
"surname": "string",
"employeeChildren": [
{
"childId" : "string",
"childNameSurname" : "string"
}
]
}
[
"employeeId" => "string",
"name" => "string",
"surname" => "string",
"employeeChildren" => [
[
"childId" => "string",
"childNameSurname" => "string",
]
],
]
The data obtained from the employeeChildren array is recorded as in the following example.
In the example below, the array type employeeChildren data is recorded with the foreach statement in the create method.
src/App/Model/EmployeeModel.php
public function create(array $data)
{
$employeeId = $data['id'];
try {
$this->conn->beginTransaction();
$data['employees']['employeeId'] = $employeeId;
$data['employees']['createdAt'] = date('Y-m-d H:i:s');
$this->employees->insert($data['employees']);
// children
if (! empty($data['employeeChildren'])) {
foreach ($data['employeeChildren'] as $val) {
$val['employeeId'] = $employeeId;
$this->employeeChildren->insert($val);
}
}
$this->conn->commit();
} catch (Exception $e) {
$this->conn->rollback();
throw $e;
}
}
In the following example, the array type employeeChildren data is recorded with the foreach statement in the update method.
src/App/Model/EmployeeModel.php
public function update(array $data)
{
$employeeId = $data['id'];
try {
$this->conn->beginTransaction();
$this->employees->update($data['employees'], ['employeeId' => $employeeId]);
// delete children
//
$this->employeeChildren->delete(['employeeId' => $employeeId]);
if (! empty($data['employeeChildren'])) {
foreach ($data['employeeChildren'] as $val) {
$val['employeeId'] = $employeeId;
$this->employeeChildren->insert($val);
}
}
$this->conn->commit();
} catch (Exception $e) {
$this->conn->rollback();
throw $e;
}
}