【发布时间】:2016-06-23 21:57:19
【问题描述】:
我有一个应用程序应该将每笔交易都保存在我的数据库中。控制器代码如下:
$info = array(
'student_id' => $this->user['id'],
'transaction_id' => $ref,
'value' => $amount,
'final_value' => $final_value,
'payment_status' => '0',
'date' => date('Y-m-d'),
);
$this->load->model('student_model');
if($this->student_model->insertPurchaseForStudent($info)){
// Some other code here
}
这是我的模型('student_model'):
public function insertPurchaseForStudent($info){
if($this->db->insert('compra', $this->db->escape($info))){
return true;
}else{
return false;
}
}
我非常头疼,因为有些事务保存在数据库中,而另一些则没有。我认为这可能是我的数据库服务器上的某种不稳定因素,所以我添加了一个 for 循环,以便多次尝试,并在无法保存时记录错误消息:
$i = 1;
for ($i == 1; $i <= 5; $i++) {
if($this->student_model->insertPurchaseForStudent($info)){
// to leave the loop
$i = 7;
// Some other code here
}else{
sleep(3);
switch($i){
case 2 :
log_message('error', 'Second attempt to add transaction');
break;
case 3 :
log_message('error', 'Third attempt to add transaction');
break;
case 4 :
log_message('error', 'Fourth attempt to add transaction');
break;
case 5 :
log_message('error', 'Fifth attempt to add transaction');
break;
case 6 :
log_message('error', 'Sixth attempt, giving up');
break;
}
}
}
问题是,当它不保存事务时,我的错误日志上看不到任何消息。
我非常感谢任何关于可能原因的想法,或者我在我编写的这段代码中滑倒的地方。
干杯。
【问题讨论】:
-
对不起@quantme,$info 是一个包含交易信息的数组。我已经更新了我的问题。谢谢!
-
$this->user['id']来自先前/当前的数据库操作还是来自会话? -
我试图复制错误,还没有。如果您仍然有问题,您必须提供更多信息(代码)。
-
@quantme,感谢您的帮助。
$this->user['id']来自一个会话:$this->data['user'] = $this->user = $this->session->userdata('user'); -
你在会话中使用什么driver?
标签: php mysql codeigniter activerecord