Skip to content

Commit aabb43b

Browse files
committed
[Software maintenance - Perfective]
* C.R.U.D implemented * Fix POST and UPDATE request to save/update data.
1 parent c143792 commit aabb43b

File tree

4 files changed

+60
-43
lines changed

4 files changed

+60
-43
lines changed

app/src/Entity/AbstractEntity.php

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,45 @@ public function getAvoidedFields() {
1616
}
1717

1818
public function toArray() {
19-
$temp = ( array ) $this;
19+
try{
2020

21-
$array = array ();
21+
$temp = ( array ) $this;
2222

23-
foreach ( $temp as $k => $v ) {
24-
$k = preg_match ( '/^\x00(?:.*?)\x00(.+)/', $k, $matches ) ? $matches [1] : $k;
25-
if (in_array ( $k, $this->getAvoidedFields () )) {
26-
$array [$k] = "";
27-
} else {
23+
$array = array ();
2824

29-
// if it is an object recursive call
30-
if (is_object ( $v ) && $v instanceof AbstractEntity) {
31-
$array [$k] = $v->toArray();
32-
}
33-
// if its an array pass por each item
34-
if (is_array ( $v )) {
35-
36-
foreach ( $v as $key => $value ) {
37-
if (is_object ( $value ) && $value instanceof AbstractEntity) {
38-
$arrayReturn [$key] = $value->toArray();
39-
} else {
40-
$arrayReturn [$key] = $value;
25+
foreach ( $temp as $k => $v ) {
26+
$k = preg_match ( '/^\x00(?:.*?)\x00(.+)/', $k, $matches ) ? $matches [1] : $k;
27+
if (in_array ( $k, $this->getAvoidedFields () )) {
28+
$array [$k] = "";
29+
} else {
30+
31+
// if it is an object recursive call
32+
if (is_object ( $v ) && $v instanceof AbstractEntity) {
33+
$array [$k] = $v->toArray();
34+
}
35+
// if its an array pass por each item
36+
if (is_array ( $v )) {
37+
38+
foreach ( $v as $key => $value ) {
39+
if (is_object ( $value ) && $value instanceof AbstractEntity) {
40+
$arrayReturn [$key] = $value->toArray();
41+
} else {
42+
$arrayReturn [$key] = $value;
43+
}
4144
}
45+
$array [$k] = $arrayReturn;
46+
}
47+
// if it is not a array and a object return it
48+
if (! is_object ( $v ) && !is_array ( $v )) {
49+
$array [$k] = $v;
4250
}
43-
$array [$k] = $arrayReturn;
44-
}
45-
// if it is not a array and a object return it
46-
if (! is_object ( $v ) && !is_array ( $v )) {
47-
$array [$k] = $v;
4851
}
4952
}
50-
}
5153

52-
return $array;
54+
return $array;
55+
56+
}catch (\Exception $e){
57+
throw new \Exception('Erro ao converter objeto para array'.$e->getMessage());
58+
}
5359
}
5460
}

app/src/Entity/Menu.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Menu extends AbstractEntity
3939
/**
4040
* @var \App\Entity\Menu
4141
*
42-
* @ORM\ManyToOne(targetEntity="App\Entity\Menu")
42+
* @ORM\ManyToOne(targetEntity="App\Entity\Menu", inversedBy="menus", cascade={"persist"})
4343
* @ORM\JoinColumns({
4444
* @ORM\JoinColumn(name="menu_id", referencedColumnName="id")
4545
* })
@@ -49,7 +49,7 @@ class Menu extends AbstractEntity
4949
/**
5050
* @var ArrayCollection|Menu[]
5151
*
52-
* @ORM\OneToMany(targetEntity="Menu", mappedBy="menu")
52+
* @ORM\OneToMany(targetEntity="Menu", mappedBy="menu", cascade={"all"}, orphanRemoval=true)
5353
*/
5454
private $menus;
5555

app/src/Resource/MenuResource.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use App\Entity\Menu;
1313
use Doctrine\ORM\EntityManager;
1414
use Doctrine\ORM\Query;
15+
use Doctrine\ORM\Query\Expr\Join;
1516
use Slim\Http\Request;
1617
use Slim\Http\Response;
1718

@@ -66,9 +67,25 @@ public function service(Request $request, Response $response, $args) {
6667
function get(Request $request, $args) {
6768
$data = new \stdClass();
6869

69-
if($request->getParam("id")!=null){
70-
$menuEdit = $this->entityManager->getRepository($this->REPOSITORY)->findOneBy(array('id'=>$request->getParam('id')));
71-
$data->menuEdit = $menuEdit->toArray();
70+
if($request->getParam('id')!=null){
71+
72+
$queryBuilder = $this->entityManager->createQueryBuilder();
73+
$queryBuilder->select("m.id,m.description,m.enabled, IDENTITY(m.menu) as parent")
74+
->from($this->REPOSITORY, 'm')
75+
->join($this->REPOSITORY,'m1', Join::WITH,'m.id = m1.id')
76+
->where('m.id = :id')->setParameter('id',$request->getParam('id'));
77+
78+
79+
80+
81+
//$queryBuilder->select('m')->from($this->REPOSITORY,'m')->where('m.id = :id')->setParameter('id',$request->getParam('id'));
82+
//$menuEdit = $this->entityManager->getRepository($this->REPOSITORY)->findOneBy(array('id'=>$request->getParam('id')));
83+
84+
$query = $queryBuilder->getQuery();
85+
86+
$data->menuEdit = $query->getArrayResult();
87+
88+
//$data->menuEdit = $menuEdit->toArray();
7289
}
7390
$menus = $this->entityManager->getRepository($this->REPOSITORY)->findAll();
7491
$data->menus = $menus;

app/templates/admin-cms/partials/script/menu-scripts.twig

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,14 @@
2626
$("#chkStatus").val('true');
2727
}
2828
29-
if(!$('#txtMenuEdit')){
29+
if($('#txtMenuEdit')){
3030
$.ajax({
3131
url: ENDPOINT, //this is the submit URL
3232
type: METHOD_PUT, //or POST
3333
data: $('#formMenu').serialize(),
3434
success: function (data) {
3535
$('#formMenu').trigger("reset");
36-
toastr.success('Registro atualizado com sucesso.').success(
37-
setTimeout(function(){
38-
location.reload();
39-
}, 5000)
40-
41-
); //TODO Twig Ajax
42-
36+
toastr.success('Registro atualizado com sucesso.');
4337
},
4438
error: function (request, status, error) {
4539
var message = 'Erro na operação. {0} \nStatus: {1} \nErro: {2}'.format(request.responseText, status, error);
@@ -101,17 +95,17 @@
10195
success: function (response) {
10296
$('#formMenu').trigger("reset");
10397
104-
var selected = response.menuEdit.menu;
98+
var selected = response.menuEdit[0];
10599
106100
if(selected){
107101
$("#txtMenu").select2('data', { id:selected.id, text: selected.description});
108102
}else{
109103
$("#txtMenu").select2('data', { id:"", text: ""});
110104
}
111105
112-
$('#txtMenuEdit').val(response.menuEdit.id);
113-
$('#txtDescricao').val(response.menuEdit.description);
114-
$('#chkStatus').val(response.menuEdit.enabled);
106+
$('#txtMenuEdit').val(selected.id);
107+
$('#txtDescricao').val(selected.description);
108+
$('#chkStatus').val(selected.enabled);
115109
116110
},
117111
error: function (request, status, error) {

0 commit comments

Comments
 (0)