简体   繁体   English

Codeigniter模型未保存在数据库中

[英]Codeigniter model not saving on database

I have an application that should save every transaction on my database. 我有一个应将每个事务保存在数据库中的应用程序。 The controller code goes like this: 控制器代码如下所示:

$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 

}

And this is my model ('student_model'): 这是我的模型(“ student_model”):

public function insertPurchaseForStudent($info){
  if($this->db->insert('compra', $this->db->escape($info))){
    return true;
  }else{
    return false;
  }
}

I'm having an enormous headache because some transactions are saved on the database and others are not. 我非常头痛,因为有些事务保存在数据库中,而另一些则没有。 I thought it could be some kind of instability on my database servers, so I've added a for loop , to try more than once, and to log an error message if it's not able to save: 我以为这可能是数据库服务器上的某种不稳定因素,所以我添加了一个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;
    }
  }
}

The problem is, when it does not save the transaction, I don't see any messages on my error log. 问题是,当它不保存事务时,我的错误日志中没有看到任何消息。

I'd be really grateful with any ideas of what's the possible cause, or where am I slipping in this code I wrote. 我真的很感激任何可能的原因,或者我在编写的这段代码中的错误之处。

Cheers. 干杯。

Try using $this->db->set($data) like this: 尝试像这样使用$this->db->set($data)

public function insertPurchaseForStudent($info){
  $this->db->set($info);
  if($this->db->insert('compra'){
    return true;
  } else {
    return false;
  }
}

You can find more example using set() in CodeIgniter Manual Inserting Data 您可以在CodeIgniter手册插入数据中找到使用set()更多示例。

See if it works. 看看是否可行。

First of all I want to thank everyone who helped me with this. 首先,我要感谢所有为此提供帮助的人。

I've finally found the problem. 我终于找到了问题。 And it's lame, I'm sorry. 很遗憾,对不起。

The model and the controller were all good. 模型和控制器都很好。 They were saving the transaction in my DB. 他们将交易保存在我的数据库中。

But I have a badly named function ' courseForStudentCheck() ' that not only checks if the student is enrolled, but also deletes it from my DB. 但是我有一个名字不正确的函数' courseForStudentCheck() ',它不仅检查学生是否已入学,还从我的数据库中删除了它。 I need that for some cases, but I should've named it in a different way, because inadvertently I started using it just to check if a user was enrolled. 在某些情况下,我需要使用它,但是我应该以其他方式命名它,因为无意间我开始使用它只是为了检查是否已注册用户。 And that's where the problem started. 这就是问题开始的地方。

Thanks again! 再次感谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM