Skip to content

Commit b8342d4

Browse files
authored
[Crud] Adds ability to pass table options through CRUD (#114)
1 parent b7ebf65 commit b8342d4

File tree

9 files changed

+61
-28
lines changed

9 files changed

+61
-28
lines changed

plugins/crud/README.md

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ Once enabled, the following services may be injected into your controller action
5151
use MixerApi\Crud\Interfaces\{CreateInterface, ReadInterface, UpdateInterface, DeleteInterface, SearchInterface};
5252
```
5353

54-
| Interface | Injected Service | Use-cases |
55-
| ------------- | ------------- | ------------- |
56-
| CreateInterface | MixerApi\Crud\Service\Create | `add()` actions |
57-
| ReadInterface | MixerApi\Crud\Service\Read | `view()` actions |
58-
| UpdateInterface | MixerApi\Crud\Service\Update | `edit()` actions |
54+
| Interface | Injected Service | Use-cases |
55+
|-----------------|------------------------------|--------------------|
56+
| CreateInterface | MixerApi\Crud\Service\Create | `add()` actions |
57+
| ReadInterface | MixerApi\Crud\Service\Read | `view()` actions |
58+
| UpdateInterface | MixerApi\Crud\Service\Update | `edit()` actions |
5959
| DeleteInterface | MixerApi\Crud\Service\Delete | `delete()` actions |
60-
| SearchInterface | MixerApi\Crud\Service\Search | `index()` actions |
60+
| SearchInterface | MixerApi\Crud\Service\Search | `index()` actions |
6161

6262
All Crud services infer the table name from the controller, you can change the table name by calling the
6363
`setTableName($name)` method.
@@ -76,6 +76,16 @@ public function add(CreateInterface $create)
7676
}
7777
```
7878

79+
Note, `save()` with `$options` is supported.
80+
81+
```php
82+
return $create->save($this, [
83+
'accessibleFields' => [
84+
'password' => true,
85+
]
86+
]);
87+
```
88+
7989
### Read
8090

8191
```php
@@ -85,13 +95,18 @@ public function view(ReadInteface $read)
8595
}
8696
```
8797

98+
Note, `read()` with `$options` is supported.
99+
100+
```php
101+
return $read->save($this, ['contains' => ['OtherTable']]);
102+
```
103+
88104
Return a CakePHP `Query` object instead:
89105

90106
```php
91107
$query = $read->query($this)
92108
```
93109

94-
95110
### Update
96111

97112
```php
@@ -101,6 +116,16 @@ public function edit(UpdateInterface $update)
101116
}
102117
```
103118

119+
Note, `update()` with `$options` is supported.
120+
121+
```php
122+
return $update->save($this, [
123+
'accessibleFields' => [
124+
'password' => true,
125+
]
126+
]);
127+
```
128+
104129
### Delete
105130

106131
```php
@@ -110,6 +135,12 @@ public function delete(DeleteInterface $delete)
110135
}
111136
```
112137

138+
Note, `delete()` with `$options` is supported.
139+
140+
```php
141+
return $delete->delete($this, ['atomic' => false]);
142+
```
143+
113144
### Search
114145

115146
The Search service works with [Pagination](https://book.cakephp.org/4/en/controllers/components/pagination.html) and
@@ -153,13 +184,13 @@ operations and will not run for non-crud operations. See [Options](#plugin-optio
153184
Allowed methods is handled by a `Controller.initialize` listener. See [Plugin Options](#plugin-options) for disabling or
154185
modifying the defaults.
155186

156-
| Action | HTTP method(s) |
157-
| ------------- | ------------- |
158-
| index() | GET |
159-
| view() | GET |
160-
| add() | POST |
161-
| edit() | POST, PUT, and PATCH |
162-
| delete() | DELETE |
187+
| Action | HTTP method(s) |
188+
|----------|----------------------|
189+
| index() | GET |
190+
| view() | GET |
191+
| add() | POST |
192+
| edit() | POST, PUT, and PATCH |
193+
| delete() | DELETE |
163194

164195
You may also call `setAllowMethods($methods)` on any service to overwrite the default behavior. This accepts a string
165196
or any array as an argument just like the native `$request->allowedMethods()`.

plugins/crud/src/Interfaces/CreateInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ interface CreateInterface
1515
* Creates the resource
1616
*
1717
* @param \Cake\Controller\Controller $controller the cakephp controller instance
18+
* @param \Cake\ORM\SaveOptionsBuilder|\ArrayAccess|array $options The options to use when saving.
1819
* @return \Cake\Datasource\EntityInterface
1920
*/
20-
public function save(Controller $controller): EntityInterface;
21+
public function save(Controller $controller, $options = []): EntityInterface;
2122

2223
/**
2324
* @param string $table the table name

plugins/crud/src/Interfaces/DeleteInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ interface DeleteInterface
1515
*
1616
* @param \Cake\Controller\Controller $controller the cakephp controller instance
1717
* @param mixed $id an optional identifier, if null the id parameter is used from the request
18+
* @param \ArrayAccess|array $options The options for the delete.
1819
* @return $this
1920
*/
20-
public function delete(Controller $controller, mixed $id = null);
21+
public function delete(Controller $controller, mixed $id, $options);
2122

2223
/**
2324
* @param string $table the table

plugins/crud/src/Interfaces/ReadInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ interface ReadInterface
1717
*
1818
* @param \Cake\Controller\Controller $controller the cakephp controller instance
1919
* @param mixed $id an optional identifier, if null the id parameter is used from the request
20+
* @param array<string, mixed> $options options accepted by `Table::find()`
2021
* @return \Cake\Datasource\EntityInterface
2122
*/
22-
public function read(Controller $controller, mixed $id = null): EntityInterface;
23+
public function read(Controller $controller, mixed $id = null, $options = []): EntityInterface;
2324

2425
/**
2526
* @param string $table the table

plugins/crud/src/Interfaces/UpdateInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ interface UpdateInterface
1616
*
1717
* @param \Cake\Controller\Controller $controller the cakephp controller instance
1818
* @param mixed $id an optional identifier, if null the id parameter is used from the request
19+
* @param \Cake\ORM\SaveOptionsBuilder|\ArrayAccess|array $options The options to use when saving.
1920
* @return \Cake\Datasource\EntityInterface
2021
*/
21-
public function save(Controller $controller, mixed $id = null): EntityInterface;
22+
public function save(Controller $controller, mixed $id = null, $options = []): EntityInterface;
2223

2324
/**
2425
* @param string $table the table

plugins/crud/src/Services/Create.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(
3232
/**
3333
* @inheritDoc
3434
*/
35-
public function save(Controller $controller): EntityInterface
35+
public function save(Controller $controller, $options = []): EntityInterface
3636
{
3737
$this->allowMethods($controller);
3838
$table = $controller->getTableLocator()->get($this->whichTable($controller));
@@ -42,7 +42,7 @@ public function save(Controller $controller): EntityInterface
4242
$this->deserializer->deserialize($controller->getRequest())
4343
);
4444

45-
$entity = $table->save($entity);
45+
$entity = $table->save($entity, $options);
4646

4747
if (!$entity) {
4848
throw new ResourceWriteException(null, "Unable to save $this->tableName resource.");

plugins/crud/src/Services/Delete.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public function __construct(
3030
/**
3131
* @inheritDoc
3232
*/
33-
public function delete(Controller $controller, mixed $id = null)
33+
public function delete(Controller $controller, mixed $id = null, $options = [])
3434
{
3535
$this->allowMethods($controller);
3636
$id = $this->whichId($controller, $id);
3737
$entity = $this->read->read($controller, $id);
3838

3939
$table = $controller->getTableLocator()->get($this->whichTable($controller));
40-
if (!$table->delete($entity)) {
40+
if (!$table->delete($entity, $options)) {
4141
throw new ResourceWriteException($entity, "Unable to delete {$table->getAlias()} resource.");
4242
}
4343

plugins/crud/src/Services/Read.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@ class Read implements ReadInterface
2020
/**
2121
* @inheritDoc
2222
*/
23-
public function read(Controller $controller, mixed $id = null): EntityInterface
23+
public function read(Controller $controller, mixed $id = null, $options = []): EntityInterface
2424
{
2525
$this->allowMethods($controller);
2626
$id = $this->whichId($controller, $id);
2727
$table = $controller->getTableLocator()->get($this->whichTable($controller));
2828

29-
return $table->get($id, [
30-
'contain' => [],
31-
]);
29+
return $table->get($id, $options);
3230
}
3331

3432
/**

plugins/crud/src/Services/Update.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(
3535
/**
3636
* @inheritDoc
3737
*/
38-
public function save(Controller $controller, mixed $id = null): EntityInterface
38+
public function save(Controller $controller, mixed $id = null, $options = []): EntityInterface
3939
{
4040
$this->allowMethods($controller);
4141

@@ -48,7 +48,7 @@ public function save(Controller $controller, mixed $id = null): EntityInterface
4848
$this->deserializer->deserialize($controller->getRequest())
4949
);
5050

51-
$entity = $table->save($entity);
51+
$entity = $table->save($entity, $options);
5252

5353
if (!$entity) {
5454
throw new ResourceWriteException(null, "Unable to save $this->tableName resource.");

0 commit comments

Comments
 (0)