0

当我删除记录Users时,ID主键自增一直在跳,我想让ID自增又是序列,这个示例

|  ID  |  UserName |
|   1  |   Budi    |
|   2  |   Joko    |    
|   3  |   Amir    |

当我删除用户 Joko,然后我添加新的其他用户时,ID 号正在跳跃

|  ID  | UserName |
|  1   |   Budi   |
|  3   |   Amir   |
|  4   |   Faris  |

当我浏览解决方案时,我得到了一些解决方案,但不起作用。在这里我添加了修改过的文件

config/app.php
'SQLkonek' => [
      'className' => 'Cake\Database\Connection',
      'driver' => 'Cake\Database\Driver\Mysql',
      'persistent' => false,
      'host' => 'localhost',
      'username' => 'root',
      'paassword' => ' ',
      'database' => 'klinikucing',
      'encoding' => 'utf8mb4',
      'timezone' => ' ',
      'cacheMetadata' => true
]

然后我通过上面调用修改

controller/UsersController.php
public function delete ($id = null)
{
   $this->request->allowMethod(['post', 'delete']);
   $kon = ConnectionManager::get('SQLkonek');
   $user = $this->Users->get($id);
   $stm = $kon->execute(array(
                  ['SET @count = 0'],
                  ['DELETE FROM users WHERE ID = :ID'],
                  ['UPDATE users SET users.ID = @count:= @count + 1'],
                  ['ALTER TABLE users AUTO_INCREMENT =1']
               ))
               ->fetchAll('assoc');
   If($this->Users->$stm) {
         $this->Flash->success(__('Users success delete.'));
   } else {
         $this->Flash->error(__('User delete failed, try again.'));
   }
   return $this->redirect(['action' => 'index']);
}

显示错误消息

Warning (2): PDO::prepare() expects parameter 1 to be string, array given [CORE\src\Database\Driver\Mysql.php, line 138]
Warning (512): Unable to emit headers. Headers sent in file=C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php line=853 [CORE\src\Http\ResponseEmitter.php, line 48]
Warning (2): Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php:853) [CORE\src\Http\ResponseEmitter.php, line 148
Warning (2): Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php:853) [CORE\src\Http\ResponseEmitter.php, line 181]
Argument 1 passed to Cake\Database\Statement\PDOStatement::__construct() must be an instance of PDOStatement or null, boolean given, called in C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Database\Driver\Mysql.php on line 139
Error in: ROOT\vendor\cakephp\cakephp\src\Database\Statement\PDOStatement.php, line 33

我的 CakePHP 版本是 3.7.2 我希望有人能帮助我,thanx

4

1 回答 1

3

不要重新编号。接受由于删除其他原因(如中止事务、INSERT IGNORE 等)而导致 AI 存在差距。

当您更改 PK 值时,您会更改它们所具有的 FK 关系。

使用类型的 ID,INT UNSIGNED否则BIGINT UNSIGNED您将永远不会用完 ID。

于 2019-03-12T03:45:05.830 回答