我的控制器中有一个AJAX函数:
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(应该是每个记录的插入id)等于0。
我需要返回insert_id来构建DOM元素,然后我将插入到DOM中。
有人知道为什么它是零吗?
按如下方法修改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;
}
}