Skip to content

Commit

Permalink
closes #1014
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Oct 24, 2016
1 parent aa87342 commit 9bbac7d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
17 changes: 17 additions & 0 deletions docs/guide1.0/ngrest-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,23 @@ public function ngRestConfig($config)
}
```

## Crud Relation Tabs

Sometimes its usefull and common to directly manage relational data inside the current ngrest crud. Therefore we have created something called `ngRestRelations()`. Inside this method you can define relations which are also based on the NgRest concept.

```php
public function ngRestRelation()
{
return [
['label' => 'The Label', 'apiEndpoint' => \path\to\ngRest\Model::ngRestApiEndpoint(), 'dataProvider' => $this->getSales()],
];
}
```

The above example will use the `getSales()` method of the current model where you are implementing this relation. The `getSales()` must return an {{yii\db\QueryInterface}} Object, for example you can use `$this->hasMany(Model, ['key' => 'rel'])` or `new \yii\db\Query()`.

> Tip: If you generate an NgRest model for a relation which is not used in any other situations you can hide those items from the menu, but not from the permission system. To hide en element add the hiddenInMenu option in the `getMenu()` method of the module as following: `itemApi('name', 'route', 'icon', 'api', ['hiddenInMenu' => true])`.
## Soft Deletion

We have also added a soft delete trait which is going to override the default implementation of the `delete` method. When enabled and configure, the soft delete trait will only mark the datarecord to `is_deleted = 1` instead of removing it from the database.
Expand Down
2 changes: 1 addition & 1 deletion modules/admin/src/ngrest/base/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function actionRelationCall($arrayIndex, $id, $modelClass)
throw new InvalidCallException("unable to resolve relation call model.");
}

$func = $model->ngRestRelation()[$arrayIndex]['dataProvider'];
$func = $model->ngRestRelations()[$arrayIndex]['dataProvider'];

return new ActiveDataProvider([
'query' => $func,
Expand Down
2 changes: 1 addition & 1 deletion modules/admin/src/ngrest/base/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function actionIndex($inline = false, $relation = false, $arrayIndex = fa
$config->groupByField = $this->model->ngRestGroupByField();

$rel = [];
foreach ($this->model->ngRestRelation() as $key => $item) {
foreach ($this->model->ngRestRelations() as $key => $item) {
$rel[] = ['label' => $item['label'], 'apiEndpoint' => $item['apiEndpoint'], 'arrayIndex' => $key, 'modelClass' => base64_encode($this->model->className())];
}

Expand Down
13 changes: 6 additions & 7 deletions modules/admin/src/ngrest/base/NgRestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,20 @@ public function ngRestGroupByField()
* Example of how to use two relation buttons based on models which as to be ngrest model as well with apis!
*
* ```php
* public function ngRestRelation()
* public function ngRestRelations()
* {
* return [
* 'Reservations' => ['api' => \app\models\Orders::ngRestApiEndpoint(), 'where' => ['order_id' => '{{id}}', 'is_reservation' => 0]],
* 'Sales' => ['api' => \app\models\Orders::ngRestApiEndpoint(), 'where' => ['order_id' => '{{id}}', 'is_reservation' => 1]]
* ];
* ['label' => 'The Label', 'apiEndpoint' => \path\to\ngRest\Model::ngRestApiEndpoint(), 'dataProvider' => $this->getSales()],
* ];
* }
* ```
*
* The above example assumes the model where you add those relations is the the table where `order_id` is the primary key. So its like a `hasMany`
* relation based on this table.
* The above example will use the `getSales()` method of the current model where you are implementing this relation. The `getSales()` must return
* an {{yii\db\QueryInterface}} Object, for example you can use `$this->hasMany(Model, ['key' => 'rel'])` or `new \yii\db\Query()`.
*
* @return array
*/
public function ngRestRelation()
public function ngRestRelations()
{
return [];
}
Expand Down

0 comments on commit 9bbac7d

Please sign in to comment.