当我删除记录用户时,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 我希望有人能帮助我,谢谢
答案 0 :(得分:3)
请勿重新编号。接受由于删除其他原因(如异常中止的交易,INSERT IGNORE等)而导致AI方面存在漏洞。
更改PK值时,将更改它们之间的FK关系。
使用INT UNSIGNED
或BIGINT UNSIGNED
类型的ID,您将永远不会用完ID。