gpt4 book ai didi

php - 每个用户自动递增

转载 作者:行者123 更新时间:2023-11-30 23:48:44 24 4
gpt4 key购买 nike

我有一张发票表。每张发票都属于一个用户。

所以table表有id,user_id列。

我想要第三列,例如基于用户自动递增的 invoice_id。

因此,例如用户 1 将拥有 id 1,2,3,4,5,6,7,8...user 的发票2 还将具有 id 1,2,3,4,5,6,7,8...

的发票

我知道我可以创建一个函数来检查特定用户的最后一个值并分配下一个值但是没有其他“更简单”更合适的方法来使用 cakephp mysql?

最佳答案

您必须在 Invoice 模型的 beforeSave 回调中执行此操作

类似于:

public function beforeSave($options = array())
{

if(!$this->id)
{
$user_id = $this->data['Invoice']['user_id'];
$invoice = $this->find(
'first',
array
(
'fields' => array('MAX(invoice_id) AS invoice_id'),
'conditions' => array('Invoice.user_id' => $user_id )
)
);
$invoice_id = $invoice [0]['invoice_id'] + 1 ;


$this->data['Invoice']['invoice_id'] = $invoice_id ;
}
return true;
}

上面的代码应该可以工作。无论如何,我不确定这是实现该目标的最佳方法,因此请继续寻找其他解决方案。

关于php - 每个用户自动递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22906455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com