Skip to content

Commit 0829694

Browse files
chore: update controller and model for relationship
1 parent bc3ff3a commit 0829694

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/Controller/AbstractController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ public function findByColumn($req, $res, $args) {
2929

3030
/** Delete Data by Id */
3131
public function delete($req, $res, $args) {
32-
$data = $this->_model->delete($args['id']);
32+
$data = $this->_model->delete($args);
3333
checkNull($data, $req);
3434
return response($req, $res, new Response(message: "This item has been successfully removed.", data: $data));
3535
}
3636

3737
/** Insert Data */
3838
public function insert($req, $res, $args) {
39-
$data = $this->_model->insert($req->getParsedBody());
39+
$data = $this->_model->insert([...$req->getParsedBody(), ...$args ]);
4040
return response($req, $res, new Response(message: "This item has been successfully added.", data: $data));
4141
}
4242

4343
/** Update by Id */
4444
public function update($req, $res, $args) {
45-
$data = $this->_model->update($req->getParsedBody(), $args["id"]);
45+
$data = $this->_model->update($req->getParsedBody(), $args);
4646
checkNull($data, $req);
4747
return response($req, $res, new Response(message: "This item has been successfully updated.", data: $data));
4848
}

src/Model/AbstractModel.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public function findAll(): array {
2727
}
2828

2929
/** Find Data by Conditional */
30-
public function findBy($array): array {
31-
return $this->repository->findBy($array);
30+
public function findBy(array $args): array {
31+
return $this->repository->findBy($args);
3232
}
3333

3434
/** Find Data by Id */
@@ -37,25 +37,30 @@ public function findById($id) {
3737
}
3838

3939
/** Delete Data by Id */
40-
public function delete(string $id) {
41-
$data = $this->repository->find($id);
42-
$this->entityManager->remove($data);
43-
$this->entityManager->flush();
40+
public function delete(array $args) {
41+
$data = $this->repository->findBy($args)[0] ?? null;
4442

45-
return $data;
43+
if ($data):
44+
$this->entityManager->remove($data);
45+
$this->entityManager->flush();
46+
47+
return $data;
48+
endif;
4649
}
4750

4851
/** Update Data by Id */
49-
public function update(array $args, string $id) {
50-
$data = $this->repository->find($id);
52+
public function update(array $data, array $args) {
53+
$item = $this->repository->findBy($args)[0] ?? null;
5154

52-
foreach ($args as $key => $value)
53-
$data->__set($key, $value);
55+
if ($item):
56+
foreach ($data as $key => $value)
57+
$item->__set($key, $value);
5458

55-
$this->entityManager->merge($data);
56-
$this->entityManager->flush();
59+
$this->entityManager->merge($item);
60+
$this->entityManager->flush();
5761

58-
return $data;
62+
return $data;
63+
endif;
5964
}
6065

6166
/** Insert Data */

0 commit comments

Comments
 (0)