新增

需求:新增角色的时候给角色添加一些默认的权限或新增权限

1.Controller:

修改src\Controller目录下的RolesController.php,添加代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?php
/**
 *添加数据的action,会接受POST请求的处理
 */
public function add()
{
    $data = json_decode($this->request->getBody(), true);
    $result = RolesLogic::instance()->add($data);
    $this->responseOk($result);
}

Tip

json_decode( string $json, bool $assoc = false )

当 $assoc=true 时,将返回 array
当 $assoc=false 时,将返回 object

2.Logic:

修改src\Model\Logic目录下的RolesLogic.php,继承BaseLogic,添加代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
namespace App\Model\Logic\System;

use Cake\ORM\TableRegistry;
use App\Model\Logic\BaseLogic;
use Cake\Datasource\Exception\RecordNotFoundException;

class RolesLogic extends BaseLogic
{

······

/**
 * 新增角色
 */
public function add($data)
{
    $newData =  $this->camelToUnderscore($data);
    $entity = $this->rolesTable->newEntity($newData, ['associated' => ['Permissions']]);
    $result = $this->rolesTable->save($entity);
    return $result;
}

2.1 驼峰与下划线互转

详细转换代码请参考 demo 项目中的src\Model\Logic\BaseLogic.php

1
2
3
4
5
<?php
// 驼峰转下划线
$newData = BaseLogic::camelToUnderscore($data);
// 下划线转驼峰
$newData = BaseLogic::underscoreToCamel($data);

在使用newEntity()和patchEntity()前,请进行驼峰转下划线的操作。

2.2 newEntity()

CakeORM\Table::newEntity($data = null, array $options = [])

$data的格式,例如:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// $data->permissions为数据库中不存在的数据时,permissions内的数据会被保存至数据库
{
    "name": "默认角色",
    "status": 0,
    "permissions": [
        {
            "parentId": 2,
            "name": "一",
            "method": "POST",
            "url": "POST",
            "code": "101"
        },
        {
            "parentId": 1,
            "name": "六",
            "method": "POST",
            "url": "POST",
            "code": "106"
        }
    ]
}

// $data->permissions为数据库中已存在的数据时,permissions只需提供id列表即可,属性名"_ids"不可修改
{
    "name": "默认角色",
    "status": 0,
    "permissions": [
        "_ids" : [
            1,2,3
        ]
    ]
}

// $data->permissions部分为数据库中已存在的数据,部分不存在时,permissions属性参考以下json
{
    "name": "默认角色",
    "status": 0,
    "permissions": [
        {
            "parentId": 2,
            "name": "一",
            "method": "POST",
            "url": "POST",
            "code": "101"
        },
        {
            "id": 1
        }
    ]
}

使用$data

1
2
3
4
<?php
$newData =  $this->camelToUnderscore($data);
$entity = $this->rolesTable->newEntity($newData, ['associated' => ['Permissions']]);
$result = $this->rolesTable->save($entity);

2.3 patchEntity()

CakeORM\Table::patchEntity(EntityInterface $entity, array $data, array $options = [])

$data数据格式同 2.2 newEntity()

1
2
3
4
<?php
$newData =  $this->camelToUnderscore($data);
$entity = $this->rolesTable->patchEntity($entity, $newData, ["associated" =>"Permissions"]);
$result = $this->rolesTable->save($entity);

2.4 save()

CakeORM\Table::save(EntityInterface $entity, $options = [])

  • $entity:需要保存的实体类

  • $options:可选项

    • atomic:是否在数据库事务中执行保存和回调(默认值:true),使用save函数时会默认启用事务进行保存,当需要手动开启事务时,可将此项设置为false。

    • checkRules:是否在保存前检查实体上的规则,如果检查失败,将中止保存操作。(默认值:true)

    • associated: 当值为false时,关联的数据不会被保存。值为true时,将保存在传递的'$entity'中找到的第一级关联实体(默认值:true)。当值为数组(array)的时候,它将被解释为要保存的关联列表,这样就可以保存多级数据。例如:

    1
    2
    3
    4
    5
    6
    7
    8
    <?php
    $companies->save($entity, [
      'associated' => [
        'Employees' => [
          'associated' => ['Addresses']
        ]
      ]
    ]);
    
    • checkExisting:检查实体是否已经存在。(默认值:true)

2.5 saveMany()

CakeORM\Table::saveMany($entities, $options = [])

  • $entities:需要保存的实体类列表

  • $options:可选项,同2.4 save()的options

3.结果

使用postman访问 http://localhost:8000/system/roles 查看结果: php api