2015-07-17 4 views
0

У меня есть функция AJAX в моем контроллере:db-> insert_id() Всегда равен нулю

public function add_display_row($shape, $rows) { 
     $newRecord = array(
      'work_id' => '', 
      'section_id' => (int)$rows + 1, 
      'shape' => $shape 
     ); 

     //insert new record after last 
     $newro = array(); 
     for($c=1; $c<6; $c++){ 
      $newRecord['ordinal'] = $c; 
      $newRecord['size_id'] = $this->work_model->get_size_from_specs($shape, $c); 
      $insNew = $this->work_model->save_new_featured_shape($newRecord); 
      $newRecord['item_id'] = $insNew; 
      array_push($newro, $newRecord); 
     } 

     print_r($newro); 
    } 

А в модели

public function save_new_featured_shape($record) { 
     $this->db->trans_begin(); 
     $this->db->insert('work_featured', $record); 

     if ($this->db->trans_status() === FALSE) { 
     $this->db->trans_rollback(); 
     return false; 
     } else { 
     $this->db->trans_commit(); 
     $insert_id = $this->db->insert_id(); 
     return $insert_id; 
     } 
    } 

После всех 5 записей вставлены, я возвращая массив для вызывающего объекта, а item_id (который должен быть идентификатором вставки каждого) равен 0.

Мне нужно, чтобы insert_id возвращался для создания элемента DOM, который я затем вставляю в DOM.

Может ли кто-нибудь понять, почему он равен нулю?

+0

Вставьте данные в базу данных ??? – Saty

+0

данные вставляются в базу данных правильно – jgravois

ответ

1

изменить add_display_row как показано ниже.

public function add_display_row($shape, $rows) { 
    //$this->load->model('work_model'); 
    $newRecord = array(
     'work_id' => '', 
     'section_id' => (int)$rows + 1, 
     'shape' => $shape 
    ); 
    $new_arr=array(); 
    //insert new record after last 
    $newro = array(); 
    for($c=1; $c<6; $c++){ 
     $newRecord['ordinal'] = $c; 
     $newRecord['size_id'] = $this->work_model->get_size_from_specs($shape, $c); 
     $insNew = $this->save_new_featured_shape($newRecord); 
     $new_arr['item_id'] = $insNew; 
     $new_arr_arr=array_merge($newRecord,$new_arr); 
     array_push($newro, $new_arr_arr); 
    } 
    echo "<pre>"; 
    print_r($newro); 
    } 

и модель функция как нижеследующий.

public function save_new_featured_shape($record) { 
    $this->db->trans_begin(); 
    $this->db->insert('work_featured', $record); 
    $insert_id = $this->db->insert_id(); 
    if ($this->db->trans_status() === FALSE) { 
    $this->db->trans_rollback(); 
    return false; 
    } else { 
    $this->db->trans_commit(); 

    return $insert_id; 
    } 
}