Skip to content

Commit c09f542

Browse files
Improved primary key
1 parent 8e59329 commit c09f542

File tree

3 files changed

+93
-90
lines changed

3 files changed

+93
-90
lines changed

ResourceControllerTrait.php

+21-20
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,22 @@ trait ResourceControllerTrait {
1919
protected $resource = null;
2020
public $queryParser;
2121

22-
public function _getResourceName() : string {
23-
if($this->resource)
22+
public function _getResourceName(): string {
23+
if ($this->resource) {
2424
return $this->resource;
25-
else
26-
return str_replace('Controllers', 'Models', singular(get_class($this)).'Model');
25+
} else {
26+
return str_replace('Controllers', 'Models', singular(get_class($this)) . 'Model');
27+
}
2728
}
2829

2930
public function get($id = 0) {
3031
/** @var Model|ResourceBaseModelInterface|ResourceModelInterface $model */
3132
$className = $this->_getResourceName();
3233
$model = new $className();
3334
$items = $model->restGet($id, $this->queryParser);
34-
if($id)
35+
if ($id) {
3536
$this->_setResource($items->first());
36-
else {
37+
} else {
3738
$this->_setResources($items);
3839
}
3940
$this->success();
@@ -49,11 +50,11 @@ public function post() {
4950

5051
$data = $this->request->getJSON(true);
5152

52-
if(is_array($this->request->getJSON())) {
53+
if (is_array($this->request->getJSON())) {
5354

5455
/** @var Entity $resources */
5556
$resources = new $entityName();
56-
foreach($data as $dataItem) {
57+
foreach ($data as $dataItem) {
5758
$resources->add($entityName::post($dataItem));
5859
}
5960
$this->_setResources($resources);
@@ -78,12 +79,12 @@ public function put($id = 0) {
7879

7980
$data = $this->request->getJSON(true);
8081

81-
if(is_array($this->request->getJSON())) {
82+
if (is_array($this->request->getJSON())) {
8283

8384
/** @var Entity $resources */
8485
$resources = new $entityName();
85-
foreach($data as $dataItem) {
86-
$resources->add($entityName::put(isset($dataItem['id']) ? $dataItem['id'] : 0, $dataItem));
86+
foreach ($data as $dataItem) {
87+
$resources->add($entityName::put(isset($dataItem[$model->getPrimaryKey()]) ? $dataItem[$model->getPrimaryKey()] : 0, $dataItem));
8788
}
8889
$this->_setResources($resources);
8990

@@ -102,23 +103,24 @@ public function patch($id = 0) {
102103

103104
/** @var Model $model */
104105
$model = new $className();
106+
$primaryKey = $model->getPrimaryKey();
105107
/** @var Entity|ResourceEntityInterface $entityName */
106108
$entityName = $model->returnType;
107109

108110
$data = $this->request->getJSON(true);
109111

110-
if($id) {
112+
if ($id) {
111113

112114
$item = $entityName::patch($id, $data);
113115
$this->_setResource($item);
114116

115-
} else if(is_array($data)) {
117+
} else if (is_array($data)) {
116118

117119
/** @var Entity $resources */
118120
$resources = new $entityName();
119-
foreach($data as $dataItem) {
120-
if(isset($dataItem['id']) && $dataItem['id'] > 0)
121-
$resources->add($entityName::patch($dataItem['id'], $dataItem));
121+
foreach ($data as $dataItem) {
122+
if (isset($dataItem[$primaryKey]) && $dataItem[$primaryKey] > 0)
123+
$resources->add($entityName::patch($dataItem[$primaryKey], $dataItem));
122124
else
123125
$resources->add($entityName::post($dataItem));
124126
}
@@ -134,13 +136,13 @@ public function delete($id) {
134136

135137
/** @var Model|ResourceBaseModelInterface|ResourceModelInterface $model */
136138
$model = new $className();
137-
$model->where('id', $id);
139+
$model->where($model->getPrimaryKey(), $id);
138140

139141
/** @var Entity $item */
140142
$item = $model->find();
141143

142-
if($item->exists()) {
143-
if(!$model->isRestDeleteAllowed($item)) {
144+
if ($item->exists()) {
145+
if (!$model->isRestDeleteAllowed($item)) {
144146
$this->error(ErrorCodes::InsufficientAccess, 403);
145147
return;
146148
}
@@ -153,5 +155,4 @@ public function delete($id) {
153155
}
154156

155157

156-
157158
}

ResourceEntityTrait.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ public static function post($data) {
1818
Data::debug(get_called_class(), 'post');
1919
$className = get_called_class();
2020

21+
/** @var ResourceEntityInterface|Entity $item */
22+
$item = new $className();
23+
/** @var Model|ResourceBaseModelInterface|ResourceModelInterface $model */
24+
$model = $item->_getModel();
25+
2126
// NB !! Is this okay? Client want to send relations as objects. But post should always create.
2227
if(isset($data['id']) && $data['id'] > 0) {
2328
/** @var Entity $entity */
2429
$entity = new $className();
25-
$entity->find($data['id']); // Have to do a get, it might be saved later
30+
$entity->find($data[$model->getPrimaryKey()]); // Have to do a get, it might be saved later
2631
$entity->fill($data);
2732
return $entity;
2833
}
2934

30-
/** @var ResourceEntityInterface|Entity $item */
31-
$item = new $className();
32-
/** @var Model|ResourceBaseModelInterface|ResourceModelInterface $model */
33-
$model = $item->_getModel();
34-
3535
$item->populatePatch($data);
3636
if(!$model->isRestCreationAllowed($item)) {
3737
Data::debug(get_class($item), "ERROR", ErrorCodes::InsufficientAccess);
@@ -97,7 +97,7 @@ public static function put($id, $data) {
9797
$model = $item->_getModel();
9898

9999
$item = $model
100-
->where('id', $id)
100+
->where($model->getPrimaryKey(), $id)
101101
->find();
102102
$item->populatePut($data);
103103
if(!$model->isRestUpdateAllowed($item)) {
@@ -198,7 +198,7 @@ public static function patch($id, $data) {
198198
$model = $item->_getModel();
199199

200200
$item = $model
201-
->where('id', $id)
201+
->where($model->getPrimaryKey(), $id)
202202
->find();
203203
if($item->populatePatch($data)) {
204204
if(!$model->isRestUpdateAllowed($item)) {

0 commit comments

Comments
 (0)