DEMO¶
1 | git clone git@gitlab.freedomcz.com.cn:php/demo.git |
表之间的关联关系:

一、User的CURD¶
1. 新增user¶
在src\Model\Table目录下新建UsersTable.php,写入下面的代码:
1 2 3 4 5 6 7 8 9 10 11 | <?php namespace App\Model\Table; class UsersTable extends BaseTable { public function initialize(array $config) { parent::initialize($config); $this->setTable('users'); } } |
在src\Model\Entity下新建User.php,写入代码:
1 2 3 4 5 6 7 8 | <?php namespace App\Model\Entity; class User extends BaseEntity { // json序列化时 password等不参与序列化 protected $_hidden = ['password', 'created_by', 'created_time', 'modified_by', 'modified_time', '_joinData']; } |
在src\Model\Logic目录下新建UsersLogic.php,写入代码:
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 | <?php namespace App\Model\Logic; use Cake\ORM\TableRegistry; use App\Model\Logic\BaseLogic; use App\Model\Table\UsersTable; use Cake\Datasource\Exception\RecordNotFoundException; class UsersLogic extends BaseLogic { /** * @var UsersLogic */ protected static $instance; /** * @var UsersTable */ private $usersTable; public function __construct() { $this->usersTable = TableRegistry::getTableLocator()->get("Users"); } public static function getInstance() { if (self::$instance == null) { self::$instance = new UsersLogic(); } return self::$instance; } /** * 添加user */ public function add($data) { $newData = $this->camelToUnderscore($data); $entity = $this->usersTable->newEntity($newData); $this->usersTable->save($entity); return $this->underscoreToCamel($entity); } } |
在src\Controller目录下新建UsersController.php,写入代码:
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 | <?php namespace App\Controller; use App\Controller\AppController; use App\Model\Logic\UsersLogic; use Cake\Datasource\Exception\RecordNotFoundException; class UsersController extends AppController { /** * @var UsersLogic */ private $userLogic; public function initialize() { parent::initialize(); $this->Auth->allow(['view', 'add', 'index', 'delete', 'edit']); $this->userLogic = UsersLogic::getInstance(); } /** * 添加数据的action,会接受POST请求的处理 * 方法名固定,不可修改 */ public function add() { $data = $this->request->getData(); $result = $this->userLogic->add($data); $this->responseOk($result); } } |
使用POST方式访问:http://localhost:8000/users ,需要保存的数据如下所示:
1 2 3 4 5 6 7 8 9 | { "loginId": "admin", "phone": "12345678901", "password": "123456", "name": "admin", "avatar": "avatar", "remarks": "remarks", "status": 0 } |
结果如图所示:

2. 修改user¶
修改src\Model\Logic目录下的UsersLogic.php,添加代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php /** * 编辑user */ public function save($id, $data) { $newData = $this->camelToUnderscore($data); try { $entity = $this->usersTable->get($id); } catch (RecordNotFoundException $e) { return null; } $entity = $this->usersTable->patchEntity($entity, $newData); $this->usersTable->save($entity); return $this->underscoreToCamel($entity); } |
修改src\Controller目录下的UsersController.php,添加代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php /** * 会接受PUT以及PATCH的请求,并且会带上ID * 方法名固定,不可修改 */ public function edit($id) { $data = $this->request->getData(); try { $result = $this->userLogic->save($id, $data); } catch (RecordNotFoundException $e) { $this->responseError(); return; } $this->responseOk($result); } |
使用PUT方式访问:http://localhost:8000/users/1 ,修改的数据如下所示:
1 2 3 4 5 6 7 8 9 | { "loginId": "admin1", "phone": "12345678901", "password": "123456", "name": "admin1", "avatar": "avatar1", "remarks": "remarks1", "status": 0 } |
结果如图所示:

3. 查询user¶
修改src\Model\Logic目录下的UsersLogic.php,添加代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php /** * 根据id查询user */ public function findUser($id) { $user = $this->usersTable->find() ->where(['id =' => $id]) ->first(); return $this->underscoreToCamel($user); } /** * 查询所有的user */ public function findAll() { $users = $this->usersTable->find(); return $this->underscoreToCamel($users); } |
修改src\Controller目录下的UsersController.php,添加代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php /** * 带id的action,取得单条数据 * 方法名固定,不可修改 */ public function view($id) { $user = $this->userLogic->findUser($id); $this->responseOk($user); } /** * 取得所有数据 * 方法名固定,不可修改 */ public function index() { $users = $this->userLogic->findAll(); $this->responseOk($users); } |
使用GET方式访问:http://localhost:8000/users ,结果如图所示:

使用GET方式访问:http://localhost:8000/users/1 ,结果如图所示:

4. 删除user¶
修改src\Model\Logic目录下的UsersLogic.php,添加代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php /** * 删除user */ public function delete($id) { try { $entity = $this->usersTable->get($id); } catch (RecordNotFoundException $e) { return null; } $result = $this->usersTable->delete($entity); return $result; } |
修改src\Controller目录下的UsersController.php,添加代码:
1 2 3 4 5 6 7 8 9 10 | <?php /** * 接受DELETE请求 * 方法名固定,不可修改 */ public function delete($id) { $result = $this->userLogic->delete($id); $this->responseOk($result); } |
使用DELETE方式访问:http://localhost:8000/users/1 ,结果如图所示:

二、User关联Address, Area¶
1. 添加关联关系¶
在src\Model\Table目录下新建AddressesTable.php,写入下面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php namespace App\Model\Table; class AddressesTable extends BaseTable { public function initialize(array $config) { parent::initialize($config); $this->belongsToMany('Areas', [ 'joinTable' => 'addresses_areas', 'foreignKey' => 'address_id', // 中间表和本表对应的外键名称 'bindingKey' => 'id', // 本表和中间表外键相对应的键名 'targetForeignKey' => 'area_id', // 中间表和关联表对应的外键名称 'finder' => 'threaded' // 查询嵌套数组 ]); $this->setTable('addresses'); } } |
在src\Model\Table目录下新建AreasTable.php,写入下面的代码:
1 2 3 4 5 6 7 8 9 10 11 | <?php namespace App\Model\Table; class AreasTable extends BaseTable { public function initialize(array $config) { parent::initialize($config); $this->setTable('areas'); } } |
在src\Model\Entity下新建Address.php,写入代码:
1 2 3 4 5 6 | <?php namespace App\Model\Entity; class Address extends BaseEntity { } |
在src\Model\Entity下新建Area.php,写入代码:
1 2 3 4 5 6 | <?php namespace App\Model\Entity; class Area extends BaseEntity { } |
修改src\Model\Table目录下的UsersTable.php,将initialize方法更改为如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php public function initialize(array $config) { parent::initialize($config); $this->hasMany('Addresses', [ 'foreignKey' => 'user_id', // 关联表的外键名称 'bindingKey' => 'id', // 本表中和关联表相对应的键名 'dependent' => true, // 为true时,会级联删除关联信息 'cascadeCallbacks' => true ]); $this->setTable('users'); } |
2. 新增¶
修改src\Model\Logic目录下的UsersLogic.php,将add方法修改为如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php /** * 添加user */ public function add($data) { $newData = $this->camelToUnderscore($data); $entity = $this->usersTable->newEntity($newData, [ 'associated' => [ 'Addresses' => [ 'associated' => ['Areas'] ] ] ]); $this->usersTable->save($entity); return $this->underscoreToCamel($entity); } |
使用POST方式访问:http://localhost:8000/users ,需要保存的数据如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | { "loginId": "admin2", "phone": "12345678901", "password": "123456", "name": "admin", "avatar": "avatar", "remarks": "remarks", "status": 0, "addresses": [ { "address": "太湖中路25号", "areas": { "_ids": [ 1000, 1001, 1003 ] } } ] } |
结果如图所示:

3. 修改¶
修改src\Model\Logic目录下的UsersLogic.php,将save方法修改为以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php /** * 编辑user */ public function save($id, $data) { $newData = $this->camelToUnderscore($data); try { $entity = $this->usersTable->get($id); } catch (RecordNotFoundException $e) { return null; } $entity = $this->usersTable->patchEntity($entity, $newData, [ 'associated' => [ 'Addresses' => [ 'associated' => ['Areas'] ] ] ]); $this->usersTable->save($entity); return $this->underscoreToCamel($entity); } |
使用PUT方式访问:http://localhost:8000/users/1 ,修改的数据如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | { "loginId": "update-admin2", "phone": "12345678901", "password": "123456", "name": "update-admin2", "avatar": "avatar1", "remarks": "remarks", "status": 1, "addresses": [ { "id": 32, "address": "太湖中路100号", "areas": { "_ids": [ 1000, 1002, 1003 ] } } ] } |
结果如图所示:

4. 查询¶
修改src\Model\Logic目录下的UsersLogic.php,将findUser和findAll方法修改为如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php /** * 根据id查询user */ public function findUser($id) { $user = $this->usersTable->find() ->contain(['Addresses' => ['Areas']]) ->where(['id =' => $id]) ->first(); return $this->underscoreToCamel($user); } /** * 查询所有的user */ public function findAll() { $users = $this->usersTable->find() ->contain(['Addresses' => ['Areas']]); return $this->underscoreToCamel($users); } |
使用GET方式访问:http://localhost:8000/users ,结果如图所示:

使用GET方式访问:http://localhost:8000/users/1 ,结果如图所示:

5. 删除¶
使用DELETE方式访问:http://localhost:8000/users/68 ,结果如图所示:

三、User关联Role¶
1. 添加关联关系¶
在src\Model\Table目录下新建RolesTable.php,写入下面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php namespace App\Model\Table; class RolesTable extends BaseTable { public function initialize(array $config) { parent::initialize($config); $this->belongsToMany('Permissions', [ 'joinTable' => 'roles_permissions', 'foreignKey' => 'role_id', // 中间表和本表对应的外键名称 'bindingKey' => 'id', // 本表和中间表外键相对应的键名 'targetForeignKey' => 'permission_id', // 中间表和关联表对应的外键名称 ]); $this->setTable('roles'); } } |
在src\Model\Table目录下新建PermissionsTable.php,写入下面的代码:
1 2 3 4 5 6 7 8 9 10 11 | <?php namespace App\Model\Table; class PermissionsTable extends BaseTable { public function initialize(array $config) { parent::initialize($config); $this->setTable('permissions'); } } |
在src\Model\Entity下新建Role.php,写入代码:
1 2 3 4 5 6 | <?php namespace App\Model\Entity; class Role extends BaseEntity { } |
在src\Model\Entity下新建Permission.php,写入代码:
1 2 3 4 5 6 | <?php namespace App\Model\Entity; class Permission extends BaseEntity { } |
修改src\Model\Table目录下的UsersTable.php,将initialize方法更改为如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php public function initialize(array $config) { parent::initialize($config); $this->belongsToMany('Roles', [ 'joinTable' => 'users_roles', 'foreignKey' => 'user_id', // 中间表和本表对应的外键名称 'bindingKey' => 'id', // 本表和中间表外键相对应的键名 'targetForeignKey' => 'role_id', // 中间表和关联表对应的外键名称 ]); $this->hasMany('Addresses', [ 'foreignKey' => 'user_id', // 关联表的外键名称 'bindingKey' => 'id', // 本表中和关联表相对应的键名 'dependent' => true, // 为true时,会级联删除关联信息 'cascadeCallbacks' => true ]); $this->setTable('users'); } |
2. 新增¶
修改src\Model\Logic目录下的UsersLogic.php,将add方法修改为如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php /** * 添加user */ public function add($data) { $newData = $this->camelToUnderscore($data); $entity = $this->usersTable->newEntity($newData, [ 'associated' => [ 'Addresses' => [ 'associated' => ['Areas'] ], 'Roles' ] ]); $this->usersTable->save($entity); return $this->underscoreToCamel($entity); } |
使用POST方式访问:http://localhost:8000/users ,需要保存的数据如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | { "loginId": "admin2", "phone": "12345678901", "password": "123456", "name": "admin", "avatar": "avatar", "remarks": "remarks", "status": 0, "addresses": [ { "address": "太湖中路25号", "areas": { "_ids": [ 1000, 1001, 1003 ] } } ], "roles": { "_ids": [ 1, 2 ] } } |
结果如图所示:

3. 修改¶
修改src\Model\Logic目录下的UsersLogic.php,将save方法修改为以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php /** * 编辑user */ public function save($id, $data) { $newData = $this->camelToUnderscore($data); try { $entity = $this->usersTable->get($id); } catch (RecordNotFoundException $e) { return null; } $entity = $this->usersTable->patchEntity($entity, $newData, [ 'associated' => [ 'Addresses' => [ 'associated' => ['Areas'] ], 'Roles' ] ]); $this->usersTable->save($entity); return $this->underscoreToCamel($entity); } |
使用PUT方式访问:http://localhost:8000/users/1 ,修改的数据如下所示:
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 | { "loginId": "update-admin", "phone": "12345678901", "password": "123456", "name": "admin", "avatar": "avatar", "remarks": "remarks", "status": 0, "addresses": [ { "id": 34, "address": "太湖中路1000号", "areas": { "_ids": [ 1000, 1001, 1003 ] } } ], "roles": { "_ids": [ 1, 3 ] } } |
结果如图所示:

4. 查询¶
修改src\Model\Logic目录下的UsersLogic.php,将findUser和findAll方法修改为如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php /** * 根据id查询user */ public function findUser($id) { $user = $this->usersTable->find() ->contain(['Roles' => ['Permissions'], 'Addresses' => ['Areas']]) ->where(['id =' => $id]) ->first(); return $this->underscoreToCamel($user); } /** * 查询所有的user */ public function findAll() { $users = $this->usersTable->find() ->contain(['Roles' => ['Permissions'], 'Addresses' => ['Areas']]); return $this->underscoreToCamel($users); } |
使用GET方式访问:http://localhost:8000/users ,结果如图所示:

使用GET方式访问:http://localhost:8000/users/1 ,结果如图所示:

5. 删除¶
使用DELETE方式访问:http://localhost:8000/users/68 ,结果如图所示:
